From 131b452e14555bfd2b0ed462a3c283badda00f1a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Mar 2021 20:25:10 +0100 Subject: [PATCH 1/5] remove tokens.py and adding environnement variable --- .gitignore | 2 +- README.md | 20 ++++++++++---------- src/cogs/internet.py | 5 ++--- src/cogs/music.py | 4 ++-- src/main.py | 7 +++---- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 01d1d66..c8f4f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ update/ .dockerignore Dockerfile -src/tokens.py +src/tokens.env src/cogs/music_old.py diff --git a/README.md b/README.md index a039636..08a7067 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,25 @@ - Python 3.8.6 - [requirements.txt](requirements.txt) +- Docker #### __Setting up__ -- A `tokens.py` file that contains: +docker...in progress... + +env: + +TOKEN_DISCORD="your token discord" +TOKEN_GENIUS="your genius token" +TOKEN_REDDIT_CLIENT_ID="your client id" +TOKEN_REDDIT_CLIENT_SECRET="your client secret" +TOKEN_REDDIT_USER_AGENT="your user agent" -```py -token_discord = "your discord token" -token_genius = "your genius token" -token_reddit = {"client_id": "your client id", "client_secret": "your secret pass", "user_agent": "your reddit's username"} -``` 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* -#### __Starting up__ - -- Run [main.py](main.py) - #### __Add the bot to your server__ - [This site](https://discordapi.com/permissions.html) allows you to choose which permissions to add by default to the bot. diff --git a/src/cogs/internet.py b/src/cogs/internet.py index 93f4c9a..8633a21 100644 --- a/src/cogs/internet.py +++ b/src/cogs/internet.py @@ -1,7 +1,6 @@ -import discord, praw, json, requests, time, feedparser +import discord, praw, json, requests, time, feedparser, os from discord.ext import commands from random import randint, choice -from tokens import token_reddit as token # à l'importation de l'extension, le fichier se retrouve dans le '/' et non dans 'cogs/', ignorez l'erreur pylint def setup(client): client.add_cog(Internet(client)) @@ -34,7 +33,7 @@ class Internet(commands.Cog): async def _memes(self, ctx, *, args = ""): """Envois un meme de reddit.\n ➡ Syntaxe: .memes/meme [subreddit]⁢⁢⁢⁢⁢⁢⁢⁢⁢⁢""" try: - reddit = praw.Reddit(client_id = token['client_id'], client_secret = token['client_secret'], user_agent = f"disreddit /u/{token['user_agent']}, http://localhost:8080") + reddit = praw.Reddit(client_id = os.environ['TOKEN_REDDIT_CLIENT_ID'], client_secret = os.environ['TOKEN_REDDIT_CLIENT_SECRET'], user_agent = f"disreddit /u/{os.environ['TOKEN_REDDIT_USER_AGENT']}, http://localhost:8080") if args != "": # si il y a un arg différent d'un meme subredditchoix = args diff --git a/src/cogs/music.py b/src/cogs/music.py index a6603f5..66e8ef0 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -20,8 +20,8 @@ from discord.ext import commands # Genius API import lyricsgenius import time -from tokens import token_genius as token # à l'importation de l'extension, music.py se retrouve dans le '/' et non dans 'cogs/', ignorez l'erreur -genius = lyricsgenius.Genius(token) +import os +genius = lyricsgenius.Genius(os.environ['TOKEN_GENIUS']) # Silence useless bug reports messages youtube_dl.utils.bug_reports_message = lambda: '' diff --git a/src/main.py b/src/main.py index 009e809..a851d14 100644 --- a/src/main.py +++ b/src/main.py @@ -1,11 +1,10 @@ print("Connexion à Discord...") -import discord, re, pytz +import discord, re, pytz, os from discord.ext import commands from random import choice from datetime import datetime from pytz import timezone -from tokens import token_discord as token client = commands.Bot(command_prefix = ".", case_insensitive = True, intents = discord.Intents.all()) @@ -20,7 +19,7 @@ client.load_extension("cogs.autopublish") @client.event async def on_connect(): - print(f"Connecté avec le token : {token}.") + print(f"Connecté.") @client.event async def on_ready(): @@ -212,4 +211,4 @@ def user_or_nick(user): else: return f"{user.name}#{user.discriminator}" -client.run(token) +client.run(os.environ['TOKEN_DISCORD']) From a01e2371db6a35a4bbc8681e10800a230f0c48be Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Mar 2021 20:40:11 +0100 Subject: [PATCH 2/5] adding dockerfile --- .gitignore | 2 +- Dockerfile | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/.gitignore b/.gitignore index c8f4f3d..d3f4f1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ +.vscode/ update/ .dockerignore -Dockerfile src/tokens.env src/cogs/music_old.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d72912b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.9.2-slim + +RUN apt update && \ + apt install -y --no-install-recommends ffmpeg git && \ + apt autoremove +RUN /usr/local/bin/python -m pip install --upgrade pip + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY src . + +CMD [ "python", "-u", "./main.py" ] From 66a53e4a864f7832ed34238f9c926de5fe559f8a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Mar 2021 21:36:01 +0100 Subject: [PATCH 3/5] add how to setup --- README.md | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 08a7067..1950276 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,19 @@ #### __Setting up__ -docker...in progress... - -env: - -TOKEN_DISCORD="your token discord" -TOKEN_GENIUS="your genius token" -TOKEN_REDDIT_CLIENT_ID="your client id" -TOKEN_REDDIT_CLIENT_SECRET="your client secret" -TOKEN_REDDIT_USER_AGENT="your user agent" +You have to replace `TOKEN_DISCORD`, `TOKEN_GENIUS`, `TOKEN_REDDIT_CLIENT_ID`, `TOKEN_REDDIT_CLIENT_SECRET` and `TOKEN_REDDIT_USER_AGENT` with your desired values. +With a [docker-compose](docker-compose.yml) or in command line: +``` +docker run -d \ + --name="kassoubot" \ + mylloon/kassoubot \ + --TOKEN_DISCORD="yourValue" \ + --TOKEN_GENIUS="yourValue" \ + --TOKEN_REDDIT_CLIENT_ID="yourValue" \ + --TOKEN_REDDIT_CLIENT_SECRET="yourValue" \ + --TOKEN_REDDIT_USER_AGENT="yourValue" +``` 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) From b28b3a21353d243a332e1b6a06d12e50b6f6ee04 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Mar 2021 21:36:41 +0100 Subject: [PATCH 4/5] downgrade python 3.9.2 to 3.8.6 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d72912b..1f0f76d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9.2-slim +FROM python:3.8.6-slim RUN apt update && \ apt install -y --no-install-recommends ffmpeg git && \ From 131aac0ea3fb016b210e86029f0575d7aa50213a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Mar 2021 21:37:05 +0100 Subject: [PATCH 5/5] ajout docker compose --- docker-compose.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2272774 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "2.1" +services: + kassoubot: + image: mylloon/kassoubot + container_name: kassoubot + environment: + - TOKEN_DISCORD=your-token-discord + - TOKEN_GENIUS=your-token-genius + - TOKEN_REDDIT_CLIENT_ID=your-reddit-client-id + - TOKEN_REDDIT_CLIENT_SECRET=your-reddit-client-secret + - TOKEN_REDDIT_USER_AGENT=your-reddit-user-agent + restart: unless-stopped