如何让机器人仅在用户的消息以问号结尾时才说些什么?
How do I make the bot say something only when the user's message ends with a question mark?
@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'```
很确定这个错误本身就说明了问题,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!")
@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'```
很确定这个错误本身就说明了问题,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!")