【问题标题】:Is there a way to get the name of the command from the message (Discord.py)有没有办法从消息中获取命令的名称(Discord.py)
【发布时间】:2021-01-07 01:03:08
【问题描述】:

通常当您使用 discord.py 模块创建命令时,您会执行以下操作:

@commands.command(name="my_command", pass_context=True, aliases=["mc"])
def my_command(ctx):
    #Do some command action here

我希望能够使用我的 Discord 机器人来调节命令,以便命令只能在指定频道中使用,例如“my_command”只能在#general中使用。

获取命令名称的一种简单方法是执行以下操作:

def get_command_name(message, prefix):
    command = message.split()[0].strip(prefix)

如果我可以使用message.command_used 之类的东西来获取命令会更好,但我检查了文档,似乎没有类似的东西。有没有办法从context 获取使用的命令,而不是从字符串操作中获取命令?

【问题讨论】:

    标签: python python-3.x discord.py-rewrite


    【解决方案1】:

    但我检查了文档,似乎没有类似的东西

    commands.Context 的文档中,您可以找到commandinvoked_with,您可以使用它们来获取命令的名称。如果您想包含可能的子命令,还有invoked_subcommand

    【讨论】:

    • 啊,我明白了。我正在查看 API 参考,而不是 discord.ext.commands API 参考。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2021-04-14
    • 2021-06-22
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多