improved reaction handling, added some comments
This commit is contained in:
55
robopage.py
55
robopage.py
@@ -3,8 +3,9 @@ import logging
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from typing import Union
|
||||
import discord
|
||||
from discord import RawReactionActionEvent, RawReactionClearEmojiEvent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import data
|
||||
@@ -47,9 +48,9 @@ class RoboPage(discord.Client):
|
||||
|
||||
self.data: data.MsgData = await data.MsgData.create(
|
||||
client=self,
|
||||
limit=5000,
|
||||
# limit=20,
|
||||
days=30,
|
||||
# limit=5000,
|
||||
limit=20,
|
||||
# days=30,
|
||||
)
|
||||
self.data.to_sql('messages.db')
|
||||
LOGGER.info(f'{self.data.msgs.shape[0]} messages total')
|
||||
@@ -61,26 +62,23 @@ class RoboPage(discord.Client):
|
||||
await self.data.add_msg(message)
|
||||
|
||||
if (m := self.leaderboard_regex.match(message.content)) is not None:
|
||||
days = int(m.group('days')) or 14
|
||||
emoji = m.group('emoji').lower()
|
||||
try:
|
||||
await message.reply(await self.data.emoji_leaderboard(
|
||||
client=self,
|
||||
emoji_name=m.group('emoji').lower(),
|
||||
days=14
|
||||
))
|
||||
await message.reply(
|
||||
await self.data.emoji_leaderboard(client=self, emoji_name=emoji, days=days)
|
||||
)
|
||||
except KeyError as e:
|
||||
LOGGER.exception(e)
|
||||
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
|
||||
return
|
||||
|
||||
elif (m := self.most_regex.match(message.content)) is not None:
|
||||
days = m.group('days') or 14
|
||||
days = int(m.group('days')) or 14
|
||||
emoji = m.group('emoji').lower()
|
||||
try:
|
||||
await message.reply(
|
||||
await self.data.biggest_single(
|
||||
client=self,
|
||||
emoji=m.group('emoji').lower(),
|
||||
days=int(days)
|
||||
))
|
||||
await self.data.biggest_single(client=self, emoji=emoji, days=days))
|
||||
except IndexError as e:
|
||||
await message.reply('NObody')
|
||||
return
|
||||
@@ -90,6 +88,21 @@ class RoboPage(discord.Client):
|
||||
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {scan_res.group()}')
|
||||
await joke.respond(message, self, scan_res)
|
||||
|
||||
async def handle_raw_reaction(self, payload: Union[RawReactionActionEvent, RawReactionClearEmojiEvent]):
|
||||
LOGGER.info(payload)
|
||||
guild = await client.fetch_guild(payload.guild_id)
|
||||
channel = await guild.fetch_channel(payload.channel_id)
|
||||
message = await channel.fetch_message(payload.message_id)
|
||||
|
||||
if isinstance(payload, RawReactionActionEvent):
|
||||
LOGGER.info(
|
||||
f'{payload.member.display_name} added {payload.emoji} to\n{message.author.display_name}: {message.content}')
|
||||
elif isinstance(payload, RawReactionClearEmojiEvent):
|
||||
LOGGER.info(f'{payload.emoji} removed from\n{message.author.display_name}: {message.content}')
|
||||
|
||||
if hasattr(self, 'data'):
|
||||
await self.data.update_reaction(msg=message)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_dotenv()
|
||||
@@ -110,17 +123,13 @@ if __name__ == '__main__':
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_raw_reaction_add(payload):
|
||||
LOGGER.info(payload)
|
||||
if hasattr(client, 'data'):
|
||||
await client.data.update_reaction(payload=payload, client=client)
|
||||
async def on_raw_reaction_add(payload: RawReactionActionEvent):
|
||||
await client.handle_raw_reaction(payload)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_raw_reaction_remove(payload):
|
||||
LOGGER.info(payload)
|
||||
if hasattr(client, 'data'):
|
||||
await client.data.update_reaction(payload=payload, client=client)
|
||||
async def on_raw_reaction_remove(payload: RawReactionClearEmojiEvent):
|
||||
await client.handle_raw_reaction(payload)
|
||||
|
||||
|
||||
client.run()
|
||||
|
||||
Reference in New Issue
Block a user