responsive error handling

This commit is contained in:
2021-08-11 17:18:30 -05:00
parent b6d41b6ba5
commit 3a4d0084fe
2 changed files with 15 additions and 12 deletions

View File

@@ -25,8 +25,8 @@ class RoboPage(discord.Client):
attrs = map(lambda n: getattr(jokes, n)(), attrs)
self.jokes = list(attrs)
self.lock = Lock()
self.emoji_regex = re.compile("^who is the most (?P<emoji>\w+)(?: in the past (?P<days>\d+) days)?\??$",
re.IGNORECASE)
self.most_regex = re.compile("^who is the most (?P<emoji>\w+)(?: in the past (?P<days>\d+) days)?\??$",
re.IGNORECASE)
self.leaderboard_regex = re.compile('^most (?P<emoji>\w+) leaderboard$', re.IGNORECASE)
def run(self):
@@ -39,8 +39,8 @@ class RoboPage(discord.Client):
self.data: msg.MsgData = await msg.MsgData.create(
client=self,
# limit=3000,
limit=20,
limit=3000,
# limit=20,
days=14,
)
self.data.to_sql('messages.db')
@@ -51,23 +51,26 @@ class RoboPage(discord.Client):
await self.data.add_msg(message)
if message.author != self.user:
try:
if (m := self.leaderboard_regex.match(message.content)) is not None:
if (m := self.leaderboard_regex.match(message.content)) is not None:
try:
await message.reply(self.data.emoji_leaderboard(emoji_name=m.group('emoji'), days=14))
except KeyError as e:
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
elif (m := self.emoji_regex.match(message.content)) is not None:
days = m.group('days') or 14
elif (m := self.most_regex.match(message.content)) is not None:
days = m.group('days') or 14
try:
await message.reply(
await self.data.biggest_single(client=self, emoji=m.group('emoji'), days=int(days))
)
except IndexError as e:
await message.reply('NObody')
else:
for joke in self.jokes:
if (scan_res := joke.scan(message)):
print(f'{joke.__class__.__name__} detected:\n{message.content}\n{scan_res}')
await joke.respond(message, self, scan_res)
except Exception as e:
# await message.reply('oops')
raise
if __name__ == '__main__':