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