使斜杠命令出现在 Discord UI

Making a Slash Command appear in Discord UI

在 discord 上,模块 discord.ext.commands 允许您创建命令。我尝试创建一个带有前缀="/"的命令,尽管它不会出现在 discord 的 UI.

代码:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="/")

@bot.command(name="kick", description="Kick a user to your wish.")
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"The user @{member} has been kicked. Reason: {reason}.")

bot.run("mytoken")

它不会弹出。

但我想这样做:

您需要安装 discord-py-slash-command,然后将代码导入 from discord-py-slash-command import SlashCommands。您可以参考以下代码:

import discord
from discord_slash import SlashCommand
client = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(client, sync_commands=True) # Declares slash commands through the client.

guild_ids = [1234567890] # Put your server IDs in this array.

@slash.slash(name="ping", guild_ids=guild_ids)
async def _ping(ctx):
    await ctx.send("Pong!")

client.run("token")