SoulofSukuna commited on
Commit
8eb4210
·
1 Parent(s): 0ce6c05

fix: Akinator game (#452)

Browse files
assistant/games.py CHANGED
@@ -1,5 +1,5 @@
1
  # Ultroid - UserBot
2
- # Copyright (C) 2021-2023 TeamUltroid
3
  #
4
  # This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
5
  # PLease read the GNU Affero General Public License in
@@ -14,65 +14,45 @@
14
  """
15
 
16
  import asyncio
17
- import operator
18
  import re
19
- import uuid
20
- from html import unescape
21
- from random import choice, shuffle
22
-
23
- from . import LOGS
24
-
25
- try:
26
- import akinator
27
- except ImportError:
28
- akinator = None
29
- LOGS.error("'akinator' not installed.")
30
-
31
- from telethon.errors.rpcerrorlist import (
32
- BotMethodInvalidError,
33
- ChatSendStickersForbiddenError,
34
- )
35
  from telethon.events import Raw
36
- from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, UpdateMessagePollVote
37
-
38
  from pyUltroid._misc._decorators import ultroid_cmd
39
  from pyUltroid.fns.helper import inline_mention
40
  from pyUltroid.fns.tools import async_searcher
41
-
42
- from . import *
43
-
44
- # -------------------------- Akinator ----------------------- #
45
 
46
  games = {}
47
  aki_photo = "https://graph.org/file/3cc8825c029fd0cab9edc.jpg"
48
 
49
-
50
  @ultroid_cmd(pattern="akinator")
51
  async def akina(e):
52
- if not akinator:
53
- return
54
- sta = akinator.Akinator()
55
- games.update({e.chat_id: {e.id: sta}})
56
  try:
57
  m = await e.client.inline_query(asst.me.username, f"aki_{e.chat_id}_{e.id}")
58
  await m[0].click(e.chat_id)
59
- except BotMethodInvalidError:
 
60
  await asst.send_file(
61
  e.chat_id,
62
  aki_photo,
63
  buttons=Button.inline(get_string("aki_2"), data=f"aki_{e.chat_id}_{e.id}"),
64
  )
65
  except Exception as er:
66
- return await e.eor(f"**ERROR :** `{er}`")
 
67
  if e.out:
68
  await e.delete()
69
 
70
-
71
  @asst_cmd(pattern="akinator", owner=True)
72
  async def _akokk(e):
73
  await akina(e)
74
 
75
-
76
  @callback(re.compile("aki_(.*)"), owner=True)
77
  async def doai(e):
78
  adt = e.pattern_match.group(1).strip().decode("utf-8")
@@ -81,42 +61,52 @@ async def doai(e):
81
  mid = int(dt[1])
82
  await e.edit(get_string("com_1"))
83
  try:
84
- qu = games[ch][mid].start_game(child_mode=True)
85
- # child mode should be promoted
 
 
 
86
  except KeyError:
87
  return await e.answer(get_string("aki_1"), alert=True)
88
- bts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Yes", "No", "Idk"]]
89
- cts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Probably", "Probably Not"]]
90
-
91
- bts = [bts, cts]
92
- # ignored Back Button since it makes the Pagination looks Bad
93
- await e.edit(f"Q. {qu}", buttons=bts)
94
-
95
 
96
  @callback(re.compile("aka_(.*)"), owner=True)
97
  async def okah(e):
98
- mk = e.pattern_match.group(1).decode("utf-8").split("_")
99
- ch = int(mk[0])
100
- mid = int(mk[1])
101
- ans = mk[2]
102
  try:
 
 
 
 
 
 
 
 
 
 
 
103
  gm = games[ch][mid]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  except KeyError:
105
  await e.answer(get_string("aki_3"))
106
- return
107
- text = gm.answer(ans)
108
- if gm.progression >= 80:
109
- gm.win()
110
- gs = gm.first_guess
111
- text = "It's " + gs["name"] + "\n " + gs["description"]
112
- return await e.edit(text, file=gs["absolute_picture_path"])
113
- bts = [Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Yes", "No", "Idk"]]
114
- cts = [
115
- Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Probably", "Probably Not"]
116
- ]
117
-
118
- bts = [bts, cts]
119
- await e.edit(text, buttons=bts)
120
 
121
 
122
  @in_pattern(re.compile("aki_?(.*)"), owner=True)
 
1
  # Ultroid - UserBot
2
+ # Copyright (C) 2021-2024 TeamUltroid
3
  #
4
  # This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
5
  # PLease read the GNU Affero General Public License in
 
14
  """
15
 
16
  import asyncio
 
17
  import re
18
+ from telethon.errors.rpcerrorlist import BotMethodInvalidError
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  from telethon.events import Raw
20
+ from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, UpdateMessagePollVote, InputMessageID
21
+ from telethon.tl.functions.messages import GetMessagesRequest
22
  from pyUltroid._misc._decorators import ultroid_cmd
23
  from pyUltroid.fns.helper import inline_mention
24
  from pyUltroid.fns.tools import async_searcher
25
+ from . import * # Ensure this import matches your project structure
26
+ from akipy.async_akipy import Akinator
 
 
27
 
28
  games = {}
29
  aki_photo = "https://graph.org/file/3cc8825c029fd0cab9edc.jpg"
30
 
 
31
  @ultroid_cmd(pattern="akinator")
32
  async def akina(e):
33
+ sta = Akinator()
34
+ games[e.chat_id] = {e.id: sta}
35
+ LOGS.info(f"Game started for chat {e.chat_id} with ID {e.id}.")
 
36
  try:
37
  m = await e.client.inline_query(asst.me.username, f"aki_{e.chat_id}_{e.id}")
38
  await m[0].click(e.chat_id)
39
+ except BotMethodInvalidError as err:
40
+ LOGS.error(f"BotMethodInvalidError: {err}")
41
  await asst.send_file(
42
  e.chat_id,
43
  aki_photo,
44
  buttons=Button.inline(get_string("aki_2"), data=f"aki_{e.chat_id}_{e.id}"),
45
  )
46
  except Exception as er:
47
+ LOGS.error(f"Unexpected error: {er}")
48
+ return await e.eor(f"ERROR : {er}")
49
  if e.out:
50
  await e.delete()
51
 
 
52
  @asst_cmd(pattern="akinator", owner=True)
53
  async def _akokk(e):
54
  await akina(e)
55
 
 
56
  @callback(re.compile("aki_(.*)"), owner=True)
57
  async def doai(e):
58
  adt = e.pattern_match.group(1).strip().decode("utf-8")
 
61
  mid = int(dt[1])
62
  await e.edit(get_string("com_1"))
63
  try:
64
+ await games[ch][mid].start_game(child_mode=True)
65
+ bts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Yes", "No", "Idk"]]
66
+ cts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Probably", "Probably Not"]]
67
+ bts = [bts, cts]
68
+ await e.edit(f"Q. {games[ch][mid].question}", buttons=bts)
69
  except KeyError:
70
  return await e.answer(get_string("aki_1"), alert=True)
 
 
 
 
 
 
 
71
 
72
  @callback(re.compile("aka_(.*)"), owner=True)
73
  async def okah(e):
 
 
 
 
74
  try:
75
+ mk = e.pattern_match.group(1).decode("utf-8").split("_")
76
+ LOGS.info(f"Parsed values: {mk}")
77
+
78
+ if len(mk) < 3:
79
+ LOGS.error("Pattern match did not return enough parts.")
80
+ return await e.answer("Invalid data received.", alert=True)
81
+
82
+ ch = int(mk[0])
83
+ mid = int(mk[1])
84
+ ans = mk[2]
85
+
86
  gm = games[ch][mid]
87
+ await gm.answer(ans)
88
+
89
+ if gm.progression is None:
90
+ gm.progression = 0
91
+
92
+ if int(float(gm.progression)) >= 80:
93
+ gm.win = True
94
+ if int(gm.step) > 3:
95
+ text = f"It's {gm.name_proposition}\n{gm.description_proposition}"
96
+ else:
97
+ text = f"Ha, You cant fool me!"
98
+ await e.edit(text, file=gm.photo)
99
+ else:
100
+ buttons = [
101
+ [Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Yes", "No", "Idk"]],
102
+ [Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Probably", "Probably Not"]]
103
+ ]
104
+ await e.edit(gm.question, buttons=buttons)
105
+
106
  except KeyError:
107
  await e.answer(get_string("aki_3"))
108
+ except Exception as ex:
109
+ LOGS.error(f"An unexpected error occurred: {ex}")
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
 
112
  @in_pattern(re.compile("aki_?(.*)"), owner=True)
resources/startup/optional-requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  # Required only for Local Deploys
2
  # ------------------------------------------------------ #
3
 
4
- akinator
5
  apscheduler
6
  aiohttp
7
  bs4
 
1
  # Required only for Local Deploys
2
  # ------------------------------------------------------ #
3
 
4
+ git+https://github.com/ufoptg/akipy.git
5
  apscheduler
6
  aiohttp
7
  bs4