Skip to content

Commit

Permalink
add /verifydb command
Browse files Browse the repository at this point in the history
  • Loading branch information
b9r5 committed Dec 8, 2024
1 parent 5ba723e commit 10121b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/discord-cluster-manager/cogs/misc_cog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import discord
from discord import app_commands
from discord.ext import commands
import psycopg2
from utils import setup_logging
from consts import DATABASE_URL

logger = setup_logging()

Expand Down Expand Up @@ -36,3 +38,27 @@ async def resync(self, interaction: discord.Interaction):
await interaction.response.send_message(
"You need administrator permissions to use this command"
)

@app_commands.command(name="verifydb")
async def verify_db(self, interaction: discord.Interaction):
"""Command to verify database connectivity"""
if not DATABASE_URL:
message = "DATABASE_URL not set."
logger.error(message)
await interaction.response.send_message(message)
return

try:
with psycopg2.connect(DATABASE_URL, sslmode='require') as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT RANDOM()")
result = cursor.fetchone()
if result:
random_value = result[0]
await interaction.response.send_message(f"Your lucky number is {random_value}.")
else:
await interaction.response.send_message("No result returned.")
except Exception as e:
message = "Error interacting with the database"
logger.error(f"{message}: {str(e)}", exc_info=True)
await interaction.response.send_message(f"{message}.")
1 change: 1 addition & 0 deletions src/discord-cluster-manager/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ class ModalGPU(Enum):
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
DATABASE_URL = os.getenv("DATABASE_URL")

0 comments on commit 10121b9

Please sign in to comment.