big rework of emoji calculations, much simpler and more efficient now

This commit is contained in:
2021-08-13 19:31:22 -05:00
parent 8ef4de693a
commit 108a51b9fe
3 changed files with 92 additions and 91 deletions

View File

@@ -3,7 +3,6 @@ import logging
import os
import re
from pathlib import Path
from threading import Lock
import discord
from dotenv import load_dotenv
@@ -26,7 +25,6 @@ class RoboPage(discord.Client):
attrs = filter(lambda n: n.endswith('Joke') and not n.startswith('Joke'), dir(jokes))
attrs = map(lambda n: getattr(jokes, n)(), attrs)
self.jokes = list(attrs)
self.lock = Lock()
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)
@@ -56,35 +54,40 @@ class RoboPage(discord.Client):
if hasattr(self, 'data'):
await self.data.add_msg(message)
if message.author != self.user:
if (m := self.leaderboard_regex.match(message.content)) is not None:
try:
await message.reply(self.data.emoji_leaderboard(emoji_name=m.group('emoji').lower(), days=14))
except KeyError as e:
LOGGER.exception(e)
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
async with self.data.lock:
if message.author != self.user:
if (m := self.leaderboard_regex.match(message.content)) is not None:
try:
await message.reply(await self.data.emoji_leaderboard(
client=self,
emoji_name=m.group('emoji').lower(),
days=14
))
except KeyError as e:
LOGGER.exception(e)
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
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').lower(), days=int(days))
)
except IndexError as e:
await message.reply('NObody')
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').lower(), days=int(days))
)
except IndexError as e:
await message.reply('NObody')
elif 'not like this' in message.content.lower():
await message.reply(self.gifs['not like this'])
elif 'not like this' in message.content.lower():
await message.reply(self.gifs['not like this'])
elif 'beans' in message.content.lower():
await message.reply('Somebody help! I\'ve got beans in my motherboard!\n')
await message.channel.send(self.gifs['beans'])
elif 'beans' in message.content.lower():
await message.reply('Somebody help! I\'ve got beans in my motherboard!\n')
await message.channel.send(self.gifs['beans'])
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)
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)
if __name__ == '__main__':