fix: only self timeline start bot

This commit is contained in:
xtaodada 2023-07-20 23:54:58 +08:00
parent 37e2294a48
commit 059d22b2bb
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 41 additions and 17 deletions

View File

@ -45,6 +45,7 @@ class MisskeyBot(commands.Bot):
await Router(ws).connect_channel(["main", "home"]) await Router(ws).connect_channel(["main", "home"])
async def on_note(self, note: Note): async def on_note(self, note: Note):
if self.tg_user.chat_id != 0 and self.tg_user.timeline_topic != 0:
await send_update( await send_update(
self.tg_user.host, self.tg_user.host,
self.tg_user.chat_id, self.tg_user.chat_id,
@ -58,29 +59,41 @@ class MisskeyBot(commands.Bot):
) )
async def on_user_followed(self, notice: NotificationFollow): async def on_user_followed(self, notice: NotificationFollow):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await send_user_followed( await send_user_followed(
self.tg_user.chat_id, notice, self.tg_user.notice_topic self.tg_user.chat_id, notice, self.tg_user.notice_topic
) )
async def on_follow_request(self, notice: NotificationFollowRequest): async def on_follow_request(self, notice: NotificationFollowRequest):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await send_follow_request( await send_follow_request(
self.tg_user.chat_id, notice, self.tg_user.notice_topic self.tg_user.chat_id, notice, self.tg_user.notice_topic
) )
async def on_follow_request_accept(self, notice: NotificationFollowRequest): async def on_follow_request_accept(self, notice: NotificationFollowRequest):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await send_follow_request_accept( await send_follow_request_accept(
self.tg_user.chat_id, notice, self.tg_user.notice_topic self.tg_user.chat_id, notice, self.tg_user.notice_topic
) )
async def on_chat(self, message: ChatMessage): async def on_chat(self, message: ChatMessage):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await send_chat_message( await send_chat_message(
self.tg_user.host, self.tg_user.chat_id, message, self.tg_user.notice_topic self.tg_user.host, self.tg_user.chat_id, message, self.tg_user.notice_topic
) )
async def on_chat_unread_message(self, message: ChatMessage): async def on_chat_unread_message(self, message: ChatMessage):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await message.api.read() await message.api.read()
async def on_achievement_earned(self, notice: NotificationAchievement): async def on_achievement_earned(self, notice: NotificationAchievement):
if self.tg_user.chat_id == 0 or self.tg_user.notice_topic == 0:
return
await send_achievement_earned( await send_achievement_earned(
self.tg_user.chat_id, notice, self.tg_user.notice_topic self.tg_user.chat_id, notice, self.tg_user.notice_topic
) )
@ -143,10 +156,12 @@ async def init_misskey_bot():
await sqlite.create_db_and_tables() await sqlite.create_db_and_tables()
count = 0 count = 0
for user in await UserAction.get_all_token_ok_users(): for user in await UserAction.get_all_token_ok_users():
if not await test_token(user.host, user.token): mid = await test_token(user.host, user.token)
if not mid:
user.status = TokenStatusEnum.INVALID_TOKEN user.status = TokenStatusEnum.INVALID_TOKEN
await UserAction.update_user(user) await UserAction.update_user(user)
continue continue
user.instance_user_id = mid
count += 1 count += 1
bot.loop.create_task(run(user)) bot.loop.create_task(run(user))
logs.info(f"初始化 Misskey Bot 完成,共启动 {count} 个 WS 任务") logs.info(f"初始化 Misskey Bot 完成,共启动 {count} 个 WS 任务")

View File

@ -23,6 +23,14 @@ class UserAction:
results = await session.exec(statement) results = await session.exec(statement)
return user[0] if (user := results.first()) else None return user[0] if (user := results.first()) else None
@staticmethod
def check_user_if_ok(user: User) -> bool:
if user.chat_id != 0 and user.timeline_topic != 0 and user.notice_topic != 0:
return True
if user.push_chat_id != 0:
return True
return False
@staticmethod @staticmethod
async def get_user_if_ok(user_id: int) -> Optional[User]: async def get_user_if_ok(user_id: int) -> Optional[User]:
async with sqlite.session() as session: async with sqlite.session() as session:
@ -31,14 +39,14 @@ class UserAction:
select(User) select(User)
.where(User.user_id == user_id) .where(User.user_id == user_id)
.where(User.status == TokenStatusEnum.STATUS_SUCCESS) .where(User.status == TokenStatusEnum.STATUS_SUCCESS)
.where(User.chat_id != 0)
.where(User.timeline_topic != 0)
.where(User.notice_topic != 0)
.where(User.token != "") .where(User.token != "")
.where(User.host != "") .where(User.host != "")
) )
results = await session.exec(statement) results = await session.exec(statement)
return user[0] if (user := results.first()) else None user = user[0] if (user := results.first()) else None
if user is not None and UserAction.check_user_if_ok(user):
return user
return None
@staticmethod @staticmethod
async def get_all_token_ok_users() -> list[User]: async def get_all_token_ok_users() -> list[User]:
@ -47,14 +55,15 @@ class UserAction:
statement = ( statement = (
select(User) select(User)
.where(User.status == TokenStatusEnum.STATUS_SUCCESS) .where(User.status == TokenStatusEnum.STATUS_SUCCESS)
.where(User.chat_id != 0)
.where(User.timeline_topic != 0)
.where(User.notice_topic != 0)
.where(User.token != "") .where(User.token != "")
.where(User.host != "")
) )
results = await session.exec(statement) results = await session.exec(statement)
users = results.all() return [
return [user[0] for user in users] user[0]
for user in results.all()
if UserAction.check_user_if_ok(user[0])
]
@staticmethod @staticmethod
async def update_user(user: User): async def update_user(user: User):