|
|
|
|
@@ -1,165 +1,168 @@
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
import re
|
|
|
|
|
import sqlite3
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
from nextcord import Client, Message, TextChannel
|
|
|
|
|
from nextcord import RawReactionActionEvent, Emoji
|
|
|
|
|
from nextcord import utils
|
|
|
|
|
|
|
|
|
|
from . import jokes
|
|
|
|
|
from .reactions import ReactionData
|
|
|
|
|
|
|
|
|
|
LIL_STINKY_ID = 704043422276780072
|
|
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Kwaylon(Client):
|
|
|
|
|
def __init__(self, limit: int = 5000, days: int = 30, db_path: Path = None, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
if db_path is None:
|
|
|
|
|
self.db_path = Path(__file__).parents[2] / 'data' / 'messages.db'
|
|
|
|
|
else:
|
|
|
|
|
self.db_path = db_path
|
|
|
|
|
|
|
|
|
|
self.limit, self.days = limit, days
|
|
|
|
|
self.jokes = list(jokes.collect_jokes())
|
|
|
|
|
self.lock = asyncio.Lock()
|
|
|
|
|
|
|
|
|
|
self.most_regex = re.compile('^most\s*(?P<emoji>\S+)', re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
def text_channels(self) -> List[TextChannel]:
|
|
|
|
|
return [chan for chan in self.get_all_channels() if isinstance(chan, TextChannel)]
|
|
|
|
|
|
|
|
|
|
def robotics_facility(self) -> TextChannel:
|
|
|
|
|
for chan in self.text_channels():
|
|
|
|
|
if chan.name == 'robotics-facility' and chan.guild.name == 'Family Dinner':
|
|
|
|
|
return chan
|
|
|
|
|
|
|
|
|
|
def kaylon_emoji(self) -> Emoji:
|
|
|
|
|
return utils.get(self.emojis, name='kaylon')
|
|
|
|
|
|
|
|
|
|
async def handle_ready(self):
|
|
|
|
|
async def alive():
|
|
|
|
|
channel: TextChannel = self.robotics_facility()
|
|
|
|
|
await channel.send('https://tenor.com/view/terminator-im-back-gif-19144173')
|
|
|
|
|
await channel.send(self.kaylon_emoji())
|
|
|
|
|
|
|
|
|
|
# await alive()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.data = ReactionData(self.db_path)
|
|
|
|
|
LOGGER.info(f'{self.data.row_count():d} reactions in {self.db_path}')
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
LOGGER.exception(e)
|
|
|
|
|
LOGGER.error(f'self.db_path: {self.db_path}')
|
|
|
|
|
|
|
|
|
|
async def handle_message(self, message: Message):
|
|
|
|
|
if message.author != self.user:
|
|
|
|
|
await self.read_command(message)
|
|
|
|
|
await self.respond_to_joke(message)
|
|
|
|
|
await self.respond_to_emoji(message)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
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}')
|
|
|
|
|
|
|
|
|
|
async with message.channel.typing():
|
|
|
|
|
with self.data.connect() as con:
|
|
|
|
|
days = get_days(message.content) or 14
|
|
|
|
|
|
|
|
|
|
if days >= 1000:
|
|
|
|
|
await message.reply(
|
|
|
|
|
f'https://tenor.com/view/i-hate-you-anakin-darth-vader-vader-star-wars-gif-13071041'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
df = self.data.read_emoji(emoji_name, con=con, days=days)
|
|
|
|
|
con.close()
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
if len(message.mentions) > 0:
|
|
|
|
|
df = df[df['auth_id'].isin([m.id for m in message.mentions])]
|
|
|
|
|
|
|
|
|
|
most = df.sort_values('count').iloc[-1]
|
|
|
|
|
msg = await self.fetch_message(most)
|
|
|
|
|
await message.reply(f'Most {emoji_ref} in past {days} days\n{msg.jump_url}')
|
|
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
counts = df['count'].sort_values(ascending=False)
|
|
|
|
|
counts.index = [(await self.fetch_user(idx)).display_name for idx in counts.index]
|
|
|
|
|
|
|
|
|
|
width = max([len(str(s)) for s in counts.index])
|
|
|
|
|
|
|
|
|
|
res = '\n'.join(
|
|
|
|
|
f"`{str(name).ljust(width + 1)}with {cnt:<2.0f} total`"
|
|
|
|
|
for name, cnt in counts.iteritems()
|
|
|
|
|
)
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
async def handle_raw_reaction(self, payload: RawReactionActionEvent):
|
|
|
|
|
LOGGER.info(payload)
|
|
|
|
|
|
|
|
|
|
guild = await self.fetch_guild(payload.guild_id)
|
|
|
|
|
channel = await guild.fetch_channel(payload.channel_id)
|
|
|
|
|
message = await channel.fetch_message(payload.message_id)
|
|
|
|
|
|
|
|
|
|
async with self.lock:
|
|
|
|
|
with self.data.connect() as con:
|
|
|
|
|
self.data.add_reactions_from_message(message, con)
|
|
|
|
|
con.close()
|
|
|
|
|
|
|
|
|
|
async def fetch_message(self, row: pd.Series):
|
|
|
|
|
guild = await self.fetch_guild(row['guild_id'])
|
|
|
|
|
channel = await guild.fetch_channel(row['channel_id'])
|
|
|
|
|
return await channel.fetch_message(row['msg_id'])
|
|
|
|
|
|
|
|
|
|
async def scan_messages(self, **kwargs):
|
|
|
|
|
async with self.lock:
|
|
|
|
|
await self.data.scan_messages(client=self, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_emoji_name(string: str) -> str:
|
|
|
|
|
if (m := re.search('<:(?P<name>\w+):(?P<id>\d+)>', string)):
|
|
|
|
|
string = m.group('name')
|
|
|
|
|
return string.lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_days(input_str):
|
|
|
|
|
if (m := re.search('(?P<days>\d+) days', input_str)):
|
|
|
|
|
return int(m.group('days'))
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
import re
|
|
|
|
|
import sqlite3
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
# from nextcord import Client, Message, TextChannel
|
|
|
|
|
# from nextcord import RawReactionActionEvent, Emoji
|
|
|
|
|
# from nextcord import utils
|
|
|
|
|
|
|
|
|
|
from discord import Client, Message, TextChannel
|
|
|
|
|
from discord import RawReactionActionEvent, Emoji
|
|
|
|
|
from discord import utils
|
|
|
|
|
|
|
|
|
|
from . import jokes
|
|
|
|
|
from .reactions import ReactionData
|
|
|
|
|
|
|
|
|
|
LIL_STINKY_ID = 704043422276780072
|
|
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Kwaylon(Client):
|
|
|
|
|
def __init__(self, limit: int = 5000, days: int = 30, db_path: Path = None, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
if db_path is None:
|
|
|
|
|
self.db_path = Path(__file__).parents[2] / 'data' / 'messages.db'
|
|
|
|
|
else:
|
|
|
|
|
self.db_path = db_path
|
|
|
|
|
|
|
|
|
|
self.limit, self.days = limit, days
|
|
|
|
|
self.jokes = list(jokes.collect_jokes())
|
|
|
|
|
self.lock = asyncio.Lock()
|
|
|
|
|
|
|
|
|
|
self.most_regex = re.compile('^most\s*(?P<emoji>\S+)', re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
def text_channels(self) -> List[TextChannel]:
|
|
|
|
|
return [chan for chan in self.get_all_channels() if isinstance(chan, TextChannel)]
|
|
|
|
|
|
|
|
|
|
def robotics_facility(self) -> TextChannel:
|
|
|
|
|
for chan in self.text_channels():
|
|
|
|
|
if chan.name == 'robotics-facility' and chan.guild.name == 'Family Dinner':
|
|
|
|
|
return chan
|
|
|
|
|
|
|
|
|
|
def kaylon_emoji(self) -> Emoji:
|
|
|
|
|
return utils.get(self.emojis, name='kaylon')
|
|
|
|
|
|
|
|
|
|
async def handle_ready(self):
|
|
|
|
|
async def alive():
|
|
|
|
|
channel: TextChannel = self.robotics_facility()
|
|
|
|
|
await channel.send('https://tenor.com/view/terminator-im-back-gif-19144173')
|
|
|
|
|
await channel.send(self.kaylon_emoji())
|
|
|
|
|
|
|
|
|
|
# await alive()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.data = ReactionData(self.db_path)
|
|
|
|
|
LOGGER.info(f'{self.data.row_count():d} reactions in {self.db_path}')
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
LOGGER.exception(e)
|
|
|
|
|
LOGGER.error(f'self.db_path: {self.db_path}')
|
|
|
|
|
|
|
|
|
|
async def handle_message(self, message: Message):
|
|
|
|
|
if message.author != self.user:
|
|
|
|
|
await self.read_command(message)
|
|
|
|
|
await self.respond_to_joke(message)
|
|
|
|
|
await self.respond_to_emoji(message)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
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}')
|
|
|
|
|
|
|
|
|
|
async with message.channel.typing():
|
|
|
|
|
with self.data.connect() as con:
|
|
|
|
|
days = get_days(message.content) or 14
|
|
|
|
|
|
|
|
|
|
if days >= 1000:
|
|
|
|
|
await message.reply(
|
|
|
|
|
f'https://tenor.com/view/i-hate-you-anakin-darth-vader-vader-star-wars-gif-13071041'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
df = self.data.read_emoji(emoji_name, con=con, days=days)
|
|
|
|
|
con.close()
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
if len(message.mentions) > 0:
|
|
|
|
|
df = df[df['auth_id'].isin([m.id for m in message.mentions])]
|
|
|
|
|
|
|
|
|
|
most = df.sort_values('count').iloc[-1]
|
|
|
|
|
msg = await self.fetch_message(most)
|
|
|
|
|
await message.reply(f'Most {emoji_ref} in past {days} days\n{msg.jump_url}')
|
|
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
counts = df['count'].sort_values(ascending=False)
|
|
|
|
|
counts.index = [(await self.fetch_user(idx)).display_name for idx in counts.index]
|
|
|
|
|
|
|
|
|
|
width = max([len(str(s)) for s in counts.index])
|
|
|
|
|
|
|
|
|
|
res = '\n'.join(
|
|
|
|
|
f"`{str(name).ljust(width + 1)}with {cnt:<2.0f} total`"
|
|
|
|
|
for name, cnt in counts.iteritems()
|
|
|
|
|
)
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
async def handle_raw_reaction(self, payload: RawReactionActionEvent):
|
|
|
|
|
LOGGER.info(payload)
|
|
|
|
|
|
|
|
|
|
guild = await self.fetch_guild(payload.guild_id)
|
|
|
|
|
channel = await guild.fetch_channel(payload.channel_id)
|
|
|
|
|
message = await channel.fetch_message(payload.message_id)
|
|
|
|
|
|
|
|
|
|
async with self.lock:
|
|
|
|
|
with self.data.connect() as con:
|
|
|
|
|
self.data.add_reactions_from_message(message, con)
|
|
|
|
|
con.close()
|
|
|
|
|
|
|
|
|
|
async def fetch_message(self, row: pd.Series):
|
|
|
|
|
guild = await self.fetch_guild(row['guild_id'])
|
|
|
|
|
channel = await guild.fetch_channel(row['channel_id'])
|
|
|
|
|
return await channel.fetch_message(row['msg_id'])
|
|
|
|
|
|
|
|
|
|
async def scan_messages(self, **kwargs):
|
|
|
|
|
async with self.lock:
|
|
|
|
|
await self.data.scan_messages(client=self, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_emoji_name(string: str) -> str:
|
|
|
|
|
if (m := re.search('<:(?P<name>\w+):(?P<id>\d+)>', string)):
|
|
|
|
|
string = m.group('name')
|
|
|
|
|
return string.lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_days(input_str):
|
|
|
|
|
if (m := re.search('(?P<days>\d+) days', input_str)):
|
|
|
|
|
return int(m.group('days'))
|
|
|
|
|
|