Skip to content

Instantly share code, notes, and snippets.

@andyshinn
Created September 28, 2024 18:17
Show Gist options
  • Save andyshinn/d75c9e87789a43aae6d9ebe2fb9146d4 to your computer and use it in GitHub Desktop.
Save andyshinn/d75c9e87789a43aae6d9ebe2fb9146d4 to your computer and use it in GitHub Desktop.
import os
from discord.ext import commands
from discord import Intents
from bridger.log import logger
try:
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
DISCORD_BOT_OWNER_ID = int(os.getenv("DISCORD_BOT_OWNER_ID"))
except TypeError as e:
logger.error(f"Failed to load environment variable {e}")
exit(1)
class BridgerBot(commands.Bot):
def __init__(self, **kwargs):
super().__init__(command_prefix="./bridger ", **kwargs)
self.initial_extensions = [
"bridger.cogs.db"
]
async def setup_hook(self):
for ext in self.initial_extensions:
await self.load_extension(ext)
intents = Intents.default()
intents.message_content = True
bot = BridgerBot(
intents=intents,
owner_id=DISCORD_BOT_OWNER_ID,
)
bot.run(DISCORD_BOT_TOKEN)
from discord.ext import commands
from discord import app_commands, Object
from bridger.db import db, get_tables
from bridger.log import logger
MY_GUILDS = [
Object(id=1253788608582914231) # andyshinn Test Server
]
class DatabaseCog(commands.GroupCog, name="bridger-schema"):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="create-tables", description="Initialize the database tables")
async def create_tables(self, ctx):
try:
tables = get_tables()
db.create_tables(tables)
await ctx.send(f"Tables created: {tables}")
except Exception as e:
await ctx.send(f"Error creating tables: {e}")
@commands.command(name="sync-commands", description="Sync the commands with the database")
async def sync_commands(self, ctx):
try:
self.bot.tree.copy_global_to(guild=MY_GUILDS[0])
await ctx.send(f"Commands synced to: {MY_GUILDS[0]}")
except Exception as e:
await ctx.send(f"Error syncing commands: {e}")
logger.exception(e)
await logger.complete()
# List all slash commands
@commands.command(name="list-commands", description="List all slash commands")
async def list_commands(self, ctx):
try:
commands = self.bot.tree.walk_commands(guild=MY_GUILDS[0])
await ctx.send(f"Commands: {list(commands)}")
except Exception as e:
await ctx.send(f"Error listing commands: {e}")
async def setup(bot):
await bot.add_cog(DatabaseCog(bot), guilds=MY_GUILDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment