From ce16dc97ed04337b4c0a62f08ee520e8e1bdc441 Mon Sep 17 00:00:00 2001 From: Gnome Date: Thu, 2 Sep 2021 16:10:35 +0100 Subject: [PATCH] Fix some logic and make an actual example --- discord/ext/commands/core.py | 22 +++++++----- examples/slash_commands.py | 35 ++++++++++++++++++++ examples/slash_commands/slash_and_message.py | 30 ----------------- examples/slash_commands/slash_only.py | 26 --------------- 4 files changed, 48 insertions(+), 65 deletions(-) create mode 100644 examples/slash_commands.py delete mode 100644 examples/slash_commands/slash_and_message.py delete mode 100644 examples/slash_commands/slash_only.py diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 0ec54418..b5eef841 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1144,17 +1144,21 @@ class Command(_BaseCommand, Generic[CogT, P, T]): :class:`bool` A boolean indicating if the command can be invoked. """ - if not self.enabled or ( - ctx.interaction is None and ( - self.message_command is False - or (self.message_command is None and not ctx.bot.message_commands) - ) or ( - self.slash_command is False - or (self.slash_command is None and not ctx.bot.slash_commands) - ) - ): + if not self.enabled: raise DisabledCommand(f'{self.name} command is disabled') + if ctx.interaction is None and ( + self.message_command is False + or (self.message_command is None and not ctx.bot.message_commands) + ): + raise DisabledCommand(f'{self.name} command cannot be run as a message command') + + if ctx.interaction is not None and ( + self.slash_command is False + or (self.slash_command is None and not ctx.bot.slash_commands) + ): + raise DisabledCommand(f'{self.name} command cannot be run as a slash command') + original = ctx.command ctx.command = self diff --git a/examples/slash_commands.py b/examples/slash_commands.py new file mode 100644 index 00000000..85a589df --- /dev/null +++ b/examples/slash_commands.py @@ -0,0 +1,35 @@ +import discord +from discord.ext import commands + +# Set slash commands=True when constructing your bot to enable all slash commands +# if your bot is only for a couple of servers, you can use the parameter +# `slash_command_guilds=[list, of, guild, ids]` to specify this, +# then the commands will be much faster to upload. +bot = commands.Bot("!", intents=discord.Intents(guilds=True, messages=True), slash_commands=True, slash_command_guilds=[514232441498763279]) + +@bot.event +async def on_ready(): + print(f'Logged in as {bot.user} (ID: {bot.user.id})') + print('------') + + +@bot.command() +# You can use commands.Option to define descriptions for your options, and converters will still work fine. +async def ping(ctx: commands.Context, emoji: bool = commands.Option(description="whether to use an emoji when responding")): + # This command can be used with slash commands or message commands + if emoji: + await ctx.send("\U0001f3d3") + else: + await ctx.send("Pong!") + +@bot.command(message_command=False) +async def only_slash(ctx: commands.Context): + # This command can only be used with slash commands + await ctx.send("Hello from slash commands!") + +@bot.command(slash_command=False) +async def only_message(ctx: commands.Context): + # This command can only be used with a message + await ctx.send("Hello from message commands!") + +bot.run("NjA1NDM2ODYyNjAxMTAxMzIz.XT8fBA.MCwQg6Bz_TuQ4oktliXHdGczCDY") diff --git a/examples/slash_commands/slash_and_message.py b/examples/slash_commands/slash_and_message.py deleted file mode 100644 index 87b89fa3..00000000 --- a/examples/slash_commands/slash_and_message.py +++ /dev/null @@ -1,30 +0,0 @@ -from discord.ext.commands import Bot, Cog, Option - -bot = Bot(slash_commands=True) # To use both normal and slash commands, you only need to enable slash commands -bot.add_cog(Info(bot)) - -@bot.command(slash=True) -async def hello(ctx): - await ctx.send(f"Hey, {ctx.author.mention}!") - -@bot.command() -async def byw(ctx): - await ctx.send(f"Bye {ctx.author.mention}!") - -# And in a cog - -class Info(Cog): - def __init__(self, bot): - self.bot = bot - - @commands.command(slash=True) - async def userinfo(self, ctx, member: discord.Member = Option(description="The user to get info about")): # Note that it must use the description kwarg - """Get info about a member""" - await ctx.send(f"ID: {member.id}", ephemeral=True) - - @commands.command() - async def ban(self, ctx, member: discord.Member, reason: str = "No reason"): - await member.ban(reason=reason) - await ctx.send("They have been banned") - -bot.run("token") diff --git a/examples/slash_commands/slash_only.py b/examples/slash_commands/slash_only.py deleted file mode 100644 index 22c7fe26..00000000 --- a/examples/slash_commands/slash_only.py +++ /dev/null @@ -1,26 +0,0 @@ -from discord.ext.commands import Bot, Cog, Option - -bot = Bot(slash_commands=True, normal_commands=False) # Setting normal_commands to False will make this slash commands only -bot.add_cog(Info(bot)) - -@bot.command(slash=True) -async def hello(ctx): - await ctx.send(f"Hey, {ctx.author.mention}!") - -# And in a cog - -class Info(Cog): - def __init__(self, bot): - self.bot = bot - - @commands.command(slash=True) - async def userinfo(self, ctx, member: discord.Member = Option(description="The user to get info about")): # Note that it must use the description kwarg - """Get info about a member""" - await ctx.send(f"ID: {member.id}") - - @commands.command(slash=True) - async def ban(self, ctx, member: discord.Member = Option(description="The member to ban"), reason: str = Option("No reason", description="Why to ban them")): # The first arg here will be used as default if nothing was provided - await member.ban(reason=reason) - await ctx.send("They have been banned", ephemeral=True) - -bot.run("token")