Add CLI argument for channel list json file

This commit is contained in:
Adam Goldsmith 2022-03-14 11:37:59 -04:00
parent 13cf739656
commit dcab5b6b40
2 changed files with 27 additions and 6 deletions

View File

@ -1,8 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
from datetime import datetime from datetime import datetime
import json import json
import re import re
from pathlib import Path
import discord import discord
from discord.ext import commands, tasks from discord.ext import commands, tasks
@ -13,14 +15,17 @@ from secret import TOKEN
class ArkhamDBUpdater(commands.Bot): class ArkhamDBUpdater(commands.Bot):
channel_list_file: Path
channel_list: set[int] channel_list: set[int]
arkhamdb_client: ArkhamDBClient arkhamdb_client: ArkhamDBClient
def __init__(self, *args, **kwargs) -> None: def __init__(self, channel_list_file: Path, *args, **kwargs) -> None:
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.channel_list_file = channel_list_file
# TODO: should really be a database # TODO: should really be a database
with open("channel_list.json") as f: with open(self.channel_list_file) as f:
self.channel_list = set(json.load(f)) self.channel_list = set(json.load(f))
self.arkhamdb_client = ArkhamDBClient() self.arkhamdb_client = ArkhamDBClient()
@ -38,7 +43,7 @@ class ArkhamDBUpdater(commands.Bot):
"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(ctx.message.channel.id)
with open("channel_list.json", "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") @self.command(name="forget")
@ -48,7 +53,7 @@ class ArkhamDBUpdater(commands.Bot):
"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(ctx.message.channel.id)
with open("channel_list.json", "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)
async def on_ready(self) -> None: async def on_ready(self) -> None:
@ -176,5 +181,18 @@ class ArkhamDBUpdater(commands.Bot):
await self.update_channel_latest_decks(channel) await self.update_channel_latest_decks(channel)
bot = ArkhamDBUpdater(command_prefix="!arkhamdb ") def main():
bot.run(TOKEN, reconnect=True) parser = argparse.ArgumentParser(
description="A Discord bot for ArkhamDB deck info."
)
parser.add_argument(
"channel_list", type=Path, help="A path to a json file to store channel info"
)
args = parser.parse_args()
bot = ArkhamDBUpdater(channel_list=args.channel_list, command_prefix="!arkhamdb ")
bot.run(TOKEN, reconnect=True)
if __name__ == "__main__":
main()

View File

@ -15,6 +15,9 @@ requires-python = ">=3.10"
[project.urls] [project.urls]
Homepage = "https://git.adamgoldsmith.name/adam/ahtcg_discord_bot" Homepage = "https://git.adamgoldsmith.name/adam/ahtcg_discord_bot"
[project.scripts]
arkhamdb_discord_bot = "ahtcg_bot:main"
[tool] [tool]
[tool.pdm] [tool.pdm]
[[tool.pdm.source]] [[tool.pdm.source]]