split up handling messages into multiple sub-functions

This commit is contained in:
jsl12
2022-01-28 00:23:46 -06:00
parent 63c4edc088
commit 7a6ccf1721

View File

@@ -59,50 +59,54 @@ class Kwaylon(Client):
async def handle_message(self, message: Message): async def handle_message(self, message: Message):
if message.author != self.user: if message.author != self.user:
for mention in message.mentions: await self.read_command(message)
if mention.id == self.user.id and 'read' in message.content: await self.respond_to_joke(message)
if (m := re.search('(\d+) days', message.content)): await self.respond_to_emoji(message)
days = int(m.group(1))
else:
days = self.days
await self.data.scan_messages(client=self, limit=self.limit, days=days) async def read_command(self, message: Message):
return for mention in message.mentions:
if mention.id == self.user.id and 'read' in message.content:
days = get_days(message.content) or self.days
await self.data.scan_messages(client=self, limit=self.limit, days=days)
if (most_match := self.most_regex.match(message.content)): async def respond_to_emoji(self, message: Message):
emoji_ref = most_match.group('emoji') if (most_match := self.most_regex.match(message.content)):
emoji_name = get_emoji_name(emoji_ref) emoji_ref = most_match.group('emoji')
LOGGER.info(f'Most {emoji_name}') emoji_name = get_emoji_name(emoji_ref)
LOGGER.info(f'Most {emoji_name}')
with self.data.connect() as con: with self.data.connect() as con:
df = self.data.read_emoji(emoji_name, con) df = self.data.read_emoji(emoji_name, con)
con.close() con.close()
days = get_days(message.content) or 14 days = get_days(message.content) or 14
df = filter_days(df, days) df = filter_days(df, days)
if df.shape[0] > 0: if df.shape[0] > 0:
LOGGER.info(f'{df.shape[0]} messages with {emoji_ref} after filtering') LOGGER.info(f'{df.shape[0]} messages with {emoji_ref} after filtering')
if 'leaderboard' in message.content:
LOGGER.info(f'Building leaderboard')
res = f'{emoji_ref} totals, past {days} days\n'
if (board := await self.leaderboard(df)) is not None:
res += board
await message.reply(res)
if 'leaderboard' in message.content:
LOGGER.info(f'Building leaderboard')
res = f'{emoji_ref} totals, past {days} days\n'
if (board := await self.leaderboard(df)) is not None:
res += board
await message.reply(res)
else:
most = df.sort_values('count').iloc[-1]
msg = await self.fetch_message(most)
await message.reply(f'{msg.jump_url}')
else: else:
await message.reply(f"NObody (in the past {days} days)...gah, leave me alone!") most = df.sort_values('count').iloc[-1]
LOGGER.info(f'Done') msg = await self.fetch_message(most)
return await message.reply(f'{msg.jump_url}')
for joke in self.jokes: else:
if (joke_match := joke.scan(message)) is not None: await message.reply(f"NObody (in the past {days} days)...gah, leave me alone!")
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {m.group()}')
await joke.respond(message, self, joke_match) LOGGER.info(f'Done')
async def respond_to_joke(self, message: Message):
for joke in self.jokes:
if (joke_match := joke.scan(message)):
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {joke_match.group()}')
await joke.respond(message, self, joke_match)
async def leaderboard(self, df: pd.DataFrame) -> str: async def leaderboard(self, df: pd.DataFrame) -> str:
df = df.groupby('auth_id').sum() df = df.groupby('auth_id').sum()