python - discord.py - 机器人不响应多个连接的消息
问题描述
我正在尝试让一个不和谐的机器人在 Python 中讲笑话。但是,特别是关于敲敲门的笑话,在用户用“谁在那儿”做出响应后,我无法让机器人做出响应。你能帮我吗?
@bot.command(pass_context=True)
async def joke(ctx, type):
if type.lower() == "ym":
yo_momma = ("fat, I took a picture of her last Christmas and it's still printing.",
"fat when she got on the scale it said, 'I need your weight not your phone number.'",
"fat and old when God said, 'Let there be light', he asked your mother to move out of the way.",
"fat she doesn't need the internet, because she's already world wide.",
"fat, when she sat on an iPod, she made the iPad!",
"fat she walked past the TV and I missed 3 episodes.",
"ugly when she tried to join an ugly contest they said, 'Sorry, no professionals.'",
"ugly she made One Direction go another direction.",
"ugly Fix-It Felix said, 'I can\'t fix it.'"
"stupid when an intruder broke into her house, she ran downstairs, dialed 9-1-1 on the microwave, and couldn't find the 'CALL' button.",
"stupid she stuck a battery up her ass and said, 'I GOT THE POWER!'",
"stupid that she sat on the TV to watch the couch.")
await bot.say("Yo momma so {}".format(random.choice(yo_momma)))
elif type.lower() == "kk":
await bot.send_message(ctx.message.channel, "Knock Knock.")
if "who's there" in ctx.message.content.lower():
kk = [["A little old lady", "All this time, I did not know you could yodel."],
["Cow says", "Cow says mooooo!"],
["Etch", "Bless you, friend."],
["Robin", "Now hand over the cash."],
["Cash", "No thanks, I'll have some peanuts."],
["Mustache", "I mustache you a question, but I'll shave it for later."],
["Tank", "You're welcome."],
["Candice", "Candice door open, or what?"],
["Boo", "No need to cry, it's only a joke."],
["Howl", "Howl you know unless you open this door?"],
["Iran", "Iran all the way here. Let me in already!"]]
joke_num = random.randint(0, 9)
chosen_joke = [kk[joke_num][0], kk[joke_num][1]]
await bot.say(chosen_joke[0])
if "{} who".format(chosen_joke[0]) in ctx.message.content.lower():
await bot.say(chosen_joke[1])
解决方案
您可以使用wait_for_message
一个check
函数来等待来自特定用户的特定内容的消息:
elif type.lower() == "kk":
await bot.send_message(ctx.message.channel, "Knock Knock.")
check = lambda m: "who's there" in m.content.lower()
await bot.wait_for_message(author=ctx.message.author, channel=ctx.message.channel, check=check)
kk = [["A little old lady", "All this time, I did not know you could yodel."],
["Cow says", "Cow says mooooo!"],
["Etch", "Bless you, friend."],
["Robin", "Now hand over the cash."],
["Cash", "No thanks, I'll have some peanuts."],
["Mustache", "I mustache you a question, but I'll shave it for later."],
["Tank", "You're welcome."],
["Candice", "Candice door open, or what?"],
["Boo", "No need to cry, it's only a joke."],
["Howl", "Howl you know unless you open this door?"],
["Iran", "Iran all the way here. Let me in already!"]]
joke_num = random.randint(0, 9)
chosen_joke = [kk[joke_num][0], kk[joke_num][1]]
await bot.say(chosen_joke[0])
check = lambda m: "{} who".format(chosen_joke[0]) in m.content.lower()
await bot.wait_for_message(author=ctx.message.author, channel=ctx.message.channel, check=check)
await bot.say(chosen_joke[1])
推荐阅读
- javascript - React Native:'TypeError:未定义不是对象(评估'response.headers ['set-cookie']')'
- php - 在 PHP 中打印树中可见的级别时出错
- c# - Razor - 如何将 id 附加到单选按钮
- python - 将熊猫 df 转换为 json
- swift - Swift:将字符串日期数组转换为格式化字符串日期
- git - 如何完全删除 Git(Windows)?或重新登录 Git Windows
- c++ - 我无法删除这些向量的元素
- esri - ESRI ArcGIS:平移和缩放以适合标记
- sql - 如何使用插入查询将数据从一个 SQL 表插入到另一个表
- three.js - 三 JS - 为什么行长等于零?