diff --git a/kmua/callbacks/help.py b/kmua/callbacks/help.py index 318b987..a7d7965 100644 --- a/kmua/callbacks/help.py +++ b/kmua/callbacks/help.py @@ -25,6 +25,7 @@ async def help(update: Update, context: ContextTypes.DEFAULT_TYPE): /qrand - 随机语录 /t - 获取头衔|互赠头衔 /setqp - 设置主动发送语录的概率 +/setu - 来点色图 (/ω\*) /id - 获取聊天ID /set_greet - 设置群组欢迎语 /set_bot_admin - 在群组中设置bot管理员 (对于bot该用户将具有同等于群主的权限, 慎用) diff --git a/kmua/callbacks/setu.py b/kmua/callbacks/setu.py new file mode 100644 index 0000000..aa79cf0 --- /dev/null +++ b/kmua/callbacks/setu.py @@ -0,0 +1,54 @@ +import asyncio + +import httpx +from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update +from telegram.ext import ContextTypes + +from kmua.config import settings +from kmua.logger import logger + +_api_url: str = settings.get("manyacg_api") +_api_url = _api_url.removesuffix("/") if _api_url else None + + +async def setu(update: Update, context: ContextTypes.DEFAULT_TYPE): + logger.info( + f"[{update.effective_chat.title}]({update.effective_user.name})" + + f" {update.effective_message.text}" + ) + if not _api_url: + return + + if context.user_data.get("setu_cd", False): + await update.effective_message.reply_text(text="太快了, 不行!", quote=True) + return + context.user_data["setu_cd"] = True + + try: + async with httpx.AsyncClient() as client: + resp = await client.get(url=f"{_api_url}/v1/artwork/random") + logger.debug(f"resp: {resp.json()}") + if resp.status_code != 200: + await update.effective_message.reply_text(text="失败惹,请稍后再试", quote=True) + return + await update.effective_message.reply_photo( + photo=resp.json()["data"]["pictures"][0]["direct_url"], + caption=resp.json()["data"]["title"], + reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton( + "Source", url=resp.json()["data"]["source_url"] + ), + InlineKeyboardButton("Channel", url="https://t.me/manyacg"), + ] + ] + ), + quote=True, + ) + except Exception as e: + logger.error(f"setu error: {e.__class__.__name__}:{e}") + await update.effective_message.reply_text(text="失败惹,请稍后再试", quote=True) + finally: + await asyncio.sleep(30) + context.user_data["setu_cd"] = False diff --git a/kmua/handlers.py b/kmua/handlers.py index 0dc72d6..dd9e8d5 100644 --- a/kmua/handlers.py +++ b/kmua/handlers.py @@ -4,36 +4,36 @@ CallbackQueryHandler, ChatMemberHandler, CommandHandler, + InlineQueryHandler, MessageHandler, filters, - InlineQueryHandler, ) from .callbacks import ( + bilibili, chatdata, chatinfo, chatmember, + help, keyword_reply, manage, quote, remake, + setu, slash, start, sticker, - userdata, title, + userdata, waifu, - help, - bilibili, ) +from .config import settings from .filters import ( keyword_reply_filter, mention_or_private_filter, slash_filter, ) from .logger import logger -from .config import settings - # CommandHandlers start_handler = CommandHandler("start", start.start, filters=mention_or_private_filter) @@ -94,6 +94,7 @@ manage.clear_inactive_user_avatar, filters=filters.ChatType.PRIVATE, ) +setu_handler = CommandHandler("setu", setu.setu) # CallbackQueryHandlers @@ -196,6 +197,7 @@ refresh_waifu_data_manually_handler, status_handler, clear_inactive_user_avatar_handler, + setu_handler, # callback handlers user_data_manage_handler, user_data_refresh_handler,