LittlePaimon/Paimon_Plugins/couplets.py

59 lines
1.9 KiB
Python
Raw Normal View History

from urllib.parse import quote
from littlepaimon_utils import aiorequests
from littlepaimon_utils.tools import FreqLimiter
from nonebot import on_command
from nonebot.adapters.onebot.v11 import MessageEvent
from nonebot.params import CommandArg
2022-06-21 16:26:13 +08:00
from nonebot.plugin import PluginMetadata
from ..utils.config import config
from ..utils.message_util import get_message_id
2022-06-21 16:26:13 +08:00
__plugin_meta__ = PluginMetadata(
name="对对联",
description="人工智能和你对对联",
usage=(
"对对联 <对联内容>"
),
extra={
'type': '娱乐',
'range': ['private', 'group', 'guild'],
"author": "惜月 <277073121@qq.com>",
"version": "1.0.0",
},
)
2022-06-09 21:57:56 +08:00
couplets = on_command('对联', aliases={'对对联'}, priority=13, block=True)
2022-06-09 21:57:56 +08:00
couplets.__paimon_help__ = {
"usage": "对对联 <对联内容>",
"introduce": "人工智能和你对对联",
"priority": 8
}
couplets_limit = FreqLimiter(config.paimon_couplets_cd)
@couplets.handle()
async def couplets_handler(event: MessageEvent, msg=CommandArg()):
if not msg:
await couplets.finish('请输入对联内容')
2022-05-20 18:44:18 +08:00
if not couplets_limit.check(get_message_id(event)):
await couplets.finish(f'对联冷却ing(剩余{couplets_limit.left_time(get_message_id(event))}秒)')
else:
msg = str(msg).split(' ')
word = msg[0].strip()
try:
num = int(msg[1]) if len(msg) > 1 else 1
except:
num = 1
num = num if num < 10 else 10
2022-05-20 18:44:18 +08:00
couplets_limit.start_cd(get_message_id(event), config.paimon_couplets_cd)
text = quote(str(word))
2022-06-21 16:26:13 +08:00
url = f'https://seq2seq-couplet-model.rssbrain.com/v0.2/couplet/{text}'
res = await aiorequests.get(url=url)
res = res.json()
result = ''
for n in range(0, num):
result += res['output'][n] + '\n'
await couplets.finish(f'上联:{word}\n下联:{result}')