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
import configargparse
from datetime import datetime
import json
import re
import logging
from pathlib import Path
import configargparse
import discord
from discord.ext import commands, tasks
from discord.ext import tasks
from discord import app_commands
from arkhamdb import ArkhamDBClient, ArkhamDBDeck
from validate_decks import Validator
class ArkhamDBUpdater(commands.Bot):
class ArkhamDBUpdater(discord.Client):
channel_list_file: Path
channel_list: set[int]
arkhamdb_client: ArkhamDBClient
@ -29,33 +31,36 @@ class ArkhamDBUpdater(commands.Bot):
self.channel_list = set(json.load(f))
self.arkhamdb_client = ArkhamDBClient()
self.setup_commands()
async def close(self) -> None:
await self.arkhamdb_client.close()
await super().close()
def setup_commands(self) -> None:
@self.command(name="monitor")
async def monitor(ctx: commands.Context) -> None:
async def setup_commands(self) -> None:
tree = app_commands.CommandTree(self)
@tree.command()
async def monitor(interaction: discord.Interaction) -> None:
"""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
)
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:
json.dump(list(self.channel_list), f)
@self.command(name="forget")
async def forget(ctx: commands.Context) -> None:
@tree.command()
async def forget(interaction: discord.Interaction) -> None:
"""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
)
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:
json.dump(list(self.channel_list), f)
await tree.sync()
async def on_ready(self) -> None:
logging.info(f"Logged in as {self.user} (ID: {self.user.id})")
logging.info("Enabled on servers:")
@ -63,7 +68,14 @@ class ArkhamDBUpdater(commands.Bot):
logging.info(f" - {guild.name}")
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]:
deck_ids: dict[int, str] = {}
@ -131,6 +143,7 @@ class ArkhamDBUpdater(commands.Bot):
title=f"Updated as of {datetime.now()}", description=message_text
)
# TODO: caching
cards = await self.arkhamdb_client.get_cards(encounter=True)
validator = Validator(cards)
validation_errors = list(
@ -165,7 +178,6 @@ class ArkhamDBUpdater(commands.Bot):
await self.update_channel_latest_decks(message.channel)
async def on_message(self, message: discord.Message) -> None:
await self.process_commands(message)
await self.maybe_update_channel_for_message(message)
async def on_message_edit(
@ -222,8 +234,12 @@ def main():
logging.basicConfig(level=args.loglevel)
intents = discord.Intents.default()
intents.message_content = True
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)

View File

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

12
pdm.lock generated
View File

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

View File

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