Delete assistant/games.py
Browse files- assistant/games.py +0 -331
assistant/games.py
DELETED
@@ -1,331 +0,0 @@
|
|
1 |
-
# Ultroid - UserBot
|
2 |
-
# Copyright (C) 2021-2025 TeamUltroid
|
3 |
-
#
|
4 |
-
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
|
5 |
-
# PLease read the GNU Affero General Public License in
|
6 |
-
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
|
7 |
-
|
8 |
-
"""
|
9 |
-
• `{i}akinator` | `/akinator`
|
10 |
-
Start akinator game from Userbot/Assistant
|
11 |
-
|
12 |
-
• `/startgame`
|
13 |
-
Open Portal for Games
|
14 |
-
"""
|
15 |
-
|
16 |
-
import asyncio
|
17 |
-
import re, uuid, operator
|
18 |
-
from random import choice, shuffle
|
19 |
-
|
20 |
-
from akipy.async_akipy import Akinator
|
21 |
-
from telethon.errors.rpcerrorlist import BotMethodInvalidError
|
22 |
-
from telethon.events import Raw
|
23 |
-
from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, UpdateMessagePollVote
|
24 |
-
|
25 |
-
from pyUltroid._misc._decorators import ultroid_cmd
|
26 |
-
from logging import getLogger
|
27 |
-
from html import unescape
|
28 |
-
from telethon.tl.types import TextWithEntities
|
29 |
-
from pyUltroid.fns.helper import inline_mention
|
30 |
-
from pyUltroid.fns.tools import async_searcher
|
31 |
-
from telethon.errors import ChatSendStickersForbiddenError
|
32 |
-
|
33 |
-
from . import * # Ensure this import matches your project structure
|
34 |
-
|
35 |
-
games = {}
|
36 |
-
aki_photo = "https://graph.org/file/3cc8825c029fd0cab9edc.jpg"
|
37 |
-
|
38 |
-
akipyLOGS = getLogger("akipy")
|
39 |
-
|
40 |
-
@ultroid_cmd(pattern="akinator")
|
41 |
-
async def akina(e):
|
42 |
-
sta = Akinator()
|
43 |
-
games[e.chat_id] = {e.id: sta}
|
44 |
-
LOGS.info(f"Game started for chat {e.chat_id} with ID {e.id}.")
|
45 |
-
try:
|
46 |
-
m = await e.client.inline_query(asst.me.username, f"aki_{e.chat_id}_{e.id}")
|
47 |
-
await m[0].click(e.chat_id)
|
48 |
-
akipyLOGS.info(f"Clicked inline result for chat {e.chat_id}")
|
49 |
-
except BotMethodInvalidError as err:
|
50 |
-
akipyLOGS.error(f"BotMethodInvalidError: {err}")
|
51 |
-
await asst.send_file(
|
52 |
-
e.chat_id,
|
53 |
-
aki_photo,
|
54 |
-
buttons=Button.inline(get_string("aki_2"), data=f"aki_{e.chat_id}_{e.id}"),
|
55 |
-
)
|
56 |
-
except Exception as er:
|
57 |
-
akipyLOGS.error(f"Unexpected error: {er}")
|
58 |
-
return await e.eor(f"ERROR : {er}")
|
59 |
-
if e.out:
|
60 |
-
await e.delete()
|
61 |
-
|
62 |
-
|
63 |
-
@asst_cmd(pattern="akinator", owner=True)
|
64 |
-
async def _akokk(e):
|
65 |
-
await akina(e)
|
66 |
-
|
67 |
-
|
68 |
-
@callback(re.compile("aki_(.*)"), owner=True)
|
69 |
-
async def doai(e):
|
70 |
-
adt = e.pattern_match.group(1).strip().decode("utf-8")
|
71 |
-
dt = adt.split("_")
|
72 |
-
ch = int(dt[0])
|
73 |
-
mid = int(dt[1])
|
74 |
-
await e.edit(get_string("com_1"))
|
75 |
-
try:
|
76 |
-
await games[ch][mid].start_game(child_mode=False)
|
77 |
-
bts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Yes", "No", "Idk"]]
|
78 |
-
cts = [Button.inline(o, f"aka_{adt}_{o}") for o in ["Probably", "Probably Not"]]
|
79 |
-
bts = [bts, cts]
|
80 |
-
await e.edit(f"Q. {games[ch][mid].question}", buttons=bts)
|
81 |
-
except KeyError:
|
82 |
-
return await e.answer(get_string("aki_1"), alert=True)
|
83 |
-
|
84 |
-
|
85 |
-
@callback(re.compile("aka_(.*)"), owner=True)
|
86 |
-
async def okah(e):
|
87 |
-
try:
|
88 |
-
mk = e.pattern_match.group(1).decode("utf-8").split("_")
|
89 |
-
#akipyLOGS.info(f"Parsed values: {mk}")
|
90 |
-
|
91 |
-
if len(mk) < 3:
|
92 |
-
akipyLOGS.error("Pattern match did not return enough parts.")
|
93 |
-
return await e.answer("Invalid data received.", alert=True)
|
94 |
-
|
95 |
-
ch = int(mk[0])
|
96 |
-
mid = int(mk[1])
|
97 |
-
ans = mk[2]
|
98 |
-
|
99 |
-
gm = games[ch][mid]
|
100 |
-
await gm.answer(ans)
|
101 |
-
|
102 |
-
# Check for the final guess in the API response
|
103 |
-
if gm.name_proposition and gm.description_proposition:
|
104 |
-
gm.win = True
|
105 |
-
text = f"It's {gm.name_proposition}\n{gm.description_proposition}"
|
106 |
-
await e.edit(text, file=gm.photo)
|
107 |
-
else:
|
108 |
-
# Game is not won yet, continue asking questions
|
109 |
-
buttons = [
|
110 |
-
[Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Yes", "No", "Idk"]],
|
111 |
-
[Button.inline(o, f"aka_{ch}_{mid}_{o}") for o in ["Probably", "Probably Not"]],
|
112 |
-
]
|
113 |
-
await e.edit(gm.question, buttons=buttons)
|
114 |
-
|
115 |
-
except KeyError:
|
116 |
-
await e.answer(get_string("aki_3"))
|
117 |
-
except Exception as ex:
|
118 |
-
akipyLOGS.error(f"An unexpected error occurred: {ex}")
|
119 |
-
|
120 |
-
|
121 |
-
@in_pattern(re.compile("aki_?(.*)"), owner=True)
|
122 |
-
async def eiagx(e):
|
123 |
-
bts = Button.inline(get_string("aki_2"), data=e.text)
|
124 |
-
ci = types.InputWebDocument(aki_photo, 0, "image/jpeg", [])
|
125 |
-
ans = [
|
126 |
-
await e.builder.article(
|
127 |
-
"Akinator",
|
128 |
-
type="photo",
|
129 |
-
content=ci,
|
130 |
-
text="Akinator",
|
131 |
-
thumb=ci,
|
132 |
-
buttons=bts,
|
133 |
-
include_media=True,
|
134 |
-
)
|
135 |
-
]
|
136 |
-
await e.answer(ans)
|
137 |
-
|
138 |
-
|
139 |
-
# ----------------------- Main Command ------------------- #
|
140 |
-
|
141 |
-
GIMAGE = "https://graph.org/file/1c51015bae5205a65fd69.jpg"
|
142 |
-
|
143 |
-
@asst_cmd(pattern="startgame", owner=True)
|
144 |
-
async def magic(event):
|
145 |
-
buttons = [
|
146 |
-
[Button.inline("Trivia Quiz", "trzia")],
|
147 |
-
[Button.inline("Cancel ❌", "delit")],
|
148 |
-
]
|
149 |
-
await event.reply(
|
150 |
-
get_string("games_1"),
|
151 |
-
file=GIMAGE,
|
152 |
-
buttons=buttons,
|
153 |
-
)
|
154 |
-
|
155 |
-
|
156 |
-
# -------------------------- Trivia ----------------------- #
|
157 |
-
|
158 |
-
TR_BTS = {}
|
159 |
-
DIFI_KEYS = ["Easy", "Medium", "Hard"]
|
160 |
-
TRIVIA_CHATS = {}
|
161 |
-
POLLS = {}
|
162 |
-
CONGO_STICKER = [
|
163 |
-
"CAADAgADSgIAAladvQrJasZoYBh68AI",
|
164 |
-
"CAADAgADXhIAAuyZKUl879mlR_dkOwI",
|
165 |
-
"CAADAgADpQAD9wLID-xfZCDwOI5LAg",
|
166 |
-
"CAADAgADjAADECECEFZM-SrKO9GgAg",
|
167 |
-
"CAADAgADSwIAAj-VzArAzNCDiGWAHAI",
|
168 |
-
"CAADAgADhQADwZxgDIuMHR9IU10iAg",
|
169 |
-
"CAADAgADiwMAAsSraAuoe2BwYu1sdQI",
|
170 |
-
]
|
171 |
-
|
172 |
-
|
173 |
-
@callback("delit", owner=True)
|
174 |
-
async def delete_it(event):
|
175 |
-
await event.delete()
|
176 |
-
|
177 |
-
|
178 |
-
@callback(re.compile("ctdown(.*)"), owner=True)
|
179 |
-
async def ct_spam(e):
|
180 |
-
n = e.data_match.group(1).decode("utf-8")
|
181 |
-
await e.answer(f"Wait {n} seconds..", alert=True)
|
182 |
-
|
183 |
-
|
184 |
-
@callback(re.compile("trzia(.*)"), owner=True)
|
185 |
-
async def choose_cata(event):
|
186 |
-
match = event.data_match.group(1).decode("utf-8")
|
187 |
-
if not match:
|
188 |
-
if TR_BTS.get("category"):
|
189 |
-
buttons = TR_BTS["category"]
|
190 |
-
else:
|
191 |
-
req = (
|
192 |
-
await async_searcher(
|
193 |
-
"https://opentdb.com/api_category.php", re_json=True
|
194 |
-
)
|
195 |
-
)["trivia_categories"]
|
196 |
-
btt = []
|
197 |
-
for i in req:
|
198 |
-
name = i["name"]
|
199 |
-
if ":" in name:
|
200 |
-
name = name.split(":")[1]
|
201 |
-
btt.append(Button.inline(name, f"trziad_{i['id']}"))
|
202 |
-
buttons = list(zip(btt[::2], btt[1::2]))
|
203 |
-
if len(btt) % 2 == 1:
|
204 |
-
buttons.append((btt[-1],))
|
205 |
-
buttons.append([Button.inline("Cancel ❌", "delit")])
|
206 |
-
TR_BTS.update({"category": buttons})
|
207 |
-
text = get_string("games_2")
|
208 |
-
elif match[0] == "d":
|
209 |
-
cat = match[1:]
|
210 |
-
buttons = [[Button.inline(i, f"trziac{cat}_{i}") for i in DIFI_KEYS]]
|
211 |
-
buttons.append(get_back_button("trzia"))
|
212 |
-
text = get_string("games_3")
|
213 |
-
elif match[0] == "c":
|
214 |
-
m = match[1:]
|
215 |
-
buttons = [[Button.inline(str(i), f"trziat{m}_{i}") for i in range(10, 70, 20)]]
|
216 |
-
text = get_string("games_4")
|
217 |
-
elif match[0] == "t":
|
218 |
-
m_ = match[1:]
|
219 |
-
buttons = [
|
220 |
-
[Button.inline(str(i), f"trzias{m_}_{i}") for i in [10, 30, 60, 120]]
|
221 |
-
]
|
222 |
-
text = get_string("games_5")
|
223 |
-
elif match[0] == "s":
|
224 |
-
chat = event.chat_id
|
225 |
-
cat, le, nu, in_ = match[2:].split("_")
|
226 |
-
msg = await event.edit(get_string("games_6").format(le, nu))
|
227 |
-
for i in reversed(range(5)):
|
228 |
-
msg = await msg.edit(buttons=Button.inline(f"{i} ⏰", f"ctdown{i}"))
|
229 |
-
await asyncio.sleep(1)
|
230 |
-
await msg.edit(
|
231 |
-
msg.text + "\n\n• Send /cancel to stop the Quiz...", buttons=None
|
232 |
-
)
|
233 |
-
qsss = await async_searcher(
|
234 |
-
f"https://opentdb.com/api.php?amount={nu}&category={cat}&difficulty={le.lower()}",
|
235 |
-
re_json=True,
|
236 |
-
)
|
237 |
-
qs = qsss["results"]
|
238 |
-
if not qs:
|
239 |
-
await event.respond("Sorry, No Question Found for the given Criteria..")
|
240 |
-
await event.delete()
|
241 |
-
return
|
242 |
-
TRIVIA_CHATS.update({chat: {}})
|
243 |
-
for copper, q in enumerate(qs):
|
244 |
-
if TRIVIA_CHATS[chat].get("cancel") is not None:
|
245 |
-
break
|
246 |
-
ansi = str(uuid.uuid1()).split("-")[0].encode()
|
247 |
-
opts = [PollAnswer(TextWithEntities(unescape(q["correct_answer"]), entities=[]), ansi)]
|
248 |
-
[
|
249 |
-
opts.append(
|
250 |
-
PollAnswer(TextWithEntities(unescape(a), entities=[]), str(uuid.uuid1()).split("-")[0].encode())
|
251 |
-
)
|
252 |
-
for a in q["incorrect_answers"]
|
253 |
-
]
|
254 |
-
shuffle(opts)
|
255 |
-
poll = InputMediaPoll(
|
256 |
-
Poll(
|
257 |
-
0,
|
258 |
-
TextWithEntities(
|
259 |
-
f"[{copper+1}]. " + unescape(q["question"]),
|
260 |
-
entities=[]
|
261 |
-
),
|
262 |
-
answers=opts,
|
263 |
-
public_voters=True,
|
264 |
-
quiz=True,
|
265 |
-
close_period=int(in_),
|
266 |
-
),
|
267 |
-
correct_answers=[ansi],
|
268 |
-
solution="Join @TeamUltroid",
|
269 |
-
solution_entities=[],
|
270 |
-
)
|
271 |
-
m_ = await event.client.send_message(chat, file=poll)
|
272 |
-
POLLS.update({m_.poll.poll.id: {"chat": m_.chat_id, "answer": ansi}})
|
273 |
-
await asyncio.sleep(int(in_))
|
274 |
-
if not TRIVIA_CHATS[chat]:
|
275 |
-
await event.respond(
|
276 |
-
"No-One Got Any Score in the Quiz!\nBetter Luck Next Time!"
|
277 |
-
)
|
278 |
-
else:
|
279 |
-
try:
|
280 |
-
await event.respond(file=choice(CONGO_STICKER))
|
281 |
-
except ChatSendStickersForbiddenError:
|
282 |
-
pass
|
283 |
-
LBD = "🎯 **Scoreboard of the Quiz.**\n\n"
|
284 |
-
TRC = TRIVIA_CHATS[chat]
|
285 |
-
if "cancel" in TRC.keys():
|
286 |
-
del TRC["cancel"]
|
287 |
-
for userid, user_score in dict(
|
288 |
-
sorted(TRC.items(), key=operator.itemgetter(1), reverse=True)
|
289 |
-
).items():
|
290 |
-
user = inline_mention(await event.client.get_entity(userid))
|
291 |
-
LBD += f"••• {user} - {user_score}\n"
|
292 |
-
await event.respond(LBD)
|
293 |
-
del TRIVIA_CHATS[chat]
|
294 |
-
list_ = list(POLLS.copy().keys())
|
295 |
-
for key in list_:
|
296 |
-
if POLLS[key]["chat"] == chat:
|
297 |
-
del POLLS[key]
|
298 |
-
return
|
299 |
-
await event.edit(text, buttons=buttons)
|
300 |
-
|
301 |
-
|
302 |
-
@asst.on(
|
303 |
-
Raw(UpdateMessagePollVote, func=lambda x: TRIVIA_CHATS and POLLS.get(x.poll_id))
|
304 |
-
)
|
305 |
-
async def pollish(eve: UpdateMessagePollVote):
|
306 |
-
if POLLS.get(eve.poll_id)["chat"] not in TRIVIA_CHATS.keys():
|
307 |
-
return
|
308 |
-
if not eve.options:
|
309 |
-
# Consider as correct answer if no options selected
|
310 |
-
chat = POLLS.get(eve.poll_id)["chat"]
|
311 |
-
user = eve.peer.user_id
|
312 |
-
if not TRIVIA_CHATS.get(chat, {}).get(user):
|
313 |
-
TRIVIA_CHATS[chat][user] = 1
|
314 |
-
else:
|
315 |
-
TRIVIA_CHATS[chat][user] += 1
|
316 |
-
return
|
317 |
-
if POLLS[eve.poll_id]["answer"] != eve.options[0]:
|
318 |
-
return
|
319 |
-
chat = POLLS.get(eve.poll_id)["chat"]
|
320 |
-
user = eve.peer.user_id
|
321 |
-
if not TRIVIA_CHATS.get(chat, {}).get(user):
|
322 |
-
TRIVIA_CHATS[chat][user] = 1
|
323 |
-
else:
|
324 |
-
TRIVIA_CHATS[chat][user] += 1
|
325 |
-
|
326 |
-
|
327 |
-
@asst_cmd("cancel", owner=True, func=lambda x: TRIVIA_CHATS.get(x.chat_id))
|
328 |
-
async def cancelish(event):
|
329 |
-
chat = TRIVIA_CHATS.get(event.chat_id)
|
330 |
-
chat.update({"cancel": True})
|
331 |
-
await event.respond("Cancelled!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|