Added possibility to disable cogs with an environment variable (mainly for Docker)

This commit is contained in:
Mylloon 2021-08-02 12:26:18 +02:00
parent c123d04631
commit e494990673
3 changed files with 23 additions and 5 deletions

View file

@ -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*

View file

@ -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]}")

View file

@ -171,8 +171,14 @@ 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:
if var == "DEACTIVATION":
keys[var] = "None"
else:
print(f"Please set the environment variable {var} (.env file supported)")
exit(1)