2021-05-02 15:30:17 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
import discord
|
2021-05-03 00:34:25 -04:00
|
|
|
from discord.ext import commands, tasks
|
2021-05-02 15:30:17 -04:00
|
|
|
|
2021-05-02 23:22:52 -04:00
|
|
|
from arkhamdb import ArkhamDBClient
|
2021-05-02 15:30:17 -04:00
|
|
|
from secret import TOKEN
|
|
|
|
|
|
|
|
|
2021-05-03 00:34:25 -04:00
|
|
|
class ArkhamDBUpdater(commands.Bot):
|
2021-05-03 00:35:30 -04:00
|
|
|
channel_list: set[int]
|
2021-05-02 23:22:52 -04:00
|
|
|
arkhamdb_client: ArkhamDBClient
|
2021-05-02 15:30:17 -04:00
|
|
|
|
2021-05-03 00:34:25 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2021-05-03 00:35:30 -04:00
|
|
|
# TODO: should really be a database
|
|
|
|
with open('channel_list.json') as f:
|
|
|
|
self.channel_list = set(json.load(f))
|
2021-05-03 00:34:25 -04:00
|
|
|
|
|
|
|
self.arkhamdb_client = ArkhamDBClient()
|
|
|
|
self.setup_commands()
|
|
|
|
|
|
|
|
def setup_commands(self):
|
|
|
|
@self.command(name='monitor')
|
|
|
|
async def monitor(ctx: commands.Context):
|
|
|
|
"""Watch this channel for deck links and link to latest versions."""
|
|
|
|
await ctx.message.reply('Now monitoring this channel for deck IDs', mention_author=True)
|
2021-05-03 00:35:30 -04:00
|
|
|
self.channel_list.add(ctx.message.channel.id)
|
2021-05-03 00:34:25 -04:00
|
|
|
with open('channel_list.json', 'w') as f:
|
2021-05-03 00:35:30 -04:00
|
|
|
json.dump(list(self.channel_list), f)
|
2021-05-03 00:34:25 -04:00
|
|
|
|
2021-05-02 15:30:17 -04:00
|
|
|
async def on_ready(self):
|
|
|
|
print(f'Logged in as {self.user} (ID: {self.user.id})')
|
|
|
|
print('Enabled on servers:')
|
|
|
|
async for guild in self.fetch_guilds(limit=150):
|
2021-05-03 00:34:25 -04:00
|
|
|
print(' -', guild.name)
|
2021-05-02 15:30:17 -04:00
|
|
|
print('------')
|
|
|
|
|
|
|
|
self.arkhamdb_monitor.start()
|
|
|
|
|
|
|
|
async def gather_deck_ids(self, channel: discord.TextChannel) -> dict[int, str]:
|
|
|
|
deck_ids: dict[int, str] = {}
|
2021-05-02 23:26:36 -04:00
|
|
|
url_regex = re.compile(
|
|
|
|
r'(?P<prefix>.*)' + self.arkhamdb_client.origin + r'/deck/view/(?P<deck_id>\d+)')
|
2021-05-02 15:30:17 -04:00
|
|
|
async for message in channel.history(limit=200, oldest_first=True):
|
|
|
|
# ignore the bot's messages
|
|
|
|
if message.author.id == self.user.id:
|
|
|
|
continue
|
|
|
|
|
2021-05-02 23:26:36 -04:00
|
|
|
matches = url_regex.finditer(message.content)
|
2021-05-02 15:30:17 -04:00
|
|
|
for match in matches:
|
|
|
|
deck_ids[int(match.group('deck_id'))] = match.group('prefix')
|
|
|
|
|
|
|
|
return deck_ids
|
|
|
|
|
2021-05-02 22:44:34 -04:00
|
|
|
async def clear_old_messages(self, channel: discord.TextChannel) -> None:
|
|
|
|
async for message in channel.history(limit=200):
|
|
|
|
if message.author.id == self.user.id and message.id != channel.last_message_id:
|
|
|
|
await message.delete()
|
|
|
|
|
2021-05-02 15:30:17 -04:00
|
|
|
async def update_channel_latest_decks(self, channel: discord.TextChannel) -> None:
|
|
|
|
deck_ids = await self.gather_deck_ids(channel)
|
2021-05-02 23:22:52 -04:00
|
|
|
latest_decks = await self.arkhamdb_client.get_latest_decks(deck_ids)
|
2021-05-02 15:30:17 -04:00
|
|
|
|
2021-05-02 22:44:34 -04:00
|
|
|
await self.clear_old_messages(channel)
|
2021-05-02 15:30:17 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
last_message = await channel.fetch_message(channel.last_message_id)
|
|
|
|
except discord.NotFound:
|
|
|
|
last_message = None
|
|
|
|
|
|
|
|
message_text = '\n'.join(
|
2021-05-02 23:22:52 -04:00
|
|
|
f"{prefix}[{deck['name']}]({self.arkhamdb_client.origin}/deck/view/{deck['id']}) [{deck['id']}]"
|
2021-05-02 15:30:17 -04:00
|
|
|
for prefix, deck in latest_decks.values())
|
|
|
|
message_embed = discord.Embed(
|
|
|
|
title=f'Updated as of {datetime.now()}',
|
|
|
|
description=message_text)
|
|
|
|
|
|
|
|
if last_message is not None and last_message.author.id == self.user.id:
|
2021-05-02 22:44:34 -04:00
|
|
|
if len(last_message.embeds) == 1 and message_text != last_message.embeds[0].description:
|
2021-05-02 15:30:17 -04:00
|
|
|
await last_message.edit(embed=message_embed)
|
|
|
|
else:
|
|
|
|
await channel.send(embed=message_embed)
|
|
|
|
|
|
|
|
async def on_message(self, message: discord.Message):
|
2021-05-03 00:34:25 -04:00
|
|
|
await self.process_commands(message)
|
|
|
|
|
2021-05-02 15:30:17 -04:00
|
|
|
# we do not want the bot to reply to itself
|
|
|
|
if message.author.id == self.user.id:
|
|
|
|
return
|
|
|
|
|
2021-05-03 00:35:30 -04:00
|
|
|
if message.channel.id in self.channel_list:
|
2021-05-02 15:30:17 -04:00
|
|
|
await self.update_channel_latest_decks(message.channel)
|
|
|
|
|
|
|
|
async def on_message_edit(self, before: discord.Message, after: discord.Message):
|
|
|
|
# we do not want the bot to reply to itself
|
|
|
|
if after.author.id == self.user.id:
|
|
|
|
return
|
|
|
|
|
2021-05-03 00:35:30 -04:00
|
|
|
if after.channel.id in self.channel_list:
|
2021-05-02 15:30:17 -04:00
|
|
|
await self.update_channel_latest_decks(after.channel)
|
|
|
|
|
|
|
|
@tasks.loop(seconds=20)
|
|
|
|
async def arkhamdb_monitor(self) -> None:
|
2021-05-03 00:35:30 -04:00
|
|
|
for channel_id in self.channel_list:
|
2021-05-02 15:30:17 -04:00
|
|
|
channel = self.get_channel(channel_id)
|
|
|
|
await self.update_channel_latest_decks(channel)
|
|
|
|
|
|
|
|
|
2021-05-03 00:34:25 -04:00
|
|
|
bot = ArkhamDBUpdater(command_prefix='!arkhamdb ')
|
|
|
|
bot.run(TOKEN, reconnect=True)
|