From 85c5b26fdf3b1d4f73745f3de13ed2c82923a79a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Sep 2022 10:21:28 +0200 Subject: [PATCH] Added slash command support --- src/cpl_discord/service/discord_bot_service.py | 3 +++ src/cpl_discord/service/discord_service.py | 2 +- .../custom/discord/src/modules/hello_world/ping_command.py | 2 +- .../custom/discord/src/modules/hello_world/purge_command.py | 5 ++++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cpl_discord/service/discord_bot_service.py b/src/cpl_discord/service/discord_bot_service.py index aa4ac44c..07682187 100644 --- a/src/cpl_discord/service/discord_bot_service.py +++ b/src/cpl_discord/service/discord_bot_service.py @@ -72,5 +72,8 @@ class DiscordBotService(DiscordBotServiceABC): Console.banner(self._env.application_name if self._env.application_name != '' else 'A bot') await self._discord_service.init(self) + await self.wait_until_ready() + await self.tree.sync() + self._logger.debug(__name__, f'Finished syncing commands') await self._discord_service.on_ready() diff --git a/src/cpl_discord/service/discord_service.py b/src/cpl_discord/service/discord_service.py index 89b1951e..ef047789 100644 --- a/src/cpl_discord/service/discord_service.py +++ b/src/cpl_discord/service/discord_service.py @@ -3,7 +3,7 @@ from typing import Optional, Sequence, Union, Type import discord from discord.ext import commands -from discord.ext.commands import Context, CommandError, Cog +from discord.ext.commands import Context, CommandError, Cog, Command from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging import LoggerABC diff --git a/src/tests/custom/discord/src/modules/hello_world/ping_command.py b/src/tests/custom/discord/src/modules/hello_world/ping_command.py index 16af3887..8d2468b7 100644 --- a/src/tests/custom/discord/src/modules/hello_world/ping_command.py +++ b/src/tests/custom/discord/src/modules/hello_world/ping_command.py @@ -20,7 +20,7 @@ class PingCommand(DiscordCommandABC): self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - @commands.command() + @commands.hybrid_command() async def ping(self, ctx: Context): self._logger.debug(__name__, f'Received command ping {ctx}') self._logger.info(__name__, f'Bot name {self._bot.user.name}') diff --git a/src/tests/custom/discord/src/modules/hello_world/purge_command.py b/src/tests/custom/discord/src/modules/hello_world/purge_command.py index 0a36f379..037543f5 100644 --- a/src/tests/custom/discord/src/modules/hello_world/purge_command.py +++ b/src/tests/custom/discord/src/modules/hello_world/purge_command.py @@ -20,9 +20,12 @@ class PurgeCommand(DiscordCommandABC): self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - @commands.command() + @commands.hybrid_command() async def purge(self, ctx: Context): self._logger.debug(__name__, f'Received command ping {ctx}') self._logger.info(__name__, f'Bot name {self._bot.user.name}') self._logger.trace(__name__, f'Finished ping command') await ctx.channel.purge() + if ctx.interaction is None: + return + await ctx.interaction.response.send_message('Purged this channel xD')