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
import argparse
from datetime import datetime
import json
import re
from pathlib import Path
import discord
from discord.ext import commands, tasks
@ -13,14 +15,17 @@ from secret import TOKEN
class ArkhamDBUpdater(commands.Bot):
channel_list_file: Path
channel_list: set[int]
arkhamdb_client: ArkhamDBClient
def __init__(self, *args, **kwargs) -> None:
def __init__(self, channel_list_file: Path, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.channel_list_file = channel_list_file
# 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.arkhamdb_client = ArkhamDBClient()
@ -38,7 +43,7 @@ class ArkhamDBUpdater(commands.Bot):
"Now monitoring this channel for deck IDs", mention_author=True
)
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)
@self.command(name="forget")
@ -48,7 +53,7 @@ class ArkhamDBUpdater(commands.Bot):
"No longer monitoring this channel for deck IDs", mention_author=True
)
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)
async def on_ready(self) -> None:
@ -176,5 +181,18 @@ class ArkhamDBUpdater(commands.Bot):
await self.update_channel_latest_decks(channel)
bot = ArkhamDBUpdater(command_prefix="!arkhamdb ")
bot.run(TOKEN, reconnect=True)
def main():
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]
Homepage = "https://git.adamgoldsmith.name/adam/ahtcg_discord_bot"
[project.scripts]
arkhamdb_discord_bot = "ahtcg_bot:main"
[tool]
[tool.pdm]
[[tool.pdm.source]]