From 54bee59aa80f3103a8ce2ca136b990acb98a9ce5 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 28 Jul 2021 03:17:37 +0200 Subject: [PATCH] using new .env system --- .gitignore | 2 +- README.md | 2 +- requirements.txt | 1 + src/cogs/internet.py | 6 +++--- src/cogs/music.py | 6 +++--- src/main.py | 8 +++++--- src/utils/time.py | 4 ++-- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 0196d05..b59abca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .vscode/ update/ __pycache__/ -.envrc +.env *.sqlite3 diff --git a/README.md b/README.md index 0281955..b161e72 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,5 @@ To find reddit tokens, go to [this site](https://www.reddit.com/prefs/apps) and - Using SQLite for the database. ## __Launching locally__ -If you want to run it without Docker, I personally use [direnv](https://direnv.net/) to store variables and have them only in the working folder. +If you want to run it without Docker, create an .env file to store variables in the root folder. Simply run `python3 main.py` in `src` folder to launch the bot in the repo folder. diff --git a/requirements.txt b/requirements.txt index faa329d..cb82009 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ youtube-dl==2021.6.6 # music lyricsgenius==3.0.1 # lyrics feedparser==6.0.8 # rss feed (news) discord-py-slash-command==2.0.1 # slash commands +python_dotenv==0.19.0 # using .env file diff --git a/src/cogs/internet.py b/src/cogs/internet.py index 52c2cb2..8f7acb0 100644 --- a/src/cogs/internet.py +++ b/src/cogs/internet.py @@ -1,11 +1,10 @@ import discord from feedparser import parse -from os import environ from discord.ext import commands from random import choice from asyncpraw import Reddit from discord_slash import cog_ext -from utils.core import randomImage, isSlash, mySendHidden +from utils.core import randomImage, isSlash, mySendHidden, load def setup(client): client.add_cog(Internet(client)) @@ -14,6 +13,7 @@ class Internet(commands.Cog): """Commandes relatives à ce qui provient d'internet.""" def __init__(self, client): self.client = client + self.keys = load(["TOKEN_REDDIT_CLIENT_ID", "TOKEN_REDDIT_CLIENT_SECRET", "TOKEN_REDDIT_USER_AGENT"]) @commands.command(name='memes', aliases = ['meme']) async def _memes(self, ctx, *args): @@ -32,7 +32,7 @@ class Internet(commands.Cog): return await mySendHidden(ctx, fromSlash, f"Désolé, tu demandes du nsfw... Fais plutôt **{ctx.prefix}sexe**.") try: - async with Reddit(client_id = environ['TOKEN_REDDIT_CLIENT_ID'], client_secret = environ['TOKEN_REDDIT_CLIENT_SECRET'], user_agent = f"disreddit /u/{environ['TOKEN_REDDIT_USER_AGENT']}, http://localhost:8080") as reddit: + async with Reddit(client_id = self.keys['TOKEN_REDDIT_CLIENT_ID'], client_secret = self.keys['TOKEN_REDDIT_CLIENT_SECRET'], user_agent = f"disreddit /u/{self.keys['TOKEN_REDDIT_USER_AGENT']}, http://localhost:8080") as reddit: subreddit = await reddit.subreddit(subredditchoix) # récupération du subreddit hot = subreddit.top(limit = 20) # récupération des memes avec une limite aux 10 premiers memes all_subs = [item async for item in hot] # liste des memes diff --git a/src/cogs/music.py b/src/cogs/music.py index 2320c8d..d7a75cd 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -19,10 +19,10 @@ from discord.ext import commands # Genius API from lyricsgenius import Genius -from os import environ -from utils.core import ligneFormatage, userOrNick +from utils.core import ligneFormatage, userOrNick, load from utils.time import nowCustom -genius = Genius(environ['TOKEN_GENIUS']) + +genius = Genius(load(["TOKEN_GENIUS"])["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 52f00c7..0591b55 100644 --- a/src/main.py +++ b/src/main.py @@ -1,11 +1,13 @@ print("Chargement des extensions & librairie...", end = " ") import discord -from os import environ, listdir +from os import listdir from discord_slash import SlashCommand from discord.ext import commands from utils.reminder import Reminder -customPrefix = environ['PREFIX'] +from utils.core import load +keys = load(["PREFIX", "TOKEN_DISCORD"]) +customPrefix = keys["PREFIX"] client = commands.Bot(command_prefix = customPrefix, case_insensitive = True, intents = discord.Intents.all()) slash = SlashCommand(client, sync_commands = True) @@ -54,4 +56,4 @@ async def on_message(message): await ctx.send(f">>> Coucou !\nMon préfix est `{prefix}` et ma commande d'aide est `{prefix}help`") print("Connexion à Discord...", end = " ") -client.run(environ['TOKEN_DISCORD']) +client.run(keys["TOKEN_DISCORD"]) diff --git a/src/utils/time.py b/src/utils/time.py index bc99ef6..a293d51 100644 --- a/src/utils/time.py +++ b/src/utils/time.py @@ -1,9 +1,9 @@ -from os import environ from pytz import timezone from datetime import datetime, timedelta from re import findall +from utils.core import load -myTimezone = environ['TIMEZONE'] +myTimezone = load(["TIMEZONE"])["TIMEZONE"] def stringTempsVersSecondes(time): """Convertis une durée rentrée par un utilisateur en string vers des secondes en int"""