Skip to content

Commit

Permalink
errors handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Drago-03 committed Dec 3, 2024
1 parent edb4299 commit 55acd35
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
23 changes: 4 additions & 19 deletions cogs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ def __init__(self, bot):

def create_embed(self, title: str, description: str = None, error: bool = False) -> discord.Embed:
"""Create a branded embed"""
embed = discord.Embed(
title=title,
description=description,
color=discord.Color.red() if error else EMBED_COLOR
)
embed.set_footer(
text=AUTHOR_NAME,
icon_url=AUTHOR_ICON
)
color = discord.Color.red() if error else discord.Color.blue()
embed = discord.Embed(title=title, description=description, color=color)
return embed

async def send_error(self, ctx, message: str):
Expand Down Expand Up @@ -56,16 +49,8 @@ async def send_success(self, ctx, message: str):
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
"""Handle errors globally"""
if isinstance(error, commands.MissingPermissions):
await self.send_permission_error(ctx)
elif isinstance(error, commands.CommandOnCooldown):
await self.send_cooldown_error(ctx, error.retry_after)
elif isinstance(error, commands.MissingRequiredArgument):
await self.send_error(ctx, f"you're missing a required argument: {error.param.name}")
elif isinstance(error, commands.CommandNotFound):
await self.send_error(ctx, "the command you tried to use does not exist")
else:
await self.send_error(ctx, "an unexpected error occurred")
# Remove this listener to prevent duplicate errors
pass

@commands.Cog.listener()
async def on_message(self, message):
Expand Down
31 changes: 24 additions & 7 deletions cogs/general.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import random
import discord
from discord.ext import commands
from discord import app_commands
from discord.ui import Button, View
import random

SUPPORT_SERVER_LINK = "https://discord.gg/your-support-server-link"
from config import SUPPORT_SERVER_LINK, BOT_WEBSITE

class ProfileView(View):
def __init__(self, member):
Expand Down Expand Up @@ -42,6 +41,8 @@ async def server_banner(self, interaction: discord.Interaction, button: discord.
await interaction.response.send_message("This server does not have a banner set.", ephemeral=True)

class General(commands.Cog):
"""General bot commands"""

def __init__(self, bot):
self.bot = bot

Expand All @@ -64,7 +65,7 @@ async def about_command(self, ctx):
color=discord.Color.blue()
)
embed.add_field(name="Author", value="Drago", inline=False)
embed.add_field(name="Website", value="[IndieGO Website](https://drago-03.github.io/IndieGo-Website/)", inline=False)
embed.add_field(name="Website", value=f"[IndieGO Website]({BOT_WEBSITE})", inline=False)
embed.set_footer(text="Thank you for using IndieGO Bot!")
await ctx.send(embed=embed)

Expand All @@ -77,7 +78,7 @@ async def about(self, interaction: discord.Interaction):
color=discord.Color.blue()
)
embed.add_field(name="Author", value="Drago", inline=False)
embed.add_field(name="Website", value="[IndieGO Website](https://drago-03.github.io/IndieGo-Website/)", inline=False)
embed.add_field(name="Website", value=f"[IndieGO Website]({BOT_WEBSITE})", inline=False)
embed.set_footer(text="Thank you for using IndieGO Bot!")
await interaction.response.send_message(embed=embed)

Expand Down Expand Up @@ -113,6 +114,8 @@ async def serverinfo_command(self, ctx):
embed.add_field(name="Region", value=guild.region, inline=False)
embed.add_field(name="Members", value=guild.member_count, inline=False)
embed.add_field(name="Created At", value=guild.created_at.strftime("%Y-%m-%d %H:%M:%S"), inline=False)
if guild.icon:
embed.set_thumbnail(url=guild.icon.url)
await ctx.send(embed=embed)

@app_commands.command(name="serverinfo", description="Show information about the server")
Expand All @@ -129,6 +132,8 @@ async def serverinfo(self, interaction: discord.Interaction):
embed.add_field(name="Region", value=guild.region, inline=False)
embed.add_field(name="Members", value=guild.member_count, inline=False)
embed.add_field(name="Created At", value=guild.created_at.strftime("%Y-%m-%d %H:%M:%S"), inline=False)
if guild.icon:
embed.set_thumbnail(url=guild.icon.url)
await interaction.response.send_message(embed=embed)

@commands.command(name="support")
Expand Down Expand Up @@ -157,15 +162,27 @@ async def profile_command(self, ctx, member: discord.Member = None):
if member is None:
member = ctx.author
view = ProfileView(member)
await ctx.send("Choose an option:", view=view)
embed = discord.Embed(
title=f"Profile - {member.name}",
description="Click the buttons below to view different profile aspects.",
color=discord.Color.blue()
)
embed.set_thumbnail(url=member.avatar.url)
await ctx.send(embed=embed, view=view)

