Last active
August 29, 2021 10:17
-
-
Save monsterxcn/f7a31e6a16c54ba10c8c0fd8ba574804 to your computer and use it in GitHub Desktop.
Akinator 网络天才 NoneBot2 插件 via @NinjaSnail1080/akinator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akinator | |
from akinator.async_aki import Akinator | |
from typing import Optional | |
from nonebot.adapters.cqhttp import Bot, Message | |
from nonebot.adapters.cqhttp.event import MessageEvent | |
from nonebot.log import logger | |
from nonebot.plugin import on_command | |
aki = Akinator() | |
akiMatch = on_command('你来猜猜看', priority=1) | |
@akiMatch.handle() | |
async def aki_start_guess(bot: Bot, event: MessageEvent, state: dict): | |
try: | |
q = await aki.start_game(language='cn') | |
await akiMatch.send(Message(q.replace("?", "")), at_sender=True) | |
except Exception as e: | |
logger.error(str(e)) | |
await akiMatch.finish(Message("未能与 Akinator 建立连接 😤")) | |
@akiMatch.receive() | |
async def receive(bot: Bot, event: MessageEvent, state: dict): | |
answer = { | |
"是" : "0", | |
"不是" : "1", | |
"不知道" : "2", | |
"可能是" : "3", | |
"可能不是" : "4", | |
"上一问" : "b" | |
} | |
args = str(event.message).strip() | |
if args in ["是", "不是", "不知道", "可能是", "可能不是", "上一问"]: | |
state["a"] = answer[args] | |
resp, prog, isEnd = await guess(state) | |
if isEnd: | |
await akiMatch.finish(Message(f"{resp}"), at_sender=True) | |
else: | |
await akiMatch.reject(Message(f"({prog}%) {resp}"), at_sender=True) | |
elif args in ["退出", "不玩了", "溜了", "算了"]: | |
await akiMatch.finish("网络天才期待下次为您服务!", at_sender=True) | |
else: | |
await akiMatch.reject("你的回答只能是以下五种之一:是、可能是、不是、可能不是、不知道!", at_sender=True) | |
async def guess(state: Optional[dict] = None): | |
while aki.progression <= 80: | |
a = state["a"] | |
prog = str(aki.progression).split('.')[0] + '.' + str(aki.progression).split('.')[1][:2] | |
if a == "b": | |
try: | |
q = await aki.back() | |
return q.replace("?", ""), prog, False | |
except akinator.CantGoBackAnyFurther: | |
return "已经到头啦!", prog, False | |
pass | |
else: | |
q = await aki.answer(a) | |
return q.replace("?", ""), prog, False | |
await aki.win() | |
result = f"你在想:{aki.first_guess['name']}({aki.first_guess['description']})!" +\ | |
f"\n[CQ:image,file={aki.first_guess['absolute_picture_path']}]" | |
await aki.close() | |
return result, '100', True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment