Files
Homelab/discord-agent/cogs/base_cog.py
Jamie Miller e883671d63 Add discord-agent files and enable reply notifications
- Add discord_agent.py with reply() instead of send() for user notifications
- Add Discord bot Dockerfile and requirements.txt
- Add bot cogs (base_cog.py and integration_cog.py)
- Update .gitignore to track discord-agent directory
- Bot now replies to messages triggering notifications for users
2026-02-06 04:47:43 +00:00

57 lines
2.2 KiB
Python

"""Discord Agent Base Cog Template
This provides a base class for all integration cogs with common functionality.
"""
import discord
from discord.ext import commands
import logging
from typing import Optional
logger = logging.getLogger(__name__)
class BaseCog(commands.Cog):
"""Base cog class with common functionality for all integration cogs."""
def __init__(self, bot):
self.bot = bot
self.config = bot.config
self.session = getattr(bot, 'session', None)
@commands.Cog.listener()
async def on_ready(self):
"""Called when the cog is ready."""
logger.info(f"{self.qualified_name} cog ready")
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
"""Handle command errors."""
if isinstance(error, commands.CommandNotFound):
return # Ignore command not found errors
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"❌ Missing required argument: {error.param.name}")
elif isinstance(error, commands.BadArgument):
await ctx.send(f"❌ Invalid argument provided")
else:
logger.error(f"Command error in {ctx.command}: {error}", exc_info=error)
await ctx.send("❌ An error occurred while processing your command")
def create_embed(self, title: str, description: str = None,
color: discord.Color = discord.Color.blue()) -> discord.Embed:
"""Create a standard embed with consistent styling."""
embed = discord.Embed(title=title, description=description, color=color)
embed.set_footer(text=f"Requested by {self.bot.user.name}")
return embed
async def check_integration_enabled(self, ctx, integration_name: str) -> bool:
"""Check if an integration is enabled."""
integrations = self.config.get('integrations', {}).get('enabled', [])
if integration_name not in integrations:
await ctx.send(f"{integration_name.title()} integration is not enabled.")
return False
return True
async def setup(bot):
"""Setup function for loading the cog."""
await bot.add_cog(BaseCog(bot))