Skip to content

Commit

Permalink
[SlashSync] Cog model restructure
Browse files Browse the repository at this point in the history
- Restructure `SlashSync` according to SFUAnime/Ren#608. No functional
  changes.
- Remove module docstring for `slashsync.py` as it is already included
  in `__init__.py`.
  • Loading branch information
quachtridat committed Jan 26, 2023
1 parent 5d6f33c commit 25108f8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
11 changes: 11 additions & 0 deletions slashsync/commandHandlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from redbot.core import checks, commands
from redbot.core.commands import Context

from .commandsCore import SlashSyncCommandsCore


class SlashSyncCommandHandlers(SlashSyncCommandsCore):
@commands.command(name="sync")
@checks.is_owner()
async def _cmdSync(self, ctx: Context):
await self.cmdSync(ctx)
11 changes: 11 additions & 0 deletions slashsync/commandsCore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from redbot.core.commands.context import Context
from redbot.core.utils.chat_formatting import info, success

from .core import SlashSyncCore


class SlashSyncCommandsCore(SlashSyncCore):
async def cmdSync(self, ctx: Context):
msg = await ctx.send(info("Syncing slash commands to Discord, please wait..."))
await self.bot.tree.sync()
await msg.edit(content=success("Synchronized slash commands to Discord."))
21 changes: 21 additions & 0 deletions slashsync/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging

from redbot.core.bot import Red


class SlashSyncCore:
def __init__(self, bot: Red):
self.bot = bot
self.logger = logging.getLogger("red.luicogs.SlashSync")
self.bgTask = self.bot.loop.create_task(self.waitUntilBotReady())

async def waitUntilBotReady(self):
self.logger.debug("Waiting for bot to be ready")
await self.bot.wait_until_red_ready()
self.logger.debug("Bot is ready, synchronizing command tree")
await self.bot.tree.sync()
self.logger.debug("Command tree synchronized")

def cog_unload(self):
self.logger.debug("Clean up background task")
self.bgTask.cancel()
37 changes: 3 additions & 34 deletions slashsync/slashsync.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
"""SlashSync
from redbot.core import commands

Sync slash commands from the bot to the server.
"""
import logging
from .commandHandlers import SlashSyncCommandHandlers

from redbot.core import checks, commands
from redbot.core.bot import Red
from redbot.core.commands.context import Context
from redbot.core.utils.chat_formatting import info, success


class SlashSync(commands.Cog):
class SlashSync(commands.Cog, SlashSyncCommandHandlers):
"""Synchronize slash commands on the bot to Discord"""

def __init__(self, bot: Red):
self.bot = bot
self.logger = logging.getLogger("red.luicogs.SlashSync")
self.bgTask = self.bot.loop.create_task(self.waitUntilBotReady())

async def waitUntilBotReady(self):
self.logger.debug("Waiting for bot to be ready")
await self.bot.wait_until_red_ready()
self.logger.debug("Bot is ready, synchronizing command tree")
await self.bot.tree.sync()
self.logger.debug("Command tree synchronized")

def cog_unload(self):
self.logger.debug("Clean up background task")
self.bgTask.cancel()

@commands.command(name="sync")
@checks.is_owner()
async def sync(self, ctx: Context):
"""Synchronize slash commands to Discord"""
msg = await ctx.send(info("Syncing slash commands to Discord, please wait..."))
await self.bot.tree.sync()
await msg.edit(content=success("Synchronized slash commands to Discord."))

0 comments on commit 25108f8

Please sign in to comment.