检查不和谐消息是否有参考(是回复)

Check if a discord message has a reference (is a reply)

我尝试为我的 discord 机器人发出命令,如果在回复中使用它,它应该可以正常工作。 要检查这样的事情,我想我可以使用 commands.check(),它基本上会在我的命令中添加一个检查,所以这就是我需要的。

但是我不确定如何正确使用这个功能。我写了一个函数来检查消息是否有引用(而不是回复):

def check_if_message_is_reply(ctx):

    result = ctx.message.reference

    if result is None:
        return 0
    else:
        return 1

所以如果没有参考,基本上我的检查应该 return False(或类似 False 的值)。将此功能添加到我的支票没有按预期工作,机器人消息永远不会发送。不管我是否使用回复:

client = commands.Bot(command_prefix = '$')

# define commands
@commands.command()
@commands.check(check_if_message_is_reply)
async def my_command(ctx):
    await ctx.send('Command worked, you used a reply')

client.add_command(my_command)

您的检查工作正常,但您为命令使用了错误的装饰器。由于您的函数似乎不在 class 中,请使用 @client.command() 而不是 @commands.command()

同样使用这个装饰器,你不需要client.add_command,它会被discord.py

自动添加
@client.command()
@commands.check(check_if_message_is_reply)
async def my_command(ctx):
    await ctx.send('Command worked, you used a reply')