From 219fef7daaafabfba70ac3cae4e8c0c8debe36f5 Mon Sep 17 00:00:00 2001 From: Paillat Date: Thu, 13 Jul 2023 14:25:38 +0200 Subject: [PATCH 1/9] 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 --- Config/Configs.py | 71 ++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/Config/Configs.py b/Config/Configs.py index 682d251..8e3dc49 100644 --- a/Config/Configs.py +++ b/Config/Configs.py @@ -1,54 +1,63 @@ -from decouple import config +import os + +from dotenv import load_dotenv from Config.Singleton import Singleton from Config.Folder import Folder +load_dotenv() + class VConfigs(Singleton): def __init__(self) -> None: if not super().created: # 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 - 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 # 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 - self.SONG_PLAYBACK_IN_SEPARATE_PROCESS = True + # This feature is for now in testing period, for a more stable version, keep this boolean = Trued + 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 # 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 = '!' - 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.BOT_PREFIX = os.getenv('BOT_PREFIX', '!') - self.CLEANER_MESSAGES_QUANT = 5 - self.ACQUIRE_LOCK_TIMEOUT = 10 - self.QUEUE_VIEW_TIMEOUT = 120 - self.COMMANDS_FOLDER_NAME = 'DiscordCogs' + self.BOT_TOKEN = os.getenv('BOT_TOKEN') + if self.BOT_TOKEN is None: + raise ValueError('No token was given') + + 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.VC_TIMEOUT = 300 + self.VC_TIMEOUT = int(os.getenv('VC_TIMEOUT', 300)) - self.CHANCE_SHOW_PROJECT = 15 - self.PROJECT_URL = 'https://github.com/RafaelSolVargas/Vulkan' - self.SUPPORTING_ICON = 'https://i.pinimg.com/originals/d6/05/b4/d605b4f8c5d1c6ae20dc353ef9f091bd.png' + self.CHANCE_SHOW_PROJECT = int(os.getenv('CHANCE_SHOW_PROJECT', 15)) + self.PROJECT_URL = os.getenv('PROJECT_URL', 'https://github.com/Paillat-dev/Vulkan') + 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_FORCED_LENGTH = 5 - self.MAX_SONGS_IN_PAGE = 10 - self.MAX_PRELOAD_SONGS = 15 - self.MAX_SONGS_HISTORY = 15 + self.MAX_PLAYLIST_LENGTH = int(os.getenv('MAX_PLAYLIST_LENGTH', 50)) + self.MAX_PLAYLIST_FORCED_LENGTH = int(os.getenv('MAX_PLAYLIST_FORCED_LENGTH', 5)) + self.MAX_SONGS_IN_PAGE = int(os.getenv('MAX_SONGS_IN_PAGE', 10)) + self.MAX_PRELOAD_SONGS = int(os.getenv('MAX_PRELOAD_SONGS', 15)) + self.MAX_SONGS_HISTORY = int(os.getenv('MAX_SONGS_HISTORY', 15)) - self.INVITE_MESSAGE = """To invite Vulkan to your own server, click [here]({}). - Or use this direct URL: {}""" + self.INVITE_MESSAGE = os.getenv('INVITE_MESSAGE', """To invite Vulkan to your own server, click [here]({}). + 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.INVITE_URL = 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot' + 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 = os.getenv('INVITE_URL', 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot') def getPlayersManager(self): return self.__manager From d86f014a3a9cf6c88cb7ccdf32885e14673f3075 Mon Sep 17 00:00:00 2001 From: Paillat Date: Thu, 13 Jul 2023 14:26:37 +0200 Subject: [PATCH 2/9] chore(requirements.txt): update requirements.txt file --- requirements.txt | Bin 1196 -> 1226 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0ade81b730e274f76459dec084178e41ceb32ff1..2cfa34362ac4d319dd938af1ebbd12636acd05b1 100644 GIT binary patch delta 38 pcmZ3(d5Uwx8Wy<%hDwGKh75*$hCBvch7=%O!jKANmoeA^App$M2;cw! delta 7 OcmX@bxrTGY8WsQyuL9`+ From f336df43ee6b97577931d63cd7d27346fe26f81d Mon Sep 17 00:00:00 2001 From: Paillat Date: Thu, 13 Jul 2023 14:47:59 +0200 Subject: [PATCH 3/9] chore(requirements.txt): update requirements.txt file --- requirements.txt | Bin 1226 -> 1222 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2cfa34362ac4d319dd938af1ebbd12636acd05b1..6eacd95a2412be680a3fe1574aa8953f4c912737 100644 GIT binary patch delta 7 OcmX@bd5m+zF%|#~Wdj`m delta 12 TcmX@cd5UwxF%}kE23rOI9&iJj From 83a864725d2793c5ec3498d3fc1a8011c30f5382 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:12:38 +0200 Subject: [PATCH 4/9] fix(Configs.py): update PROJECT_URL to point to the correct GitHub repository --- Config/Configs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Config/Configs.py b/Config/Configs.py index 8e3dc49..a3a3982 100644 --- a/Config/Configs.py +++ b/Config/Configs.py @@ -44,7 +44,7 @@ class VConfigs(Singleton): self.VC_TIMEOUT = int(os.getenv('VC_TIMEOUT', 300)) self.CHANCE_SHOW_PROJECT = int(os.getenv('CHANCE_SHOW_PROJECT', 15)) - self.PROJECT_URL = os.getenv('PROJECT_URL', 'https://github.com/Paillat-dev/Vulkan') + self.PROJECT_URL = os.getenv('PROJECT_URL', 'https://github.com/RafaelSolVargas/Vulkan') self.SUPPORTING_ICON = os.getenv('SUPPORTING_ICON', 'https://i.pinimg.com/originals/d6/05/b4/d605b4f8c5d1c6ae20dc353ef9f091bd.png') self.MAX_PLAYLIST_LENGTH = int(os.getenv('MAX_PLAYLIST_LENGTH', 50)) From 6cfd188c3be2c40ebc9855fa77bb2f9f30a837f8 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:22:53 +0200 Subject: [PATCH 5/9] feat: add Dockerfile and docker-compose.yaml for containerization The Dockerfile is added to define the container image for the application. It uses the Python 3.10.0 base image, sets the PYTHONUNBUFFERED environment variable, clones the Vulkan repository, installs the required dependencies, installs ffmpeg, creates a non-root user, and sets the command to run the main.py file. The docker-compose.yaml file is added to define the Docker Compose configuration for the application. It specifies a service named "vulkan" that builds the image using the Dockerfile and uses the .env file for environment variables. --- Dockerfile | 11 +++++++++++ docker-compose.yaml | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6474b4e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.10.0 + +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app +RUN git clone https://github.com/RafaelSolVargas/Vulkan.git /app +RUN pip install -r requirements.txt +RUN apt-get update && apt-get install -y software-properties-common && apt-get install -y ffmpeg +RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app +USER appuser +CMD ["python", "main.py"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..ab3a80c --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,8 @@ +version: '3' +services: + vulkan: + build: + context: . + dockerfile: Dockerfile + env_file: + - .env \ No newline at end of file From aa5649bcdc24d21515723ce4c862847723839c62 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:50:14 +0200 Subject: [PATCH 6/9] fix(Configs.py): set SPOTIFY_ID and SPOTIFY_SECRET to None if they are set to default values --- Config/Configs.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Config/Configs.py b/Config/Configs.py index a3a3982..dd6ffdc 100644 --- a/Config/Configs.py +++ b/Config/Configs.py @@ -31,7 +31,13 @@ class VConfigs(Singleton): self.SPOTIFY_ID = os.getenv('SPOTIFY_ID') self.SPOTIFY_SECRET = os.getenv('SPOTIFY_SECRET') - + + if self.SPOTIFY_ID =="Your_Own_Spotify_ID": + self.SPOTIFY_ID = None + + if self.SPOTIFY_SECRET =="Your_Own_Spotify_Secret": + self.SPOTIFY_SECRET = None + if self.SPOTIFY_ID is None or self.SPOTIFY_SECRET is None: print('Spotify will not work') From a82bf7950ba83c0c68f7342a9689aeb9665d2974 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:53:23 +0200 Subject: [PATCH 7/9] fix(Configs.py): change default BOT_PREFIX to '!' if it is set to 'Your_Wanted_Prefix_For_Vulkan' --- Config/Configs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Config/Configs.py b/Config/Configs.py index dd6ffdc..28effe2 100644 --- a/Config/Configs.py +++ b/Config/Configs.py @@ -25,6 +25,9 @@ class VConfigs(Singleton): self.BOT_PREFIX = os.getenv('BOT_PREFIX', '!') + if self.BOT_PREFIX == 'Your_Wanted_Prefix_For_Vulkan': + self.BOT_PREFIX = '!' + self.BOT_TOKEN = os.getenv('BOT_TOKEN') if self.BOT_TOKEN is None: raise ValueError('No token was given') From 582e35bce18b64c442461812d5db0be31408ca39 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:53:55 +0200 Subject: [PATCH 8/9] chore(.env.example): add example .env file with configurable settings docs(DOCKER.md): add instructions for running the bot in a Docker container docs(SETTINGS.md): update instructions for changing bot settings in .env file docs(README.md): update instructions for running the bot and add Docker instructions --- .env.example | 7 +++++++ .github/Docs/DOCKER.md | 31 +++++++++++++++++++++++++++++++ .github/Docs/SETTINGS.md | 23 ++++++++++++++++------- README.md | 10 +++++++--- 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 .env.example create mode 100644 .github/Docs/DOCKER.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..61323b8 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +BOT_TOKEN=Your_Own_Bot_Token +# If you do not want to use spotify, remove the 2 following lines +SPOTIFY_ID=Your_Own_Spotify_ID +SPOTIFY_SECRET=Your_Own_Spotify_Secret +BOT_PREFIX=Your_Wanted_Prefix_For_Vulkan +SHOULD_AUTO_DISCONNECT_WHEN_ALONE=True #all settings can be set like this +#etc... All settings can be set this way \ No newline at end of file diff --git a/.github/Docs/DOCKER.md b/.github/Docs/DOCKER.md new file mode 100644 index 0000000..2192ef5 --- /dev/null +++ b/.github/Docs/DOCKER.md @@ -0,0 +1,31 @@ +