@app_commands.command(name="profile", description="Show profile options for the user")
async def profile(self, interaction: discord.Interaction, member: discord.Member = None):
"""Show profile options for the user"""
if member is None:
member = interaction.user
view = ProfileView(member)
await interaction.response.send_message("Choose an option:", view=view)
embed = discord.Embed(
title=f"Profile - {member.name}",
description="Click the buttons below to view different profile aspects.",
color=discord.Color.blue()
)
embed.set_thumbnail(url=member.avatar.url)
await interaction.response.send_message(embed=embed, view=view)

async def setup(bot):
await bot.add_cog(General(bot))
22 changes: 17 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@
ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
PREFIX = '.'
TICKET_CATEGORY_ID = int(os.getenv('TICKET_CATEGORY_ID', '0'))
STAFF_ROLE_ID = int(os.getenv('STAFF_ROLE_ID', '0'))
#TICKET_CATEGORY_ID = int(os.getenv('TICKET_CATEGORY_ID', '0'))
#STAFF_ROLE_ID = int(os.getenv('STAFF_ROLE_ID', '0'))
LOG_CHANNEL_ID = int(os.getenv('LOG_CHANNEL_ID', '1308525133048188949'))

# URLs and Endpoints
BOT_WEBSITE = "https://drago-03.github.io/IndieGo-Website/"
SUPPORT_SERVER_LINK = "https://discord.gg/9bPsjgnJ5v"
INSTALL_URL = "https://discord.com/oauth2/authorize?client_id=1304755116255088670&permissions=8&scope=bot%20applications.commands"
OAUTH2_URL = f"https://discord.com/oauth2/authorize?client_id=1304755116255088670&redirect_uri={BOT_WEBSITE}/callback&response_type=code&scope=identify%20guilds"

# API Endpoints
INTERACTIONS_URL = f"{BOT_WEBSITE}/api/interactions"
LINKED_ROLES_URL = f"{BOT_WEBSITE}/api/linked-roles"
TERMS_URL = f"{BOT_WEBSITE}/terms"
PRIVACY_URL = f"{BOT_WEBSITE}/privacy"
INSTALL_URL = "https://discord.com/oauth2/authorize?client_id=1304755116255088670"
OAUTH2_URL = f"https://discord.com/api/oauth2/authorize?client_id=1304755116255088670&redirect_uri={BOT_WEBSITE}/callback&response_type=code&scope=identify%20guilds"

# Branding
EMBED_COLOR = 0x9F7AEA # Purple
AUTHOR_NAME = "Built by Drago"
AUTHOR_ICON = "https://your-domain.com/assets/drago-icon.png"
AUTHOR_ICON = f"{BOT_WEBSITE}/assets/logo.png"

# Command Settings
COOLDOWN_TIME = 3 # seconds
MAX_WARNINGS = 3
DEFAULT_DELETE_DAYS = 7

# Logging
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'
LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LOG_FILE = 'bot.log'
22 changes: 11 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,34 @@ def __init__(self):
)
self.initial_extensions = [
'cogs.errors', # Load error handler first
'cogs.general',
'cogs.general', # Load general first after errors
'cogs.moderation',
'cogs.fun',
'cogs.admin',
'cogs.tickets',
'cogs.logging',
'cogs.ai_assistant',
'cogs.coding_help',
'cogs.help',
'cogs.base',
'cogs.automod',
'cogs.dm_interaction',
'cogs.voice_channel',
'cogs.ocr',
'cogs.reddit',
'cogs.help' # Load help cog last
'cogs.reddit'
]

async def setup_hook(self):
logger.info('Starting bot setup...')

# Sync commands on startup
try:
synced = await self.tree.sync()
logger.info(f'Synced {len(synced)} command(s)')
except Exception as e:
logger.error(f'Failed to sync commands: {str(e)}')
logger.error(traceback.format_exc())

# Load all cogs
for extension in self.initial_extensions:
try:
Expand All @@ -62,14 +70,6 @@ async def setup_hook(self):
logger.error(f'Failed to load extension {extension}: {str(e)}')
logger.error(traceback.format_exc())

# Sync commands
try:
synced = await self.tree.sync()
logger.info(f'Synced {len(synced)} command(s)')
except Exception as e:
logger.error(f'Failed to sync commands: {str(e)}')
logger.error(traceback.format_exc())

async def on_ready(self):
logger.info(f'Logged in as {self.user} (ID: {self.user.id})')
logger.info('------')
Expand Down

0 comments on commit 55acd35

Please sign in to comment.