various minor tweaks
This commit is contained in:
@@ -93,8 +93,7 @@ class MsgData:
|
|||||||
if len(msg.reactions) > 0:
|
if len(msg.reactions) > 0:
|
||||||
new = reaction_df(msg)
|
new = reaction_df(msg)
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
# self.reactions = self.reactions.join(new, how='outer')
|
self.reactions = self.reactions.append(new)
|
||||||
self.reactions = pd.concat([self.reactions, new])
|
|
||||||
LOGGER.info(str(new.droplevel(level=0, axis=0).loc[:, 'count']))
|
LOGGER.info(str(new.droplevel(level=0, axis=0).loc[:, 'count']))
|
||||||
|
|
||||||
if msg.id not in self.msgs.index:
|
if msg.id not in self.msgs.index:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from .base import Joke, GifJoke
|
|||||||
def collect_jokes():
|
def collect_jokes():
|
||||||
for name in dir(jokes):
|
for name in dir(jokes):
|
||||||
try:
|
try:
|
||||||
if issubclass((j := getattr(jokes, name)), Joke):
|
if issubclass((joke := getattr(jokes, name)), Joke):
|
||||||
yield j
|
yield joke()
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -1,25 +1,24 @@
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from typing import Union
|
from pathlib import Path
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from discord import RawReactionActionEvent, RawReactionClearEmojiEvent
|
from discord import RawReactionActionEvent
|
||||||
|
|
||||||
from . import data, jokes
|
from . import data, jokes
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
LIL_STINKY_ID = 704043422276780072
|
LIL_STINKY_ID = 704043422276780072
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Kwaylon(discord.Client):
|
class Kwaylon(discord.Client):
|
||||||
db_path: str = '../messages.db'
|
db_path: Path = Path('../messages.db')
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, limit: int = 5000, days: int = 30, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
self.limit, self.days = limit, days
|
||||||
self.jokes = list(jokes.collect_jokes())
|
self.jokes = list(jokes.collect_jokes())
|
||||||
|
|
||||||
self.most_regex = re.compile(
|
self.most_regex = re.compile(
|
||||||
@@ -36,13 +35,7 @@ class Kwaylon(discord.Client):
|
|||||||
channel: discord.TextChannel = discord.utils.get(self.get_all_channels(), name='robotics-facility')
|
channel: discord.TextChannel = discord.utils.get(self.get_all_channels(), name='robotics-facility')
|
||||||
await channel.send(f"I'm aliiiiiive {discord.utils.get(self.emojis, name='kaylon')}")
|
await channel.send(f"I'm aliiiiiive {discord.utils.get(self.emojis, name='kaylon')}")
|
||||||
|
|
||||||
self.data: data.MsgData = await data.MsgData.create(
|
self.data: data.MsgData = await data.MsgData.create(client=self, limit=self.limit, days=self.days)
|
||||||
client=self,
|
|
||||||
limit=5000,
|
|
||||||
days=30,
|
|
||||||
# limit=100,
|
|
||||||
# days=14,
|
|
||||||
)
|
|
||||||
self.data.to_sql(self.db_path)
|
self.data.to_sql(self.db_path)
|
||||||
LOGGER.info(f'{self.data.msgs.shape[0]} messages total')
|
LOGGER.info(f'{self.data.msgs.shape[0]} messages total')
|
||||||
# await alive()
|
# await alive()
|
||||||
@@ -74,7 +67,7 @@ class Kwaylon(discord.Client):
|
|||||||
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {m.group()}')
|
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {m.group()}')
|
||||||
await joke.respond(message, self, m)
|
await joke.respond(message, self, m)
|
||||||
|
|
||||||
async def handle_raw_reaction(self, payload: Union[RawReactionActionEvent, RawReactionClearEmojiEvent]):
|
async def handle_raw_reaction(self, payload: RawReactionActionEvent):
|
||||||
LOGGER.info(payload)
|
LOGGER.info(payload)
|
||||||
guild = await self.fetch_guild(payload.guild_id)
|
guild = await self.fetch_guild(payload.guild_id)
|
||||||
channel = await guild.fetch_channel(payload.channel_id)
|
channel = await guild.fetch_channel(payload.channel_id)
|
||||||
@@ -116,14 +109,14 @@ class Kwaylon(discord.Client):
|
|||||||
msg += '\n' + await self.worst_offsenses(user=user, days=days, top=3, emoji_str=match.group('emoji'))
|
msg += '\n' + await self.worst_offsenses(user=user, days=days, top=3, emoji_str=match.group('emoji'))
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
async def worst_offsenses(self, user: discord.User, days: int, top: int, emoji_str: str) -> str:
|
async def worst_offsenses(self, user: discord.User, emoji_str: str, days: int = None, top: int = 3) -> str:
|
||||||
cdf = self.data.emoji_messages(get_emoji_name(emoji_str), days=days)
|
df: pd.DataFrame = self.data.emoji_messages(get_emoji_name(emoji_str), days=days)
|
||||||
cdf = cdf[cdf['user id'] == user.id].sort_values('count', ascending=False).iloc[:top]
|
df: pd.DataFrame = df[df['user id'] == user.id].sort_values('count', ascending=False).iloc[:top]
|
||||||
|
|
||||||
if cdf.shape[0] > 0:
|
if df.shape[0] > 0:
|
||||||
res = f'Top {top} {emoji_str}\n'
|
res = f'Top {top} {emoji_str}\n'
|
||||||
res += f'\n'.join(
|
res += f'\n'.join(
|
||||||
f'{emoji_str}x{row["count"]:.0f}\n{row["link"]}' for idx, row in cdf.iterrows())
|
f'{emoji_str}x{row["count"]:.0f}\n{row["link"]}' for idx, row in df.iterrows())
|
||||||
else:
|
else:
|
||||||
res = f'No {emoji_str} for {user} in the past {days} days'
|
res = f'No {emoji_str} for {user} in the past {days} days'
|
||||||
|
|
||||||
|
|||||||
4
main.py
4
main.py
@@ -6,6 +6,10 @@ from dotenv import load_dotenv
|
|||||||
from kwaylon import Kwaylon
|
from kwaylon import Kwaylon
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
# https://discordpy.readthedocs.io/en/stable/quickstart.html
|
# https://discordpy.readthedocs.io/en/stable/quickstart.html
|
||||||
client = Kwaylon()
|
client = Kwaylon()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user