Docker

+ +This bot can be easily deployed using Docker. + +## **Requirements** +To run this project in a Docker container you must have Docker and Docker Compose installed in your machine. Find how to install Docker in your machine [here](https://docs.docker.com/get-docker/) and find how to install Docker Compose in your machine [here](https://docs.docker.com/compose/install/). + +Once you have Docker and Docker Compose installed in your machine, clone or download this repository and follow the instructions below. + +## **Running the Bot** +To run the bot in a Docker container, you must first create a `.env` file in the root of the project if there isn't one already. You will need to change the parameters in the `.env` file to your own parameters. You can find an example of a `.env` file [here](.env.example). You will also be able to change the settings in that environment file as explained in the [Settings page](.github/Docs/SETTINGS.md). + +To run the bot, simply execute the following command in the root of the project: +```bash +docker-compose up -d +``` +This will build the Docker image and run the bot in a Docker container. The `-d` flag is used to run the container in detached mode, which means that the container will run in the background. If you want to see the logs of the container, you can run the following command: +```bash +docker-compose logs -f +``` + +To stop the container, run the following command: +```bash +docker-compose down +``` +## **Updating the Bot** +To update the bot, you must first stop the container as explained in the previous section. Then, you must pull the latest changes from the repository, in any way you want. Finally, you must build the Docker image again and run the container again. To do this, run the following commands: +```bash +docker-compose build +docker-compose up -d +``` \ No newline at end of file diff --git a/.github/Docs/SETTINGS.md b/.github/Docs/SETTINGS.md index b7af533..4a402e9 100644 --- a/.github/Docs/SETTINGS.md +++ b/.github/Docs/SETTINGS.md @@ -8,31 +8,40 @@ - Maximum songs downloading at a time - Maximum songs in a Queue Page -All parameters can be modified in the Configs.py file that are located in the Config folder -> The path to the file is ./Config/Configs.py +All parameters can be modified in the .env file or in an environment variable. +Here is a sample of the .env file: + +```env +BOT_TOKEN=Your_Own_Bot_Token +SPOTIFY_ID=Your_Own_Spotify_ID +SPOTIFY_SECRET=Your_Own_Spotify_Secret +BOT_PREFIX=Your_Wanted_Prefix_For_Vulkan +SHOULD_AUTO_DISCONNECT_WHEN_ALONE=True #all settings can be set like this +#etc... All settings can be set this way +``` ### **Bot Prefix** The Bot Prefix is just a string that must be passed as prefix when calling any Bot command from the Discord. To change that you must:
-- Change the property BOT_PREFIX of the VConfigs class to what prefix you want to. +- Change the property BOT_PREFIX in the .env file or in an environment variable to what you want to. ### **Auto Disconnect** As a result of the [Issue 33](https://github.com/RafaelSolVargas/Vulkan/issues/33) you can configure if the Bot will auto disconnect when being alone in the voice channel. The default configuration is to disconnect within 300 seconds if it finds out no one is currently listing to it. To change that you must:
-- Change the property SHOULD_AUTO_DISCONNECT_WHEN_ALONE of the VConfigs class to False +- Change the property SHOULD_AUTO_DISCONNECT_WHEN_ALONE in the .env file or in an environment variable to False ### **Multiprocessing or Threading** As a result of the [Issue 35](https://github.com/RafaelSolVargas/Vulkan/issues/35) you can configure if the Bot will create a specific Python Process for each Player (Guild) that he is playing songs or all will happen in the Main Process. The Default behavior is to create a new process. To change that you must:
-- Change the property SONG_PLAYBACK_IN_SEPARATE_PROCESS of the VConfigs class to False +- Change the property SONG_PLAYBACK_IN_SEPARATE_PROCESS in the .env file or in an environment variable to False ### **Maximum Downloading Quant** The download of songs can be very fast or very slow, the faster it is the slower the response time for any command (during the download) is higher, (including the playback quality), because there will be a Task for each song. But it's possible to set up this variable to slow the download and keep the response time better. To change that you must:
-- Change the property MAX_DOWNLOAD_SONGS_AT_A_TIME of the VConfigs class to what you want to. +- Change the property MAX_DOWNLOAD_SONGS_AT_A_TIME in the .env file or in an environment variable to what you want to. ### **Maximum Songs In Queue Page** When the ```Queue``` command is called, the current song playlist is presented in the Discord, you can configure how many songs you will want to show in each page. To change that you must:
-- Change the property MAX_SONGS_IN_PAGE of the VConfigs class to what you want to. \ No newline at end of file +- Change the property MAX_SONGS_IN_PAGE in the .env file or in an environment variable to what you want to. \ No newline at end of file diff --git a/README.md b/README.md index 4a89b2d..fd3e8eb 100644 --- a/README.md +++ b/README.md @@ -62,16 +62,17 @@ You can download the executables in this link `https://www.ffmpeg.org/download.h ### **.Env File Example** This is an example of how your .env file (located in root) should look like. -``` +```env BOT_TOKEN=Your_Own_Bot_Token SPOTIFY_ID=Your_Own_Spotify_ID SPOTIFY_SECRET=Your_Own_Spotify_Secret BOT_PREFIX=Your_Wanted_Prefix_For_Vulkan - +SHOULD_AUTO_DISCONNECT_WHEN_ALONE=True #all settings can be set like this +#etc... All settings can be set this way ``` ### **⚙️ Configs** -The config file is located at ```./config/Configs.py```, it doesn't require any change, but if you can change the values to the way you want.
+The bot's configuration is stored in the [.env](.env) file, you can change the prefix and the bot token there, as well as all the other configurations. Take a look in the [Settings page](.github/Docs/SETTINGS.md) to personalize the Bot for you. @@ -80,6 +81,9 @@ Take a look in the [Settings page](.github/Docs/SETTINGS.md) to personalize the - Run ```python main.py``` in console to start - Give this project a nice 🌟 +### **🐳 Docker** +You can also run this project in a Docker container. You can find the instructions to run this project in a Docker container in the [Docker Instructions](.github/Docs/DOCKER.md) page. +

