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

2024-10-01 16:43:56 发布

您现在位置:Python中文网/ 问答频道 /正文

@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!")

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

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

Tags: noanswerclientsend机器人contentawaitcommand
1条回答
网友
1楼 · 发布于 2024-10-01 16:43:56

可以肯定的是,这个错误本身就说明了,Context没有content属性。您在await client.wait_for(...)中定义了msg,应该使用该变量而不是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!")

相关问题 更多 >

    热门问题