-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmisc_cog.py
More file actions
71 lines (62 loc) · 2.61 KB
/
misc_cog.py
File metadata and controls
71 lines (62 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from typing import TYPE_CHECKING
import discord
from discord import app_commands
from discord.ext import commands
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
from env import DATABASE_URL
from utils import send_discord_message, setup_logging
if TYPE_CHECKING:
from ..bot import ClusterBot
logger = setup_logging()
class BotManagerCog(commands.Cog):
def __init__(self, bot: "ClusterBot"):
self.bot = bot
@app_commands.command(name="ping")
async def ping(self, interaction: discord.Interaction):
"""Simple ping command to check if the bot is responsive"""
await send_discord_message(interaction, "pong")
@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 send_discord_message(interaction, message)
return
try:
engine = create_engine(DATABASE_URL)
with engine.connect() as connection:
result = connection.execute(text("SELECT RANDOM()"))
row = result.fetchone()
if row:
random_value = row[0]
await send_discord_message(
interaction, f"Your lucky number is {random_value}."
)
else:
await send_discord_message(interaction, "No result returned.")
except SQLAlchemyError as e:
message = "Database error occurred"
logger.error(f"{message}: {str(e)}", exc_info=True)
await send_discord_message(interaction, f"{message}.")
except Exception as e:
message = "Error interacting with the database"
logger.error(f"{message}: {str(e)}", exc_info=True)
await send_discord_message(interaction, f"{message}.")
@app_commands.command(name="get-api-url")
async def get_api_url(self, interaction: discord.Interaction):
if not os.environ.get("HEROKU_APP_DEFAULT_DOMAIN_NAME"):
await send_discord_message(
interaction,
"No `HEROKU_APP_DEFAULT_DOMAIN_NAME` present,"
" are you sure you aren't running locally?",
ephemeral=True,
)
else:
await send_discord_message(
interaction,
f"API URL: `https://{os.environ['HEROKU_APP_DEFAULT_DOMAIN_NAME'].rstrip('/')}`",
ephemeral=True,
)