Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Akinator #452

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 50 additions & 60 deletions assistant/games.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ultroid - UserBot
# Copyright (C) 2021-2023 TeamUltroid
# Copyright (C) 2021-2024 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
Expand All @@ -14,65 +14,45 @@
"""

import asyncio
import operator
import re
import uuid
from html import unescape
from random import choice, shuffle

from . import LOGS

try:
import akinator
except ImportError:
akinator = None
LOGS.error("'akinator' not installed.")

from telethon.errors.rpcerrorlist import (
BotMethodInvalidError,
ChatSendStickersForbiddenError,
)
from telethon.errors.rpcerrorlist import BotMethodInvalidError
from telethon.events import Raw
from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, UpdateMessagePollVote

from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, UpdateMessagePollVote, InputMessageID
from telethon.tl.functions.messages import GetMessagesRequest
from pyUltroid._misc._decorators import ultroid_cmd
from pyUltroid.fns.helper import inline_mention
from pyUltroid.fns.tools import async_searcher

from . import *

# -------------------------- Akinator ----------------------- #
from . import * # Ensure this import matches your project structure
from akipy.async_akipy import Akinator

games = {}
aki_photo = "https://graph.org/file/3cc8825c029fd0cab9edc.jpg"


@ultroid_cmd(pattern="akinator")
async def akina(e):
if not akinator:
return
sta = akinator.Akinator()
games.update({e.chat_id: {e.id: sta}})
sta = Akinator()
games[e.chat_id] = {e.id: sta}
LOGS.info(f"Game started for chat {e.chat_id} with ID {e.id}.")
try:
m = await e.client.inline_query(asst.me.username, f"aki_{e.chat_id}_{e.id}")
await m[0].click(e.chat_id)
except BotMethodInvalidError:
except BotMethodInvalidError as err:
LOGS.error(f"BotMethodInvalidError: {err}")
await asst.send_file(
e.chat_id,
aki_photo,
buttons=Button.inline(get_string("aki_2"), data=f"aki_{e.chat_id}_{e.id}"),
)
except Exception as er:
return await e.eor(f"**ERROR :** `{er}`")
LOGS.error(f"Unexpected error: {er}")
return await e.eor(f"ERROR : {er}")
if e.out:
await e.delete()


@asst_cmd(pattern="akinator", owner=True)
async def _akokk(e):
await akina(e)


@callback(re.compile("aki_(.*)"), owner=True)
async def doai(e):
adt = e.pattern_match.group(1).strip().decode("utf-8")
Expand All @@ -81,42 +61,52 @@ async def doai(e):
mid = int(dt[1])
await e.edit(get_string("com_1"))
try:
qu = games[ch][mid].start_game(child_mode=True)
# child mode should be promoted
await games[ch][mid].start_game(child_mode=True)
bts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Yes", "No", "Idk"]]
cts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Probably", "Probably Not"]]
bts = [bts, cts]
await e.edit(f"Q. {games[ch][mid].question}", buttons=bts)
except KeyError:
return await e.answer(get_string("aki_1"), alert=True)
bts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Yes", "No", "Idk"]]
cts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Probably", "Probably Not"]]

bts = [bts, cts]
# ignored Back Button since it makes the Pagination looks Bad
await e.edit(f"Q. {qu}", buttons=bts)


@callback(re.compile("aka_(.*)"), owner=True)
async def okah(e):
mk = e.pattern_match.group(1).decode("utf-8").split("_")
ch = int(mk[0])
mid = int(mk[1])
ans = mk[2]
try:
mk = e.pattern_match.group(1).decode("utf-8").split("_")
LOGS.info(f"Parsed values: {mk}")

if len(mk) < 3:
LOGS.error("Pattern match did not return enough parts.")
return await e.answer("Invalid data received.", alert=True)

ch = int(mk[0])
mid = int(mk[1])
ans = mk[2]

gm = games[ch][mid]
await gm.answer(ans)

if gm.progression is None:
gm.progression = 0

if int(float(gm.progression)) >= 80:
gm.win = True
if int(gm.step) > 3:
text = f"It's {gm.name_proposition}\n{gm.description_proposition}"
else:
text = f"Ha, You cant fool me!"
await e.edit(text, file=gm.photo)
else:
buttons = [
[Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Yes", "No", "Idk"]],
[Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Probably", "Probably Not"]]
]
await e.edit(gm.question, buttons=buttons)

except KeyError:
await e.answer(get_string("aki_3"))
return
text = gm.answer(ans)
if gm.progression >= 80:
gm.win()
gs = gm.first_guess
text = "It's " + gs["name"] + "\n " + gs["description"]
return await e.edit(text, file=gs["absolute_picture_path"])
bts = [Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Yes", "No", "Idk"]]
cts = [
Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Probably", "Probably Not"]
]

bts = [bts, cts]
await e.edit(text, buttons=bts)
except Exception as ex:
LOGS.error(f"An unexpected error occurred: {ex}")


@in_pattern(re.compile("aki_?(.*)"), owner=True)
Expand Down
2 changes: 1 addition & 1 deletion resources/startup/optional-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Required only for Local Deploys
# ------------------------------------------------------ #

akinator
git+https://github.com/ufoptg/akipy.git
apscheduler
aiohttp
bs4
Expand Down
Loading