Use configargparse to accept discord token from env or config file

This commit is contained in:
Adam Goldsmith 2022-03-14 11:57:28 -04:00
parent dcab5b6b40
commit 83dfec3aa6
4 changed files with 33 additions and 10 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
/secret.py
/channel_list.json
/__pypackages__/
/.pdm.toml
__pycache__/
/arkhamdb_discord_bot.conf

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import argparse
import configargparse
from datetime import datetime
import json
import re
@ -11,7 +11,6 @@ from discord.ext import commands, tasks
from arkhamdb import ArkhamDBClient, ArkhamDBDeck
from validate_decks import Validator
from secret import TOKEN
class ArkhamDBUpdater(commands.Bot):
@ -182,16 +181,29 @@ class ArkhamDBUpdater(commands.Bot):
def main():
parser = argparse.ArgumentParser(
description="A Discord bot for ArkhamDB deck info."
parser = configargparse.ArgParser(
description="A Discord bot for ArkhamDB deck info.",
default_config_files=["./arkhamdb_discord_bot.conf"],
)
parser.add_argument(
"channel_list", type=Path, help="A path to a json file to store channel info"
parser.add(
"--channel_list",
required=True,
type=Path,
help="path to a json file to store channel info",
)
parser.add("-c", "--config", is_config_file=True, help="config file path")
parser.add(
"--discord_token",
required=True,
help="Discord API token",
env_var="DISCORD_TOKEN",
)
args = parser.parse_args()
bot = ArkhamDBUpdater(channel_list=args.channel_list, command_prefix="!arkhamdb ")
bot.run(TOKEN, reconnect=True)
bot = ArkhamDBUpdater(
channel_list_file=args.channel_list, command_prefix="!arkhamdb "
)
bot.run(args.discord_token, reconnect=True)
if __name__ == "__main__":

View File

@ -58,6 +58,12 @@ version = "0.4.4"
requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
summary = "Cross-platform colored terminal text."
[[package]]
name = "configargparse"
version = "1.5.3"
requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
summary = "A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables."
[[package]]
name = "discord.py"
version = "1.7.3"
@ -120,7 +126,7 @@ dependencies = [
[metadata]
lock_version = "3.1"
content_hash = "sha256:da292ccf4e3850e904927e30412026e99f5618b549b8de606742ccc0f6e03d64"
content_hash = "sha256:469d71915ef6abb3b5edf07fd93fc3a29cd907b8e5e3c61f5823fc4763c1d325"
[metadata.files]
"aiohttp 3.7.4.post0" = [
@ -207,6 +213,10 @@ content_hash = "sha256:da292ccf4e3850e904927e30412026e99f5618b549b8de606742ccc0f
{file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"},
{file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"},
]
"configargparse 1.5.3" = [
{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"},

View File

@ -9,6 +9,7 @@ license-expression = "MIT"
dependencies = [
"discord.py>=1.7.3",
"aiohttp>=3.7.4.post0",
"ConfigArgParse~=1.5",
]
requires-python = ">=3.10"