Fix some logic and make an actual example
This commit is contained in:
parent
16ed293e00
commit
ce16dc97ed
@ -1144,17 +1144,21 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
|||||||
:class:`bool`
|
:class:`bool`
|
||||||
A boolean indicating if the command can be invoked.
|
A boolean indicating if the command can be invoked.
|
||||||
"""
|
"""
|
||||||
if not self.enabled or (
|
if not self.enabled:
|
||||||
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)
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise DisabledCommand(f'{self.name} command is disabled')
|
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
|
original = ctx.command
|
||||||
ctx.command = self
|
ctx.command = self
|
||||||
|
|
||||||
|
35
examples/slash_commands.py
Normal file
35
examples/slash_commands.py
Normal file
@ -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")
|
@ -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")
|
|
@ -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")
|
|
Loading…
x
Reference in New Issue
Block a user