Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
enable using environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzbit committed Dec 9, 2021
1 parent e131abf commit 66ebebe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG='{"administratorIds": [],"automaticallyAmputate": true,"discordToken": "PUT DISCORD TOKEN HERE","logLevel": "debug","logOutput": "both"}'
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ Copy example_config.json to config.json and put in your discord bot token

"logOutput" can be "stdout", "file", or "both"

"administratorIds" is an array of user IDs for users who are able to run administrator commands (currently only !archivestatus)
"administratorIds" is an array of user IDs for users who are able to run administrator commands (currently only !archivestatus)

You can also set $CONFIG to the config json either via environment variables or the .env file.
33 changes: 23 additions & 10 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import discord
import json
import logging
import os
import pathlib
import requests
import sys
import time
import urllib
from dotenv import load_dotenv
from urlextract import URLExtract

archive_api = 'https://web.archive.org'
Expand Down Expand Up @@ -39,18 +41,29 @@ def load_config(self):
'''
Initializes the bot state by reading it from a file
'''
self.current_dir = str(pathlib.Path(__file__).resolve().parent)
config_file = f'{current_dir}/config.json'
state_logger = logging.getLogger('bot')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
state_logger.addHandler(ch)
load_dotenv()
try:
with open(config_file, 'r') as read_file:
try:
self.config = json.load(read_file)
except Exception as e:
logger.error(f'Unable to read config file at {config_file}, {e}')
sys.exit(1)
self.config = json.loads(os.environ.get('CONFIG'))
except Exception as e:
logger.warning(f'Config file not found at {config_file}, exiting')
sys.exit(1)
state_logger.error(f'$CONFIG environment variable could not be read (exception was {e}), trying to load from config.json')
self.current_dir = str(pathlib.Path(__file__).resolve().parent)
config_file = f'{current_dir}/config.json'
try:
with open(config_file, 'r') as read_file:
try:
self.config = json.load(read_file)
except Exception as e:
logger.error(f'Unable to read config file at {config_file}, {e}')
sys.exit(1)
except Exception as e:
logger.warning(f'Config file not found at {config_file}, exiting')
sys.exit(1)
state_logger.removeHandler(ch)

# This object keeps track of handled messages.
self.handled_messages = []
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
discord.py == 1.4.1
python-dotenv == 0.19.2
urlextract == 1.3.0
requests == 2.25.1

0 comments on commit 66ebebe

Please sign in to comment.