misskey2telegram/modules/search_user.py

71 lines
3.0 KiB
Python
Raw Normal View History

2023-01-17 17:11:26 +00:00
import contextlib
2022-12-25 14:42:07 +00:00
from mipac.errors import InternalErrorError, AlreadyFollowingError, FolloweeIsYourselfError
2022-12-25 09:40:41 +00:00
from pyrogram import Client, filters
2022-12-25 14:42:07 +00:00
from pyrogram.types import Message, CallbackQuery
2022-12-25 09:40:41 +00:00
from defs.search_user import search_user, gen_text, gen_button
2023-01-27 12:36:41 +00:00
from misskey_init import get_misskey_bot
from models.filters import notice_filter
2022-12-25 09:40:41 +00:00
# @xxx
2023-01-27 12:36:41 +00:00
@Client.on_message(filters.incoming & notice_filter & filters.regex(r"^@(.*)$"))
2022-12-25 09:40:41 +00:00
async def search_user_command(_: Client, message: Message):
"""
搜索用户
"""
username = message.matches[0].group(1)
path = username.strip().split("@")
username = path[0]
host = path[1] if len(path) > 1 else None
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(message.from_user.id)
user = await search_user(misskey_bot, username, host)
2022-12-25 09:40:41 +00:00
if not user:
return await message.reply("没有找到用户", quote=True)
text, button = gen_text(user), gen_button(user)
if user.avatar_url:
try:
await message.reply_photo(user.avatar_url, caption=text, reply_markup=button, quote=True)
except Exception:
await message.reply(text, reply_markup=button, quote=True, disable_web_page_preview=True)
else:
await message.reply(text, reply_markup=button, quote=True, disable_web_page_preview=True)
except Exception as e:
return await message.reply(f"搜索用户失败:{e}", quote=True)
2022-12-25 14:42:07 +00:00
# follow:xxx
2023-01-27 12:36:41 +00:00
@Client.on_callback_query(filters.regex(r"^follow:(.*)$") & notice_filter)
2022-12-25 14:42:07 +00:00
async def follow_user_callback(_: Client, callback_query: CallbackQuery):
"""
关注/取消关注用户
"""
user_id = callback_query.matches[0].group(1)
2023-01-17 17:11:26 +00:00
button = callback_query.message.reply_markup
follow = True
2022-12-25 14:42:07 +00:00
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(callback_query.from_user.id)
2022-12-25 14:42:07 +00:00
await misskey_bot.core.api.follow.action.add(user_id)
await callback_query.answer("关注成功", show_alert=True)
except InternalErrorError:
await callback_query.answer("关注申请未批准,请等待对方同意", show_alert=True)
except AlreadyFollowingError:
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(callback_query.from_user.id)
2022-12-25 14:42:07 +00:00
await misskey_bot.core.api.follow.action.remove(user_id)
await callback_query.answer("取消关注成功", show_alert=True)
2023-01-17 17:11:26 +00:00
follow = False
2022-12-25 14:42:07 +00:00
except Exception as e:
await callback_query.answer("取消关注失败", show_alert=True)
await callback_query.message.reply(f"取消关注失败:{e}", quote=True)
except FolloweeIsYourselfError:
await callback_query.answer("不能关注自己", show_alert=True)
except Exception as e:
await callback_query.answer("关注失败", show_alert=True)
await callback_query.message.reply(f"关注失败:{e}", quote=True)
2023-01-17 17:11:26 +00:00
if button:
with contextlib.suppress(Exception):
button.inline_keyboard[1][0].text = "" if follow else ""
await callback_query.message.edit_reply_markup(button)