mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Fix and improve trivia
This commit is contained in:
parent
754019084c
commit
dba6d5aa19
2 changed files with 25 additions and 13 deletions
|
@ -163,8 +163,8 @@ class TelegramBot(GenericBot):
|
|||
return
|
||||
# Prepare data
|
||||
data = self._Data(interface=command.interface, update=update)
|
||||
# Run the command
|
||||
try:
|
||||
# Run the command
|
||||
await command.run(CommandArgs(parameters), data)
|
||||
except InvalidInputError as e:
|
||||
await data.reply(f"⚠️ {e.message}\n"
|
||||
|
@ -178,6 +178,7 @@ class TelegramBot(GenericBot):
|
|||
error_message = f"🦀 [b]{e.__class__.__name__}[/b] 🦀\n"
|
||||
error_message += '\n'.join(e.args)
|
||||
await data.reply(error_message)
|
||||
finally:
|
||||
# Close the data session
|
||||
await data.session_close()
|
||||
|
||||
|
@ -196,9 +197,12 @@ class TelegramBot(GenericBot):
|
|||
return
|
||||
try:
|
||||
response = await callback(data=self._Data(interface=command.interface, update=update))
|
||||
except KeyboardExpiredError:
|
||||
except KeyboardExpiredError as e:
|
||||
# FIXME: May cause a memory leak, as keys are not deleted after use
|
||||
await self.safe_api_call(source.edit_reply_markup, reply_markup=None)
|
||||
if len(e.args) > 0:
|
||||
await self.safe_api_call(query.answer, text=f"⛔️ {e.args[0]}")
|
||||
else:
|
||||
await self.safe_api_call(query.answer, text="⛔️ This keyboard has expired.")
|
||||
return
|
||||
except Exception as e:
|
||||
|
|
|
@ -24,13 +24,19 @@ class TriviaCommand(Command):
|
|||
|
||||
_wrong_emoji = "❌"
|
||||
|
||||
_answer_time = 18
|
||||
_answer_time = 14
|
||||
|
||||
_question_lock: bool = False
|
||||
|
||||
def __init__(self, interface: CommandInterface):
|
||||
super().__init__(interface)
|
||||
self._answerers: typing.Dict[uuid.UUID, typing.Dict[..., bool]] = {}
|
||||
self._answerers: typing.Dict[uuid.UUID, typing.Dict[str, bool]] = {}
|
||||
|
||||
async def run(self, args: CommandArgs, data: CommandData) -> None:
|
||||
if self._question_lock:
|
||||
raise CommandError("C'è già un'altra domanda attiva!")
|
||||
self._question_lock = True
|
||||
await data.delete_invoking()
|
||||
# Fetch the question
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get("https://opentdb.com/api.php?amount=1") as response:
|
||||
|
@ -70,17 +76,17 @@ class TriviaCommand(Command):
|
|||
async def correct(data: CommandData):
|
||||
answerer_ = await data.get_author(error_if_none=True)
|
||||
try:
|
||||
self._answerers[question_id][answerer_] = True
|
||||
self._answerers[question_id][answerer_.uid] = True
|
||||
except KeyError:
|
||||
raise KeyboardExpiredError("Question time ran out.")
|
||||
raise KeyboardExpiredError("Tempo scaduto!")
|
||||
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
||||
|
||||
async def wrong(data: CommandData):
|
||||
answerer_ = await data.get_author(error_if_none=True)
|
||||
try:
|
||||
self._answerers[question_id][answerer_] = False
|
||||
self._answerers[question_id][answerer_.uid] = False
|
||||
except KeyError:
|
||||
raise KeyboardExpiredError("Question time ran out.")
|
||||
raise KeyboardExpiredError("Tempo scaduto!")
|
||||
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
||||
|
||||
# Add question
|
||||
|
@ -94,12 +100,13 @@ class TriviaCommand(Command):
|
|||
await asyncio.sleep(self._answer_time)
|
||||
results = f"❗️ Tempo scaduto!\n" \
|
||||
f"La risposta corretta era [b]{answers[correct_index]}[/b]!\n\n"
|
||||
for answerer in self._answerers[question_id]:
|
||||
for answerer_id in self._answerers[question_id]:
|
||||
answerer = data.session.query(self.alchemy.User).get(answerer_id)
|
||||
if answerer.trivia_score is None:
|
||||
ts = self.interface.alchemy.TriviaScore(royal=answerer)
|
||||
data.session.add(ts)
|
||||
data.session.commit()
|
||||
if self._answerers[question_id][answerer]:
|
||||
if self._answerers[question_id][answerer_id]:
|
||||
results += self._correct_emoji
|
||||
answerer.trivia_score.correct_answers += 1
|
||||
else:
|
||||
|
@ -109,3 +116,4 @@ class TriviaCommand(Command):
|
|||
await data.reply(results)
|
||||
del self._answerers[question_id]
|
||||
await asyncify(data.session.commit)
|
||||
self._question_lock = False
|
||||
|
|
Loading…
Reference in a new issue