Skip to content

Commit

Permalink
来点setu
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Nov 22, 2023
1 parent effdb13 commit a564661
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions kmua/callbacks/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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该用户将具有同等于群主的权限, 慎用)
Expand Down
54 changes: 54 additions & 0 deletions kmua/callbacks/setu.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 8 additions & 6 deletions kmua/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -94,6 +94,7 @@
manage.clear_inactive_user_avatar,
filters=filters.ChatType.PRIVATE,
)
setu_handler = CommandHandler("setu", setu.setu)


# CallbackQueryHandlers
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a564661

Please sign in to comment.