WIP: Initial attempt to make slash commands work

This commit is contained in:
Adam Goldsmith 2022-04-02 11:49:22 -04:00
parent 5d96211700
commit 0adb6d32ff
4 changed files with 39 additions and 24 deletions

View File

@ -1,20 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import configargparse
from datetime import datetime from datetime import datetime
import json import json
import re import re
import logging import logging
from pathlib import Path from pathlib import Path
import configargparse
import discord import discord
from discord.ext import commands, tasks from discord.ext import tasks
from discord import app_commands
from arkhamdb import ArkhamDBClient, ArkhamDBDeck from arkhamdb import ArkhamDBClient, ArkhamDBDeck
from validate_decks import Validator from validate_decks import Validator
class ArkhamDBUpdater(commands.Bot): class ArkhamDBUpdater(discord.Client):
channel_list_file: Path channel_list_file: Path
channel_list: set[int] channel_list: set[int]
arkhamdb_client: ArkhamDBClient arkhamdb_client: ArkhamDBClient
@ -29,33 +31,36 @@ class ArkhamDBUpdater(commands.Bot):
self.channel_list = set(json.load(f)) self.channel_list = set(json.load(f))
self.arkhamdb_client = ArkhamDBClient() self.arkhamdb_client = ArkhamDBClient()
self.setup_commands()
async def close(self) -> None: async def close(self) -> None:
await self.arkhamdb_client.close() await self.arkhamdb_client.close()
await super().close() await super().close()
def setup_commands(self) -> None: async def setup_commands(self) -> None:
@self.command(name="monitor") tree = app_commands.CommandTree(self)
async def monitor(ctx: commands.Context) -> None:
@tree.command()
async def monitor(interaction: discord.Interaction) -> None:
"""Watch this channel for deck links and link to latest versions.""" """Watch this channel for deck links and link to latest versions."""
await ctx.message.reply( await interaction.message.reply(
"Now monitoring this channel for deck IDs", mention_author=True "Now monitoring this channel for deck IDs", mention_author=True
) )
self.channel_list.add(ctx.message.channel.id) self.channel_list.add(interaction.message.channel.id)
with open(self.channel_list_file, "w") as f: with open(self.channel_list_file, "w") as f:
json.dump(list(self.channel_list), f) json.dump(list(self.channel_list), f)
@self.command(name="forget") @tree.command()
async def forget(ctx: commands.Context) -> None: async def forget(interaction: discord.Interaction) -> None:
"""Remove this channel from the monitor list""" """Remove this channel from the monitor list"""
await ctx.message.reply( await interaction.message.reply(
"No longer monitoring this channel for deck IDs", mention_author=True "No longer monitoring this channel for deck IDs", mention_author=True
) )
self.channel_list.discard(ctx.message.channel.id) self.channel_list.discard(interaction.message.channel.id)
with open(self.channel_list_file, "w") as f: with open(self.channel_list_file, "w") as f:
json.dump(list(self.channel_list), f) json.dump(list(self.channel_list), f)
await tree.sync()
async def on_ready(self) -> None: async def on_ready(self) -> None:
logging.info(f"Logged in as {self.user} (ID: {self.user.id})") logging.info(f"Logged in as {self.user} (ID: {self.user.id})")
logging.info("Enabled on servers:") logging.info("Enabled on servers:")
@ -63,7 +68,14 @@ class ArkhamDBUpdater(commands.Bot):
logging.info(f" - {guild.name}") logging.info(f" - {guild.name}")
logging.info("------") logging.info("------")
self.arkhamdb_monitor.start() for channel_id in self.channel_list:
channel = self.get_channel(channel_id)
print(channel)
print(channel.permissions_for(self.user))
await self.setup_commands()
# self.arkhamdb_monitor.start()
async def gather_deck_ids(self, channel: discord.TextChannel) -> dict[int, str]: async def gather_deck_ids(self, channel: discord.TextChannel) -> dict[int, str]:
deck_ids: dict[int, str] = {} deck_ids: dict[int, str] = {}
@ -131,6 +143,7 @@ class ArkhamDBUpdater(commands.Bot):
title=f"Updated as of {datetime.now()}", description=message_text title=f"Updated as of {datetime.now()}", description=message_text
) )
# TODO: caching
cards = await self.arkhamdb_client.get_cards(encounter=True) cards = await self.arkhamdb_client.get_cards(encounter=True)
validator = Validator(cards) validator = Validator(cards)
validation_errors = list( validation_errors = list(
@ -165,7 +178,6 @@ class ArkhamDBUpdater(commands.Bot):
await self.update_channel_latest_decks(message.channel) await self.update_channel_latest_decks(message.channel)
async def on_message(self, message: discord.Message) -> None: async def on_message(self, message: discord.Message) -> None:
await self.process_commands(message)
await self.maybe_update_channel_for_message(message) await self.maybe_update_channel_for_message(message)
async def on_message_edit( async def on_message_edit(
@ -222,8 +234,12 @@ def main():
logging.basicConfig(level=args.loglevel) logging.basicConfig(level=args.loglevel)
intents = discord.Intents.default()
intents.message_content = True
bot = ArkhamDBUpdater( bot = ArkhamDBUpdater(
channel_list_file=args.channel_list, command_prefix="!arkhamdb " channel_list_file=args.channel_list,
intents=intents,
) )
bot.run(args.discord_token, reconnect=True) bot.run(args.discord_token, reconnect=True)

View File

@ -33,6 +33,7 @@ class ArkhamDBDeck(TypedDict):
problem: Optional[str] problem: Optional[str]
# TODO: some sort of caching?
class ArkhamDBClient: class ArkhamDBClient:
_session: aiohttp.ClientSession _session: aiohttp.ClientSession

View File

@ -66,8 +66,10 @@ summary = "A drop-in replacement for argparse that allows options to also be set
[[package]] [[package]]
name = "discord.py" name = "discord.py"
version = "1.7.3" version = "2.0.0a3936+g2876622"
requires_python = ">=3.5.3" requires_python = ">=3.8.0"
git = "https://github.com/Rapptz/discord.py"
revision = "2876622f8404c380d2ce5fa353452336ad2c4306"
summary = "A Python wrapper for the Discord API" summary = "A Python wrapper for the Discord API"
dependencies = [ dependencies = [
"aiohttp<3.8.0,>=3.6.0", "aiohttp<3.8.0,>=3.6.0",
@ -126,7 +128,7 @@ dependencies = [
[metadata] [metadata]
lock_version = "3.1" lock_version = "3.1"
content_hash = "sha256:469d71915ef6abb3b5edf07fd93fc3a29cd907b8e5e3c61f5823fc4763c1d325" content_hash = "sha256:29e6b9b9cd9d9cdf264d42904722679db817f05f1f9aeca621f5fd19c7a66130"
[metadata.files] [metadata.files]
"aiohttp 3.7.4.post0" = [ "aiohttp 3.7.4.post0" = [
@ -217,10 +219,6 @@ content_hash = "sha256:469d71915ef6abb3b5edf07fd93fc3a29cd907b8e5e3c61f5823fc476
{file = "ConfigArgParse-1.5.3-py3-none-any.whl", hash = "sha256:18f6535a2db9f6e02bd5626cc7455eac3e96b9ab3d969d366f9aafd5c5c00fe7"}, {file = "ConfigArgParse-1.5.3-py3-none-any.whl", hash = "sha256:18f6535a2db9f6e02bd5626cc7455eac3e96b9ab3d969d366f9aafd5c5c00fe7"},
{file = "ConfigArgParse-1.5.3.tar.gz", hash = "sha256:1b0b3cbf664ab59dada57123c81eff3d9737e0d11d8cf79e3d6eb10823f1739f"}, {file = "ConfigArgParse-1.5.3.tar.gz", hash = "sha256:1b0b3cbf664ab59dada57123c81eff3d9737e0d11d8cf79e3d6eb10823f1739f"},
] ]
"discord.py 1.7.3" = [
{file = "discord.py-1.7.3-py3-none-any.whl", hash = "sha256:c6f64db136de0e18e090f6752ea68bdd4ab0a61b82dfe7acecefa22d6477bb0c"},
{file = "discord.py-1.7.3.tar.gz", hash = "sha256:462cd0fe307aef8b29cbfa8dd613e548ae4b2cb581d46da9ac0d46fb6ea19408"},
]
"idna 3.3" = [ "idna 3.3" = [
{file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"}, {file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"},
{file = "idna-3.3.tar.gz", hash = "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"}, {file = "idna-3.3.tar.gz", hash = "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"},

View File

@ -7,7 +7,7 @@ authors = [
] ]
license-expression = "MIT" license-expression = "MIT"
dependencies = [ dependencies = [
"discord.py>=1.7.3", "discord.py @ git+https://github.com/Rapptz/discord.py",
"aiohttp>=3.7.4.post0", "aiohttp>=3.7.4.post0",
"ConfigArgParse~=1.5", "ConfigArgParse~=1.5",
] ]