From fb0a4cec6c1e998622b45a605d22efbb7553024b Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 3 May 2021 00:34:25 -0400 Subject: [PATCH] Use command.Bot for better commands/automatic help --- ahtcg_bot.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/ahtcg_bot.py b/ahtcg_bot.py index b762b5e..902072c 100755 --- a/ahtcg_bot.py +++ b/ahtcg_bot.py @@ -5,7 +5,7 @@ import json import re import discord -from discord.ext import tasks +from discord.ext import commands, tasks from arkhamdb import ArkhamDBClient from secret import TOKEN @@ -16,17 +16,32 @@ with open('channel_list.json') as f: channel_list = json.load(f) -class ArkhamDBUpdater(discord.Client): +class ArkhamDBUpdater(commands.Bot): arkhamdb_client: ArkhamDBClient + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + + 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) + channel_list.append(ctx.message.channel.id) + with open('channel_list.json', 'w') as f: + json.dump(channel_list, f) + 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): - print(' ', guild.name) + print(' -', guild.name) print('------') - self.arkhamdb_client = ArkhamDBClient() self.arkhamdb_monitor.start() async def gather_deck_ids(self, channel: discord.TextChannel) -> dict[int, str]: @@ -74,16 +89,12 @@ class ArkhamDBUpdater(discord.Client): await channel.send(embed=message_embed) async def on_message(self, message: discord.Message): + await self.process_commands(message) + # we do not want the bot to reply to itself if message.author.id == self.user.id: return - if message.content.startswith('!arkhamdb_monitor'): - await message.reply('Hello!', mention_author=True) - channel_list.append(message.channel.id) - with open('channel_list.json', 'w') as f: - json.dump(channel_list, f) - if message.channel.id in channel_list: await self.update_channel_latest_decks(message.channel) @@ -102,5 +113,5 @@ class ArkhamDBUpdater(discord.Client): await self.update_channel_latest_decks(channel) -client = ArkhamDBUpdater() -client.run(TOKEN) +bot = ArkhamDBUpdater(command_prefix='!arkhamdb ') +bot.run(TOKEN, reconnect=True)