diff --git a/.gitignore b/.gitignore index df1d76e..5b5d11d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,9 @@ -files/status.txt -files/readyMessage.txt -files/client.txt files/lastTasks.json files/c4.json files/hangman.json files/stats.json files/lost.json files/locked.json -files/database.json files/ufora_notifications.json .idea/ __pycache__ diff --git a/cogs/events.py b/cogs/events.py index 7cea495..f8301db 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -5,6 +5,7 @@ from discord.ext import commands from functions import checks, easterEggResponses from functions.database import stats, muttn, custom_commands import pytz +from settings import READY_MESSAGE, SANDBOX, STATUS_MESSAGE import time import traceback @@ -30,20 +31,13 @@ class Events(commands.Cog): """ Function called when the bot is ready & done leading. """ - # Change status - with open("files/status.txt", "r") as statusFile: - status = statusFile.readline() + # Set status + await self.client.change_presence(status=discord.Status.online, activity=discord.Game(STATUS_MESSAGE)) - await self.client.change_presence(status=discord.Status.online, activity=discord.Game(str(status))) - - # Print a message in the terminal to show that he's ready - with open("files/readyMessage.txt", "r") as readyFile: - readyMessage = readyFile.readline() - - print(readyMessage) + print(READY_MESSAGE) # Add constants to the client as a botvar - self.client.constants = constants.Live if "zandbak" not in readyMessage else constants.Zandbak + self.client.constants = constants.Live if SANDBOX else constants.Zandbak @commands.Cog.listener() async def on_message(self, message): diff --git a/cogs/tasks.py b/cogs/tasks.py index 4ac974d..2c4818a 100644 --- a/cogs/tasks.py +++ b/cogs/tasks.py @@ -246,7 +246,7 @@ class Tasks(commands.Cog): Task that checks for new Ufora announcements every few minutes """ # Don't run this when testing - if self.client.user.id == int(constants.coolerDidierId): + if self.client.user.id != int(constants.didierId): return # Get new notifications diff --git a/didier.py b/didier.py index e2b4b18..551161e 100644 --- a/didier.py +++ b/didier.py @@ -1,45 +1,8 @@ import discord -from discord.ext import commands, ipc from dotenv import load_dotenv from functions.prefixes import get_prefix -import os - - -class Didier(commands.Bot): - """ - Main Bot class for Didier - """ - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - # IPC Server - # TODO secret key - self.ipc = ipc.Server(self, secret_key="SOME_SECRET_KEY") - - # Cogs that should be loaded before the others - self.preload = ("ipc", "utils", "failedchecks", "events",) - - # Remove default help command - self.remove_command("help") - - self.init_extensions() - - def init_extensions(self): - # Load initial extensions - for ext in self.preload: - self.load_extension(f"cogs.{ext}") - - # Load all remaining cogs - for file in os.listdir("./cogs"): - if file.endswith(".py") and not (file.startswith(self.preload)): - self.load_extension("cogs.{}".format(file[:-3])) - - async def on_ipc_ready(self): - print("IPC server is ready.") - - async def on_ipc_error(self, endpoint, error): - print(endpoint, "raised", error) - +from settings import TOKEN +from startup.didier import Didier if __name__ == "__main__": load_dotenv(verbose=True) @@ -50,9 +13,7 @@ if __name__ == "__main__": client = Didier(command_prefix=get_prefix, case_insensitive=True, intents=intents) - # Get the token out of the file & run the bot - with open("files/client.txt", "r") as fp: - token = fp.readline() + if client.ipc is not None: + client.ipc.start() - client.ipc.start() - client.run(token) + client.run(TOKEN) diff --git a/functions/database/utils.py b/functions/database/utils.py index 828e198..d4c7b32 100644 --- a/functions/database/utils.py +++ b/functions/database/utils.py @@ -1,6 +1,5 @@ import psycopg2 -import json -import os +from settings import DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD connection = None @@ -17,15 +16,11 @@ def connect(): def create_connection(): global connection - dir_path = os.path.dirname(os.path.realpath(__file__)) - with open(os.path.join(dir_path, "../../files/database.json"), "r") as fp: - db = json.load(fp) - connection = psycopg2.connect( - host=db["host"], - database=db["database"], - user=db["username"], - password=db["password"] + host=DB_HOST, + database=DB_NAME, + user=DB_USERNAME, + password=DB_PASSWORD ) diff --git a/ignored.md b/ignored.md index 9ce1bdf..ce73183 100644 --- a/ignored.md +++ b/ignored.md @@ -2,28 +2,7 @@ A list of all ignored files with copy-pastable templates. Useful for when you want to work on commands that use these, for obvious reasons. Every file has a copy-pastable template to make it easy for you to use. -These are usually files which would be overkill to make a PSQL table for. Other possibilities are files that are never edited, but should be different on every machine (Discord token, status message, ...). - -### files/client.txt - -Contains the application's token to connect to Discord. You can create your own bot & put it's token in this file to run & test Didier code. - - token_goes_here - -### files/database.json - -Contains the credentials needed to connect to the PSQL database. This is ignored so that I don't have to leak my IP address, but also so that you can set up a local database to mess around without affecting the Live one or having to change any code. - -```json -{ - "username": "username", - "password": "password", - "host": "host_address", - "database": "database_name" -} -``` - -When connecting to a local PSQL database, `host` should be `"localhost"`. +These are usually files which would be overkill to make a PSQL table for. Other possibilities are files that are never edited, but should be different on every machine. ### files/hangman.json @@ -64,14 +43,6 @@ Contains a boolean indicating whether or not the server is currently locked, and } ``` -### files/readyMessage.txt - -Contains the message printed in your terminal when Didier is ready. - - I'M READY I'M READY I'M READY I'M READY - -In case you were wondering: yes, this is a Spongebob reference. - ### files/stats.json Contains the stats to track for gambling games. Weren't made as a PSQL table because they would be too long (and every game is different). @@ -98,12 +69,6 @@ Contains the stats to track for gambling games. Weren't made as a PSQL table bec } ``` -### files/status.txt - -Contains Didier's status message for when he logs in. Keep in mind that his activity is set to `Playing `. This was first used in Didier V1 to show whether or not he was in sandbox mode. - - with your Didier Dinks. - ### files/ufora_notifications.json Stores ID's of all received Ufora notifications. diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..b64e27c --- /dev/null +++ b/settings.py @@ -0,0 +1,33 @@ +from dotenv import load_dotenv +import os + + +load_dotenv() + + +def _to_bool(value: str) -> bool: + """ + Env variables are strings so this converts them to booleans + """ + return value.lower() in ["true", "1", "y", "yes"] + + +# Sandbox or live +SANDBOX = _to_bool(os.getenv("SANDBOX", "true")) + +# Tokens & API keys +URBANDICTIONARY = os.getenv("URBANDICTIONARY", "") +IMGFLIP_NAME = os.getenv("IMGFLIPNAME", "") +IMGFLIP_PASSWORD = os.getenv("IMGFLIPPASSWORD", "") + +# Database credentials +DB_USERNAME = os.getenv("DBUSERNAME", "") +DB_PASSWORD = os.getenv("DBPASSWORD", "") +DB_HOST = os.getenv("DBHOST", "") +DB_NAME = os.getenv("DBNAME", "") + +# Discord-related +TOKEN = os.getenv("TOKEN", "") +HOST_IPC = _to_bool(os.getenv("HOSTIPC", "false")) +READY_MESSAGE = os.getenv("READYMESSAGE", "I'M READY I'M READY I'M READY I'M READY") # Yes, this is a Spongebob reference +STATUS_MESSAGE = os.getenv("STATUSMESSAGE", "with your Didier Dinks.") diff --git a/startup/__init__.py b/startup/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/startup/didier.py b/startup/didier.py new file mode 100644 index 0000000..c9d545d --- /dev/null +++ b/startup/didier.py @@ -0,0 +1,41 @@ +from discord.ext import commands, ipc +from settings import HOST_IPC +import os + + +class Didier(commands.Bot): + """ + Main Bot class for Didier + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self._host_ipc = HOST_IPC + + # IPC Server + # TODO secret key + self.ipc = ipc.Server(self, secret_key="SOME_SECRET_KEY") if self._host_ipc else None + + # Cogs that should be loaded before the others + self._preload = ("ipc", "utils", "failedchecks", "events",) + + # Remove default help command + self.remove_command("help") + + self.init_extensions() + + def init_extensions(self): + # Load initial extensions + for ext in self._preload: + self.load_extension(f"cogs.{ext}") + + # Load all remaining cogs + for file in os.listdir("./cogs"): + if file.endswith(".py") and not (file.startswith(self._preload)): + self.load_extension("cogs.{}".format(file[:-3])) + + async def on_ipc_ready(self): + print("IPC server is ready.") + + async def on_ipc_error(self, endpoint, error): + print(endpoint, "raised", error) diff --git a/startup/init_files.py b/startup/init_files.py new file mode 100644 index 0000000..eeecc55 --- /dev/null +++ b/startup/init_files.py @@ -0,0 +1,5 @@ +from os import path + + +def check_all(): + pass