仅当用户的消息以问号结尾时,如何让机器人说些什么?

时间:2021-01-18 16:55:43

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

@client.command(aliases=["ques", "q", "8ball", "QUES", "Q", "8BALL"])
async def question(ctx):
    answer = "yes", "no", "idk"
    final_answer = random.choice(answer)

    await ctx.send("Please ask a yes or no question")
    msg = await client.wait_for("message")

    if ctx.content.endswith("?"):
        await ctx.send(final_answer)
    else:
        await ctx.send("That's not a question!")

所以我想为 8ball 的 Discord 机器人添加一个功能。但我希望机器人找出发送的文本是否是一个问题。我在这里使用了endswith 属性,但它显示

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'content'```

1 个答案:

答案 0 :(得分:2)

可以肯定错误几乎不言自明,Context 没有 content 属性。您在 msg 中定义了 await client.wait_for(...),您应该使用该变量而不是 ctx

@client.command(aliases=["ques", "q", "8ball", "QUES", "Q", "8BALL"])
async def question(ctx):
    answer = "yes", "no", "idk"
    final_answer = random.choice(answer)

    await ctx.send("Please ask a yes or no question")
    msg = await client.wait_for("message")

    if msg.content.endswith("?"):
        await ctx.send(final_answer)
    else:
        await ctx.send("That's not a question!")