python - 如何使用@client.event on_message discord-py-slash (discord.py) 添加斜杠命令
问题描述
我正在尝试向我现有的机器人添加斜杠命令。
目前我正在使用
intents = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
slash = SlashCommand(client, sync_commands=True)
@client.event
async def on_message(message):
# do some stuff
@slash.slash(name="test",
description="This is just a test command, nothing more.")
async def test(ctx):
await ctx.send(content="Hello World!")
但是当我不和谐时,我输入 / 和命令。我明白了
Invalid interaction application command
我也试过这个:discord_slash:如何实际添加斜线命令/为什么我的不工作?.
但我不明白如何忽略on_message
函数中的 /slash 命令。
编辑
我尝试在中打印 message.content on_message
,但我没有得到任何内容。当我尝试执行它时,终端中没有错误或异常。我只得到不和谐的错误。
这是我得到的唯一错误。
提前谢谢。
解决方案
编辑
我实际上正在更改很多东西,但我不小心以为这与同步命令有关,结果并非如此。它不起作用的实际原因是,它需要一个小时来注册全局斜杠命令,但是如果你将 guild_ids 参数添加到斜杠装饰器,它会更改为 guild_slash_command。它可以立即工作(不到 1 分钟)。我还添加了sync_command,它在添加或删除时将更改应用于斜杠命令。
编辑代码
intents = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
slash = SlashCommand(client, sync_commands=True)
guilds_ids = [guild(server) ids]
@client.event
async def on_message(message):
# do some stuff
@slash.slash(name="test",
description="This is just a test command, nothing more.", guild_ids=guild_ids)
async def test(ctx):
await ctx.send(content="Hello World!")
原来的
经过一些尝试和错误,我发现通过删除斜线 obj 声明中的 sync_commands 。有效
这是整个代码..
intents = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
slash = SlashCommand(client)
@client.event
async def on_message(message):
# do some stuff
@slash.slash(name="test",
description="This is just a test command, nothing more.")
async def test(ctx):
await ctx.send(content="Hello World!")
推荐阅读
- ios - 在 swiftUI 中部分更新 UI
- android-11 - 来自具有 API 30 (android 11) 的三星物理模拟器的错误 Android 11 -> java.net.SocketTimeoutException
- java - 有谁知道是否可以更改下载请求通知的内容标题?如何?
- typescript - vuejs + typescript项目中导入interactjs会产生类型检查错误
- angular - Angular uri路由的Nginx后备
- javascript - 方括号如何在下面的“FizzBuzz”代码中求值?
- python-3.x - 如何从同一个 python 脚本/notebook 并行运行多个 AUTOML 框架?
- c# - 如何更改播放器位置 ontriggerenter unity 2d c#
- groovy - 如何在groovy中组合多个json数组
- spring-boot - 无法在 Spring Boot 中测试 SessionRegistry