mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +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
|
return
|
||||||
# Prepare data
|
# Prepare data
|
||||||
data = self._Data(interface=command.interface, update=update)
|
data = self._Data(interface=command.interface, update=update)
|
||||||
# Run the command
|
|
||||||
try:
|
try:
|
||||||
|
# Run the command
|
||||||
await command.run(CommandArgs(parameters), data)
|
await command.run(CommandArgs(parameters), data)
|
||||||
except InvalidInputError as e:
|
except InvalidInputError as e:
|
||||||
await data.reply(f"⚠️ {e.message}\n"
|
await data.reply(f"⚠️ {e.message}\n"
|
||||||
|
@ -178,8 +178,9 @@ class TelegramBot(GenericBot):
|
||||||
error_message = f"🦀 [b]{e.__class__.__name__}[/b] 🦀\n"
|
error_message = f"🦀 [b]{e.__class__.__name__}[/b] 🦀\n"
|
||||||
error_message += '\n'.join(e.args)
|
error_message += '\n'.join(e.args)
|
||||||
await data.reply(error_message)
|
await data.reply(error_message)
|
||||||
# Close the data session
|
finally:
|
||||||
await data.session_close()
|
# Close the data session
|
||||||
|
await data.session_close()
|
||||||
|
|
||||||
async def _handle_callback_query(self, update: telegram.Update):
|
async def _handle_callback_query(self, update: telegram.Update):
|
||||||
query: telegram.CallbackQuery = update.callback_query
|
query: telegram.CallbackQuery = update.callback_query
|
||||||
|
@ -196,10 +197,13 @@ class TelegramBot(GenericBot):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
response = await callback(data=self._Data(interface=command.interface, update=update))
|
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
|
# 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)
|
await self.safe_api_call(source.edit_reply_markup, reply_markup=None)
|
||||||
await self.safe_api_call(query.answer, text="⛔️ This keyboard has expired.")
|
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
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_text = f"⛔️ {e.__class__.__name__}\n"
|
error_text = f"⛔️ {e.__class__.__name__}\n"
|
||||||
|
|
|
@ -24,13 +24,19 @@ class TriviaCommand(Command):
|
||||||
|
|
||||||
_wrong_emoji = "❌"
|
_wrong_emoji = "❌"
|
||||||
|
|
||||||
_answer_time = 18
|
_answer_time = 14
|
||||||
|
|
||||||
|
_question_lock: bool = False
|
||||||
|
|
||||||
def __init__(self, interface: CommandInterface):
|
def __init__(self, interface: CommandInterface):
|
||||||
super().__init__(interface)
|
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:
|
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
|
# Fetch the question
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get("https://opentdb.com/api.php?amount=1") as response:
|
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):
|
async def correct(data: CommandData):
|
||||||
answerer_ = await data.get_author(error_if_none=True)
|
answerer_ = await data.get_author(error_if_none=True)
|
||||||
try:
|
try:
|
||||||
self._answerers[question_id][answerer_] = True
|
self._answerers[question_id][answerer_.uid] = True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeyboardExpiredError("Question time ran out.")
|
raise KeyboardExpiredError("Tempo scaduto!")
|
||||||
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
||||||
|
|
||||||
async def wrong(data: CommandData):
|
async def wrong(data: CommandData):
|
||||||
answerer_ = await data.get_author(error_if_none=True)
|
answerer_ = await data.get_author(error_if_none=True)
|
||||||
try:
|
try:
|
||||||
self._answerers[question_id][answerer_] = False
|
self._answerers[question_id][answerer_.uid] = False
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeyboardExpiredError("Question time ran out.")
|
raise KeyboardExpiredError("Tempo scaduto!")
|
||||||
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
return "🆗 Hai risposto alla domanda. Ora aspetta un attimo per i risultati!"
|
||||||
|
|
||||||
# Add question
|
# Add question
|
||||||
|
@ -94,12 +100,13 @@ class TriviaCommand(Command):
|
||||||
await asyncio.sleep(self._answer_time)
|
await asyncio.sleep(self._answer_time)
|
||||||
results = f"❗️ Tempo scaduto!\n" \
|
results = f"❗️ Tempo scaduto!\n" \
|
||||||
f"La risposta corretta era [b]{answers[correct_index]}[/b]!\n\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:
|
if answerer.trivia_score is None:
|
||||||
ts = self.interface.alchemy.TriviaScore(royal=answerer)
|
ts = self.interface.alchemy.TriviaScore(royal=answerer)
|
||||||
data.session.add(ts)
|
data.session.add(ts)
|
||||||
data.session.commit()
|
data.session.commit()
|
||||||
if self._answerers[question_id][answerer]:
|
if self._answerers[question_id][answerer_id]:
|
||||||
results += self._correct_emoji
|
results += self._correct_emoji
|
||||||
answerer.trivia_score.correct_answers += 1
|
answerer.trivia_score.correct_answers += 1
|
||||||
else:
|
else:
|
||||||
|
@ -109,3 +116,4 @@ class TriviaCommand(Command):
|
||||||
await data.reply(results)
|
await data.reply(results)
|
||||||
del self._answerers[question_id]
|
del self._answerers[question_id]
|
||||||
await asyncify(data.session.commit)
|
await asyncify(data.session.commit)
|
||||||
|
self._question_lock = False
|
||||||
|
|
Loading…
Reference in a new issue