chore(Configs.py): refactor Configs.py to use environment variables instead of decouple config

feat(Configs.py): add support for loading environment variables from .env file using dotenv
feat(Configs.py): add support for SHOULD_AUTO_DISCONNECT_WHEN_ALONE environment variable
feat(Configs.py): add support for SONG_PLAYBACK_IN_SEPARATE_PROCESS environment variable
feat(Configs.py): add support for MAX_DOWNLOAD_SONGS_AT_A_TIME environment variable
feat(Configs.py): add support for BOT_PREFIX environment variable
feat(Configs.py): add support for BOT_TOKEN environment variable
feat(Configs.py): add support for SPOTIFY_ID and SPOTIFY_SECRET environment variables
feat(Configs.py): add support for CLEANER_MESSAGES_QUANT environment variable
feat(Configs.py): add support for ACQUIRE_LOCK_TIMEOUT environment variable
feat(Configs.py): add support for QUEUE_VIEW_TIMEOUT environment variable
feat(Configs.py): add support for COMMANDS
This commit is contained in:
Paillat 2023-07-13 14:25:38 +02:00
parent 45d27b4c47
commit 219fef7daa

View File

@ -1,54 +1,63 @@
from decouple import config import os
from dotenv import load_dotenv
from Config.Singleton import Singleton from Config.Singleton import Singleton
from Config.Folder import Folder from Config.Folder import Folder
load_dotenv()
class VConfigs(Singleton): class VConfigs(Singleton):
def __init__(self) -> None: def __init__(self) -> None:
if not super().created: if not super().created:
# You can change this boolean to False if you want to prevent the Bot from auto disconnecting # You can change this boolean to False if you want to prevent the Bot from auto disconnecting
# Resolution for the issue: https://github.com/RafaelSolVargas/Vulkan/issues/33 # Resolution for the issue: https://github.com/RafaelSolVargas/Vulkan/issues/33
self.SHOULD_AUTO_DISCONNECT_WHEN_ALONE = False self.SHOULD_AUTO_DISCONNECT_WHEN_ALONE = os.getenv('SHOULD_AUTO_DISCONNECT_WHEN_ALONE') == 'True'
# Recommended to be True, except in cases when your Bot is present in thousands servers, in that case # Recommended to be True, except in cases when your Bot is present in thousands servers, in that case
# the delay to start a new Python process for the playback is too much, and to avoid that you set as False # the delay to start a new Python process for the playback is too much, and to avoid that you set as False
# This feature is for now in testing period, for a more stable version, keep this boolean = True # This feature is for now in testing period, for a more stable version, keep this boolean = Trued
self.SONG_PLAYBACK_IN_SEPARATE_PROCESS = True self.SONG_PLAYBACK_IN_SEPARATE_PROCESS = os.getenv('SONG_PLAYBACK_IN_SEPARATE_PROCESS', 'True') == 'True'
# Maximum of songs that will be downloaded at once, the higher this number is, the faster the songs will be all available # Maximum of songs that will be downloaded at once, the higher this number is, the faster the songs will be all available
# but the slower will be the others commands of the Bot during the downloading time, for example, the playback quality # but the slower will be the others commands of the Bot during the downloading time, for example, the playback quality
self.MAX_DOWNLOAD_SONGS_AT_A_TIME = 5 self.MAX_DOWNLOAD_SONGS_AT_A_TIME = int(os.getenv('MAX_DOWNLOAD_SONGS_AT_A_TIME', 5))
self.BOT_PREFIX = '!' self.BOT_PREFIX = os.getenv('BOT_PREFIX', '!')
try:
self.BOT_TOKEN = config('BOT_TOKEN')
self.SPOTIFY_ID = config('SPOTIFY_ID')
self.SPOTIFY_SECRET = config('SPOTIFY_SECRET')
self.BOT_PREFIX = config('BOT_PREFIX')
except:
print(
'[ERROR] -> You must create and .env file with all required fields, see documentation for help')
self.CLEANER_MESSAGES_QUANT = 5 self.BOT_TOKEN = os.getenv('BOT_TOKEN')
self.ACQUIRE_LOCK_TIMEOUT = 10 if self.BOT_TOKEN is None:
self.QUEUE_VIEW_TIMEOUT = 120 raise ValueError('No token was given')
self.COMMANDS_FOLDER_NAME = 'DiscordCogs'
self.SPOTIFY_ID = os.getenv('SPOTIFY_ID')
self.SPOTIFY_SECRET = os.getenv('SPOTIFY_SECRET')
if self.SPOTIFY_ID is None or self.SPOTIFY_SECRET is None:
print('Spotify will not work')
self.CLEANER_MESSAGES_QUANT = int(os.getenv('CLEANER_MESSAGES_QUANT', 5))
self.ACQUIRE_LOCK_TIMEOUT = int(os.getenv('ACQUIRE_LOCK_TIMEOUT', 10))
self.QUEUE_VIEW_TIMEOUT = int(os.getenv('QUEUE_VIEW_TIMEOUT', 120))
self.COMMANDS_FOLDER_NAME = os.getenv('COMMANDS_FOLDER_NAME', 'DiscordCogs')
self.COMMANDS_PATH = f'{Folder().rootFolder}{self.COMMANDS_FOLDER_NAME}' self.COMMANDS_PATH = f'{Folder().rootFolder}{self.COMMANDS_FOLDER_NAME}'
self.VC_TIMEOUT = 300 self.VC_TIMEOUT = int(os.getenv('VC_TIMEOUT', 300))
self.CHANCE_SHOW_PROJECT = 15 self.CHANCE_SHOW_PROJECT = int(os.getenv('CHANCE_SHOW_PROJECT', 15))
self.PROJECT_URL = 'https://github.com/RafaelSolVargas/Vulkan' self.PROJECT_URL = os.getenv('PROJECT_URL', 'https://github.com/Paillat-dev/Vulkan')
self.SUPPORTING_ICON = 'https://i.pinimg.com/originals/d6/05/b4/d605b4f8c5d1c6ae20dc353ef9f091bd.png' self.SUPPORTING_ICON = os.getenv('SUPPORTING_ICON', 'https://i.pinimg.com/originals/d6/05/b4/d605b4f8c5d1c6ae20dc353ef9f091bd.png')
self.MAX_PLAYLIST_LENGTH = 50 self.MAX_PLAYLIST_LENGTH = int(os.getenv('MAX_PLAYLIST_LENGTH', 50))
self.MAX_PLAYLIST_FORCED_LENGTH = 5 self.MAX_PLAYLIST_FORCED_LENGTH = int(os.getenv('MAX_PLAYLIST_FORCED_LENGTH', 5))
self.MAX_SONGS_IN_PAGE = 10 self.MAX_SONGS_IN_PAGE = int(os.getenv('MAX_SONGS_IN_PAGE', 10))
self.MAX_PRELOAD_SONGS = 15 self.MAX_PRELOAD_SONGS = int(os.getenv('MAX_PRELOAD_SONGS', 15))
self.MAX_SONGS_HISTORY = 15 self.MAX_SONGS_HISTORY = int(os.getenv('MAX_SONGS_HISTORY', 15))
self.INVITE_MESSAGE = """To invite Vulkan to your own server, click [here]({}). self.INVITE_MESSAGE = os.getenv('INVITE_MESSAGE', """To invite Vulkan to your own server, click [here]({}).
Or use this direct URL: {}""" Or use this direct URL: {}""")
self.MY_ERROR_BAD_COMMAND = 'This string serves to verify if some error was raised by myself on purpose' self.MY_ERROR_BAD_COMMAND = os.getenv('MY_ERROR_BAD_COMMAND', 'This string serves to verify if some error was raised by myself on purpose')
self.INVITE_URL = 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot' self.INVITE_URL = os.getenv('INVITE_URL', 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot')
def getPlayersManager(self): def getPlayersManager(self):
return self.__manager return self.__manager