From 634e6560c69c213baa38fa5c9e7bbc6425dc1a20 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 14 Jul 2023 16:55:59 +0200 Subject: [PATCH 9/9] docs(DOCKER.md): update instructions for running the bot in a Docker container - Add clarification about creating a `.env` file in the root of the project - Mention the need to change parameters in the `.env` file - Provide a link to an example `.env` file - Refer to the [Settings page](.github/Docs/SETTINGS.md) for changing environment file settings - Add a note about trying the commands without a dash between docker and compose if they fail --- .github/Docs/DOCKER.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/Docs/DOCKER.md b/.github/Docs/DOCKER.md index 2192ef5..e101cd5 100644 --- a/.github/Docs/DOCKER.md +++ b/.github/Docs/DOCKER.md @@ -8,7 +8,9 @@ To run this project in a Docker container you must have Docker and Docker Compos Once you have Docker and Docker Compose installed in your machine, clone or download this repository and follow the instructions below. ## **Running the Bot** -To run the bot in a Docker container, you must first create a `.env` file in the root of the project if there isn't one already. You will need to change the parameters in the `.env` file to your own parameters. You can find an example of a `.env` file [here](.env.example). You will also be able to change the settings in that environment file as explained in the [Settings page](.github/Docs/SETTINGS.md). +To run the bot in a Docker container, you must first create a `.env` file in the root of the project if there isn't one already. You will need to change the parameters in the `.env` file to your own parameters. You can find an example of a `.env` file [here](.env.example). You will also be able to change the settings in that environment file as explained in the [Settings page](.github/Docs/SETTINGS.md). + +**If any of the following commands fail, try without a dash between docker and compose.** To run the bot, simply execute the following command in the root of the project: ```bash