The mdaddon class is the foundation for creating global addons that extend McDis-RCON’s functionality across all processes, independent of any specific server or network.
from mcdis_rcon.classes import McDisClientimport discordimport asyncioclass mdaddon: def __init__(self, client: McDisClient): """Called when addon is loaded""" self.client = client # Initialize your variables here async def on_message(self, message: discord.Message): """Called for every Discord message""" pass async def on_ready(self): """Called when bot connects to Discord""" pass def unload(self): """Called when addon is unloaded (optional)""" pass
async def on_message(self, message: discord.Message): """Handle Discord messages globally""" if message.author.bot: return if message.content == "!status": running = [p.name for p in self.client.processes if p.is_running()] stopped = [p.name for p in self.client.processes if not p.is_running()] status = f"**Status:**\n" status += f"🟢 Running: {', '.join(running) or 'None'}\n" status += f"🔴 Stopped: {', '.join(stopped) or 'None'}" await message.channel.send(status)
on_message is async and works even when no processes are running.
async def on_member_join(self, member: discord.Member): """Called when a member joins the Discord""" await self.client.panel.send( f"👋 Welcome {member.mention} to the server!" )async def on_reaction_add(self, reaction, user): """Called when a reaction is added""" if user.bot: return if reaction.emoji == "📌": await reaction.message.pin()async def on_voice_state_update(self, member, before, after): """Called when voice state changes""" if before.channel is None and after.channel is not None: print(f"{member.name} joined voice: {after.channel.name}")
def unload(self): """Cleanup resources""" # Cancel background tasks if hasattr(self, 'monitor_task'): self.monitor_task.cancel() # Save data self.save_config() # Close connections if hasattr(self, 'session'): asyncio.create_task(self.session.close()) print("Addon unloaded")
Always implement unload() to clean up tasks, files, and connections.
1. McDis-RCON starts ↓2. Addon __init__() called ↓3. Discord bot connects ↓4. Addon on_ready() called ↓5. on_message() called for each message Other events triggered as they occur ↓6. !!adreload command OR McDis-RCON exits ↓7. Addon unload() called
class mdaddon: def __init__(self, client: McDisClient): self.client = client async def on_message(self, message: discord.Message): if message.content == "!restart-all": for process in self.client.processes: if process.is_running(): await process.restart() await message.channel.send("✔ All processes restarted")