From c5473b6543e5fd296e20140a740994da103dd22a Mon Sep 17 00:00:00 2001 From: jackra1n <45038833+jackra1n@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:00:58 +0200 Subject: [PATCH] Change config from .py to .env, add db backup script --- .gitignore | 2 +- README.md | 4 ++-- core/bot.py | 6 +++--- core/config.py | 18 ++++++++++++++++++ core/config.py.example | 7 ------- database/__init__.py | 2 +- example.env | 14 ++++++++++++++ extensions/music.py | 4 ++-- extensions/owner.py | 2 +- main.py | 5 +++-- resources/db_backup.sh | 13 +++++++++++++ 11 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 core/config.py delete mode 100644 core/config.py.example create mode 100644 example.env create mode 100755 resources/db_backup.sh diff --git a/.gitignore b/.gitignore index 58c472d..25a851c 100644 --- a/.gitignore +++ b/.gitignore @@ -147,7 +147,7 @@ cython_debug/ # substiify custom core/version.toml -config.py +.env postgres-data logs/ diff --git a/README.md b/README.md index ff1f226..426dace 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ To run the bot you'll need docker compose. -- Copy or rename `config_example.py` in `/core` to `config.py` and fill out the fields. -- Start the postgres container and create a database which you configured in `config.py` +- Copy or rename `example.env` in `/` to `.env` and fill out the fields. +- Start the postgres container and create a database which you configured in `.env` - Build and start bot container Build docker image diff --git a/core/bot.py b/core/bot.py index 10166ea..7e99c71 100644 --- a/core/bot.py +++ b/core/bot.py @@ -3,8 +3,8 @@ import discord import wavelink -from discord.ext import commands from discord.app_commands import errors as slash_errors +from discord.ext import commands import core from database import Database @@ -19,7 +19,7 @@ def __init__(self, *, database: Database) -> None: self.start_time = datetime.datetime.now(datetime.timezone.utc) intents = discord.Intents().all() super().__init__( - command_prefix=commands.when_mentioned_or(core.config.PREFIX), + command_prefix=commands.when_mentioned_or(core.config.BOT_PREFIX), intents=intents, owner_id=276462585690193921, max_messages=3000, @@ -37,7 +37,7 @@ async def on_wavelink_node_ready(self, payload: wavelink.NodeReadyEventPayload) async def on_ready(self: commands.Bot) -> None: servers = len(self.guilds) - activity_name = f"{core.config.PREFIX}help | {servers} servers" + activity_name = f"{core.config.BOT_PREFIX}help | {servers} servers" activity = discord.Activity(type=discord.ActivityType.listening, name=activity_name) await self.change_presence(activity=activity) colored_name = f"\x1b[96m{self.user}\x1b[0m" diff --git a/core/config.py b/core/config.py new file mode 100644 index 0000000..0623700 --- /dev/null +++ b/core/config.py @@ -0,0 +1,18 @@ +import os + +from dotenv import load_dotenv + +load_dotenv() + +BOT_TOKEN = os.getenv("BOT_TOKEN") +BOT_PREFIX = os.getenv("BOT_PREFIX") + +DB_HOST = os.getenv("DB_HOST") +DB_PORT = os.getenv("DB_PORT") +DB_USER = os.getenv("DB_USER") +DB_PASSWORD = os.getenv("DB_PASSWORD") +DB_NAME = os.getenv("DB_NAME") +POSTGRES_DSN = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}" + +LAVALINK_NODE_URL = os.getenv("LAVALINK_NODE_URL") +LAVALINK_PASSWORD = os.getenv("LAVALINK_PASSWORD") diff --git a/core/config.py.example b/core/config.py.example deleted file mode 100644 index 422c2f1..0000000 --- a/core/config.py.example +++ /dev/null @@ -1,7 +0,0 @@ -TOKEN = "" -PREFIX = "" - -POSTGRESQL_DSN = "postgresql://[user]:[password]@localhost:5440/substiify" - -LAVALINK_NODE_URL = "http://localhost:2333/" -LAVALINK_PASSWORD = "youshallnotpass" diff --git a/database/__init__.py b/database/__init__.py index 82ef7fc..928d0bb 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -38,7 +38,7 @@ async def __aexit__(self, *args: Any) -> None: logger.info("Successfully closed Database connection.") async def setup(self) -> None: - pool: _Pool | None = await asyncpg.create_pool(dsn=core.config.POSTGRESQL_DSN) + pool: _Pool | None = await asyncpg.create_pool(dsn=core.config.POSTGRES_DSN) if pool is None: raise RuntimeError('Unable to intialise the Database, "create_pool" returned None.') diff --git a/example.env b/example.env new file mode 100644 index 0000000..5337b34 --- /dev/null +++ b/example.env @@ -0,0 +1,14 @@ +# Bot configuration +BOT_TOKEN="" +BOT_PREFIX="" + +# Database configuration +DB_HOST="localhost" +DB_PORT="5440" +DB_USER="" +DB_PASSWORD="" +DB_NAME="substiify" + +# Lavalink configuration +LAVALINK_NODE_URL="http://localhost:2333/" +LAVALINK_PASSWORD="youshallnotpass" diff --git a/extensions/music.py b/extensions/music.py index 20f7ebc..ffc85fa 100644 --- a/extensions/music.py +++ b/extensions/music.py @@ -230,8 +230,8 @@ async def lavalink_stats(self, ctx: commands.Context, full: bool = False): """ stats: wavelink.StatsResponsePayload = await wavelink.Pool.get_node().fetch_stats() info: wavelink.InfoResponsePayload = await wavelink.Pool.get_node().fetch_info() - - uptime_str = utils.seconds_to_human_readable(stats.uptime/1000) + + uptime_str = utils.seconds_to_human_readable(stats.uptime / 1000) memory_used = utils.bytes_to_human_readable(stats.memory.used) memory_free = utils.bytes_to_human_readable(stats.memory.reservable) system_load = round((stats.cpu.system_load * 100), 1) diff --git a/extensions/owner.py b/extensions/owner.py index e5dd1d7..f47585d 100644 --- a/extensions/owner.py +++ b/extensions/owner.py @@ -28,7 +28,7 @@ def __init__(self, bot: core.Substiify): async def set_default_status(self): if self.bot.is_ready(): servers = len(self.bot.guilds) - activity_name = f"{core.config.PREFIX}help | {servers} servers" + activity_name = f"{core.config.BOT_PREFIX}help | {servers} servers" activity = Activity(type=ActivityType.listening, name=activity_name) await self.bot.change_presence(activity=activity) diff --git a/main.py b/main.py index 8717b14..ea02eb1 100644 --- a/main.py +++ b/main.py @@ -9,13 +9,14 @@ from core.custom_logger import CustomLogFormatter, RemoveNoise discord.utils.setup_logging(formatter=CustomLogFormatter(), level=20) -logging.getLogger('discord.gateway').addFilter(RemoveNoise()) +logging.getLogger("discord.gateway").addFilter(RemoveNoise()) + async def main() -> None: utils.ux.print_system_info() async with database.Database() as db, core.Substiify(database=db) as substiify: - await substiify.start(core.config.TOKEN) + await substiify.start(core.config.BOT_TOKEN) if __name__ == "__main__": diff --git a/resources/db_backup.sh b/resources/db_backup.sh new file mode 100755 index 0000000..bfa80ef --- /dev/null +++ b/resources/db_backup.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +source ../.env + +TIMESTAMP=$(date +\%Y\%m\%d\%H\%M\%S) +BACKUP_DIR="/mnt/backups/substiify" +BACKUP_FILE="$BACKUP_DIR/${DB_NAME}_backup_$TIMESTAMP.sql" + +# Create backup directory +mkdir -p $BACKUP_DIR + +# Perform the backup using pg_dump +PGPASSWORD="$DB_PASSWORD" pg_dump -h $DB_HOST -p $DB_PORT -U $DB_USER -F c -b -v -f $BACKUP_FILE $DB_NAME