Skip to content

Commit

Permalink
fix(all): 只保存一个guild_id,可能已修复QQ频道私信
Browse files Browse the repository at this point in the history
Closes #241, #236
  • Loading branch information
Ljzd-PRO committed Jan 24, 2024
1 parent e7d1223 commit 12d1c3b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/nonebot_plugin_mystool/command/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ async def _(event: Union[GeneralPrivateMessageEvent], state: T_State, captcha: s
user = PluginDataManager.plugin_data.users[user_id]
# 如果是QQ频道,需要记录频道ID
if isinstance(event, DirectMessageCreateEvent):
user.qq_guilds.setdefault(user_id, set())
user.qq_guilds[user_id].add(event.channel_id)
user.qq_guild[user_id] = event.channel_id
# 1. 通过短信验证码获取 login_ticket / 使用已有 login_ticket
login_status, cookies = await get_login_ticket_by_captcha(phone_number, int(captcha), device_id)
if login_status:
Expand Down
8 changes: 3 additions & 5 deletions src/nonebot_plugin_mystool/command/user_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,10 @@ async def _(
await matcher.finish("⚠️找不到此UUID密钥对应的用户数据")
PluginDataManager.plugin_data.do_user_bind(user_id, target_id)
user = PluginDataManager.plugin_data.users[user_id]
user.qq_guilds.setdefault(user_id, set())
if isinstance(event, DirectMessageCreateEvent):
user.qq_guilds[user_id].add(event.channel_id)
user.qq_guild[user_id] = event.channel_id
elif isinstance(event, MessageCreateEvent):
user.qq_guilds[user_id].add(event.guild_id)
user.qq_guild[user_id] = event.guild_id
if isinstance(event, GeneralGroupMessageEvent):
user.uuid = str(uuid4())
await matcher.send("🔑由于您在群聊中进行绑定,已刷新您的UUID密钥,但不会影响其他已绑定用户")
Expand Down Expand Up @@ -224,8 +223,7 @@ async def _(bot: Bot, event: Union[GeneralGroupMessageEvent]):
if isinstance(event, MessageCreateEvent):
user_id = event.get_user_id()
if user := PluginDataManager.plugin_data.users.get(user_id):
user.qq_guilds.setdefault(user_id, set())
user.qq_guilds[user_id].add(event.guild_id)
user.qq_guild[user_id] = event.guild_id
PluginDataManager.write_plugin_data()

msg_text = f"{PLUGIN.metadata.name}" \
Expand Down
12 changes: 9 additions & 3 deletions src/nonebot_plugin_mystool/model/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from httpx import Cookies
from nonebot.log import logger
from pydantic import BaseModel, ValidationError, validator
from pydantic import BaseModel, ValidationError, validator, Field

from .._version import __version__
from ..model.common import data_path, BaseModelWithSetter, Address, BaseModelWithUpdate, Good, GameRecord
Expand Down Expand Up @@ -370,8 +370,10 @@ class UserData(BaseModelWithSetter):
"""是否开启通知"""
uuid: Optional[str] = None
"""用户UUID密钥,用于不同NoneBot适配器平台之间的数据同步,因此不可泄露"""
qq_guilds: Optional[Dict[str, Set[int]]] = {}
"""储存用户所在的QQ频道ID {用户ID : [频道ID]}"""
qq_guild: Optional[Dict[str, int]] = {}
"""储存用户所在的QQ频道ID {用户ID : 频道ID}"""
qq_guilds: Optional[Dict[str, List[int]]] = Field(default={}, exclude=True)
"""旧版(v2.1.0 之前)储存用户所在的QQ频道ID {用户ID : [频道ID]}"""
exchange_plans: Union[Set[ExchangePlan], List[ExchangePlan]] = set()
"""兑换计划列表"""
accounts: Dict[str, UserAccount] = {}
Expand Down Expand Up @@ -405,6 +407,10 @@ def __init__(self, **data: Any):
_new_uuid_in_init = True
_uuid_set.add(self.uuid)

# 读取旧版配置中的 qq_guilds 信息,对每个账号取第一个 GuildID 值以生成新的 qq_guild Dict
if not self.qq_guild:
self.qq_guild = {k: v[0] for k, v in filter(lambda x: x[1], self.qq_guilds.items())}

def __hash__(self):
return hash(self.uuid)

Expand Down
6 changes: 2 additions & 4 deletions src/nonebot_plugin_mystool/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,12 @@ async def send_private_msg(
bots = nonebot.get_bots().values()

# 获取 PlatformTarget 对象
if guild_id or ((user := PluginDataManager.plugin_data.users.get(user_id)) and user_id in user.qq_guilds):
if guild_id or ((user := PluginDataManager.plugin_data.users.get(user_id)) and user_id in user.qq_guild.keys()):
if guild_id is None:
if user := PluginDataManager.plugin_data.users.get(user_id):
if not (guilds := user.qq_guilds.get(user_id)):
if not (guild_id := user.qq_guild.get(user_id)):
logger.error(f"{plugin_config.preference.log_head}用户 {user_id} 数据中没有任何频道ID")
return False, None
else:
guild_id = list(guilds)[0]
else:
logger.error(f"{plugin_config.preference.log_head}用户数据中不存在用户 {user_id},无法获取频道ID")
return False, None
Expand Down

0 comments on commit 12d1c3b

Please sign in to comment.