From e494990673a3fdf98520a2c44191808ad6d525e1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 2 Aug 2021 12:26:18 +0200 Subject: [PATCH] Added possibility to disable cogs with an environment variable (mainly for Docker) --- README.md | 3 +++ src/main.py | 13 +++++++++++-- src/utils/core.py | 12 +++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2f19dfc..fc26105 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ docker run -d \ -v /here/your/path/:/db ``` +You can add the environment variable `DEACTIVATE` to disable some cogs (separate the cogs with commas and no spaces between). +You may have to recreate the container on Docker to re-enable the disabled cogs. + To find reddit tokens, go to [this site](https://www.reddit.com/prefs/apps) and here are the instructions: ![instructions](https://i.imgur.com/tEzYKDA.png) *redirection uri (for copy/paste) : http://localhost:8080* diff --git a/src/main.py b/src/main.py index 0591b55..2069206 100644 --- a/src/main.py +++ b/src/main.py @@ -1,17 +1,26 @@ print("Chargement des extensions & librairie...", end = " ") import discord -from os import listdir +from os import listdir, rename, getcwd from discord_slash import SlashCommand from discord.ext import commands from utils.reminder import Reminder from utils.core import load -keys = load(["PREFIX", "TOKEN_DISCORD"]) +keys = load(["PREFIX", "TOKEN_DISCORD", "DEACTIVATION"]) customPrefix = keys["PREFIX"] client = commands.Bot(command_prefix = customPrefix, case_insensitive = True, intents = discord.Intents.all()) slash = SlashCommand(client, sync_commands = True) +if keys["DEACTIVATION"] != "None": + path = getcwd() + for file in keys["DEACTIVATION"]: + try: + rename(f"{path}/cogs/{file}.py", f"{path}/cogs/-{file}.py") # désactivation + except: + print(f"\nNo file {file} found, check your \"DEACTIVATION\" variable.") + exit(1) + for file in listdir("cogs"): if file.endswith(".py") and file.startswith("-") == False: client.load_extension(f"cogs.{file[:-3]}") diff --git a/src/utils/core.py b/src/utils/core.py index 84f0d08..56e8098 100644 --- a/src/utils/core.py +++ b/src/utils/core.py @@ -171,9 +171,15 @@ def load(variables): load_dotenv() # load .env file for var in variables: try: - keys[var] = environ[var] + res = environ[var] + if var == "DEACTIVATION": + res = list(set(res.split(',')) - {""}) # create a list for the cogs we need to deactivate and remove blank channels and doubles + keys[var] = res except KeyError: - print(f"Please set the environment variable {var} (.env file supported)") - exit(1) + if var == "DEACTIVATION": + keys[var] = "None" + else: + print(f"Please set the environment variable {var} (.env file supported)") + exit(1) return keys