![](/img/trans.png)
[英]Discord.py bot - How to make a bot respond depending on the command content?
[英]Discord.py bot: How to make a bot respond to command only in certain circumstances?
我正在用 Python 制作一個 Discord 機器人,我想在機器人僅在某些情況下響應命令時添加這樣一個功能。
例如:
我: !quiz
Bot:提供變體 - !a; !b; !c
!a; !b; !c
我:選擇變體並機器人響應
!a
; !b
或!c
在使用!quiz
命令之前,bot不會響應。 有任何想法嗎?您可以使用bot.wait_for()
等待用戶響應:
@bot.command(pass_context=True)
async def quiz(ctx):
channel, author = ctx.channel, ctx.message.author
await ctx.send('Choose one:\n!a;\n!b;\n!c;')
answer = await bot.wait_for(
"message",
check=lambda x: x.content.startswith('!') and x.channel == channel and x.author == author
)
if answer.content.strip() == '!a':
# here goes your logic
按照 Mahrkeenerh 所說的,你可以使用 wait_for() 下面是一個例子:
@bot.command()
async def quiz(ctx):
await ctx.send("A, B or C")
try:
msg = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
except asyncio.TimeoutError:
await ctx.send("You took to long...")
else:
if msg.content == "A":
await ctx.send("Correct")
elif msg.content == "B":
await ctx.send("Wrong")
elif msg.content == "C":
await ctx.send("Wrong")
else:
await ctx.send("Huh?")
我們正在發送消息然后等待響應,我們也在檢查它是否是原始作者和原始頻道。 如果是,那么我們正在檢查是否回答以查看響應是什么。
您可以使用wait_for()
:
或者你可以有一個全局變量(一個標志 - 布爾值)來標記,如果你使用了!quiz
命令。 然后在 answers 命令中,檢查這個全局變量是否是True
,如果是,響應(並將其重置回False
),否則忽略。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.