49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import discord
|
|
from discord.ext import commands
|
|
from discord import app_commands
|
|
|
|
class DBTest(commands.Cog):
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
|
|
@app_commands.command(name="test_db", description="Tests the database connection")
|
|
async def test_db(self, interaction: discord.Interaction):
|
|
await interaction.response.defer(ephemeral=True)
|
|
|
|
try:
|
|
db = self.bot.database.conn
|
|
|
|
async with db.execute("SELECT 1;") as cursor:
|
|
result = await cursor.fetchone()
|
|
|
|
if result:
|
|
await interaction.followup.send("Database connection successful!")
|
|
else:
|
|
await interaction.followup.send("Database responded, but returned no data.")
|
|
|
|
|
|
except Exception as e:
|
|
await interaction.followup.send(f"Database connection failed: {e}")
|
|
|
|
@app_commands.command(name="test_db_add", description="Add a string to the Database")
|
|
@app_commands.describe(message="a message")
|
|
async def test_db(self, interaction: discord.Interaction, message: str):
|
|
await interaction.response.defer(ephemeral=True)
|
|
|
|
try:
|
|
db = self.bot.database.conn
|
|
|
|
await db.execute(
|
|
"INSERT INTO test (message) VALUES (?)",
|
|
(message,)
|
|
)
|
|
await db.commit()
|
|
|
|
await interaction.followup.send("Database INSERT was successful!")
|
|
|
|
|
|
except Exception as e:
|
|
await interaction.followup.send(f"Database INSERT failed: {e}")
|
|
|
|
async def setup(bot: commands.Bot):
|
|
await bot.add_cog(DBTest(bot)) |