[Trivia] Handle potential Discord errors in session.

This commit is contained in:
PredaaA
2021-07-03 21:58:56 +02:00
parent 9d35df2048
commit d90e45f9e0

View File

@@ -1,5 +1,6 @@
"""Module to manage trivia sessions.""" """Module to manage trivia sessions."""
import asyncio import asyncio
import contextlib
import time import time
import random import random
from collections import Counter from collections import Counter
@@ -207,7 +208,8 @@ class TriviaSession:
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
if time.time() - self._last_response >= timeout: if time.time() - self._last_response >= timeout:
await self.ctx.send(_("Guys...? Well, I guess I'll stop then.")) with contextlib.suppress(discord.HTTPException):
await self.ctx.send(_("Guys...? Well, I guess I'll stop then."))
self.stop() self.stop()
return False return False
if self.settings["reveal_answer"]: if self.settings["reveal_answer"]:
@@ -220,11 +222,19 @@ class TriviaSession:
if self.settings["bot_plays"]: if self.settings["bot_plays"]:
reply += _(" **+1** for me!") reply += _(" **+1** for me!")
self.scores[self.ctx.guild.me] += 1 self.scores[self.ctx.guild.me] += 1
await self.ctx.send(reply) try:
await self.ctx.send(reply)
except (discord.NotFound, discord.Forbidden):
self.stop()
return False
else: else:
self.scores[message.author] += 1 self.scores[message.author] += 1
reply = _("You got it {user}! **+1** to you!").format(user=message.author.display_name) reply = _("You got it {user}! **+1** to you!").format(user=message.author.display_name)
await self.ctx.send(reply) try:
await self.ctx.send(reply)
except (discord.NotFound, discord.Forbidden):
self.stop()
return False
return True return True
def check_answer(self, answers): def check_answer(self, answers):