Skip to content

Commit

Permalink
Change config from .py to .env, add db backup script
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Aug 15, 2024
1 parent 17105f4 commit c5473b6
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ cython_debug/

# substiify custom
core/version.toml
config.py
.env
postgres-data

logs/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions core/config.py
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 0 additions & 7 deletions core/config.py.example

This file was deleted.

2 changes: 1 addition & 1 deletion database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
14 changes: 14 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions extensions/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion extensions/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
13 changes: 13 additions & 0 deletions resources/db_backup.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c5473b6

Please sign in to comment.