我有一个斜杠命令,我基本上是这样定义的:
import discord
from discord import app_commands
tree = app_commands.CommandTree(client)
@tree.command(
name="mycmd",
description="...",
guild=discord.Object(id=serverId)
)
@app_commands.autocomplete(...)
async def mycmd(
interaction: discord.Interaction,
arg1: str,
arg2: str,
extra: str = ''
):
有没有办法访问命令中的任何后续内容?例如。如果有人运行
/mycmd [foo] [bar] baz bam bap
,其中 foo
和 bar
是 arg1 和 arg2,有没有办法访问“baz bam bap”?在discord.py 文档中,我看到对 *
或 *args
的引用,但这些似乎不适用于此东西,也许仅适用于机器人模块。目前我有一个可选的“额外”参数,人们可以指定,但我宁愿只获取没有该参数的所有内容。
我不知道这是否是实现斜杠命令的正确或现代方法;我根据文档和示例将其拼凑在一起。 Discord.py 有点令人困惑。但它确实有效。
这才是实现你的想法的正确方法。让我们分解一下代码,
@tree.command(
name="mycmd",
description="...",
guild=discord.Object(id=serverId)
)
@app_commands.autocomplete(...)
async def mycmd(
interaction: discord.Interaction,
arg1: str,
arg2: str,
extra: str = ''
):
...
extra
是可选参数。用户可以运行 /mycmd arg1: a arg2: b extra: baz bam bap
。
斜线命令接受参数,例如
arg1: a
。与位置前缀命令不同 !mycmd a b baz bam bap
。
在命令提示符(如
/mycmd arg1: a arg2: b something
)后输入的任何内容都不会发送到机器人。
对于这样的事情,我建议使用前缀命令;
import discord
from discord.ext import commands
bot = commands.Bot(...)
@bot.command()
async def mycmd(ctx, arg1, arg2, *, extra):
...
根据您的前缀,可以使用
!mycmd arg1 arg2 baz bam bap
运行。