python - bot 执行下一个命令而不等待消息
问题描述
您好,我一直在尝试为我的服务器创建一个应用程序机器人,该机器人向您发送一个问题,然后该人回答,然后记下等,但我的问题是,有时下一个问题会在不等待上一个问题的答案的情况下发送。这只发生在大多数时候。试试我可能无法解决问题。我对 python 还很陌生,而且我也束手无策。我的参考代码:
import os
from discord.ext import commands
from discord.utils import get
BOT_PREFIX = ("!")
client = commands.Bot(command_prefix=BOT_PREFIX)
a_list = []
@client.event
async def on_ready():
print ("------------------------------------")
print ("Bot Name: " + client.user.name)
print ("------------------------------------")
submit_wait = False
a_list = []
b_list = []
c_list = []
d_list = []
@client.command(aliases=['application'])
async def app(ctx):
a_list = []
b_list = []
c_list = []
d_list = []
submit_wait = False
submit_channel = client.get_channel(806404345830047744)
channel = await ctx.author.create_dm()
await channel.send("starting applaction!")
await ctx.send(ctx.author.mention + "check your dms!")
time.sleep(2)
def check(m):
return m.content is not None and m.channel == channel
await channel.send("Do you have any prior milli sim experiance?")
msg = await client.wait_for('message', check=check)
a_list.append(msg.content)
await channel.send("What time zone are you in?")
msg2 = await client.wait_for('message', check=check)
b_list.append(msg2.content)
await channel.send("If a officer ordered you to break the genva convention would you do it?")
msg3 = await client.wait_for('message', check=check)
c_list.append(msg3.content)
await channel.send("Is there any particular dvision you want to be in?")
msg4 = await client.wait_for('message', check=check)
d_list.append(msg4.content)
await channel.send('thank you for applying! a officer will do your application as soon as they can - respound with "submit" to submit your application')
msg = await client.wait_for('message', check=check)
if "submit" in msg.content.lower():
submit_wait = False
answers = "\n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1))
answer = "\n".join(f'{a}. {b}' for a, b in enumerate(b_list, 2))
answerr = "\n".join(f'{a}. {b}' for a, b in enumerate(c_list, 3))
answerss = "\n".join(f'{a}. {b}' for a, b in enumerate(d_list, 4))
submit_msg = f'Application from {msg.author} \nThe answers are:\n{answers}'
submit_msg2 = f'{answer}'
submit_msg3 = f'{answerr}'
submit_msg4 = f'{answerss}'
await submit_channel.send(submit_msg)
await submit_channel.send(submit_msg2)
await submit_channel.send(submit_msg3)
await submit_channel.send(submit_msg4)
client.run(os.getenv('TOKEN'))
解决方案
我使用测试代码print()
,我认为问题是因为check()
有时会收到机器人的问题并将其视为用户的答案。
你必须办理登机手续m.author
-check()
像这样
@client.command(aliases=['application'])
async def app(ctx):
submit_channel = client.get_channel(806404345830047744)
#print('submit_channel:', submit_channel)
channel = await ctx.author.create_dm()
author = ctx.author
def check(m):
#print('m.author:', m.author)
#print('m.content:', m.content)
#print('m.channel:', m.channel, m.channel == channel)
return m.author == author and m.content and m.channel == channel
具有其他更改的最少工作代码
import os
import time
from discord.ext import commands
client = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
print ("------------------------------------")
print ("Bot Name:", client.user.name)
print ("------------------------------------")
@client.command(aliases=['application'])
async def app(ctx):
submit_channel = client.get_channel(806404345830047744)
print('submit_channel:', submit_channel)
channel = await ctx.author.create_dm()
author = ctx.author
def check(m):
print('m.author:', m.author)
print('m.content:', m.content)
print('m.channel:', m.channel, m.channel == channel)
return m.author == author and m.content and m.channel == channel
await channel.send("starting applaction!")
await ctx.send(ctx.author.mention + " check your dms!")
time.sleep(2)
await channel.send("Do you have any prior milli sim experiance?")
answer_a = await client.wait_for('message', check=check)
answer_a = answer_a.content
await channel.send("What time zone are you in?")
answer_b = await client.wait_for('message', check=check)
answer_b = answer_b.content
await channel.send("If a officer ordered you to break the genva convention would you do it?")
answer_c = await client.wait_for('message', check=check)
answer_c = answer_c.content
await channel.send("Is there any particular dvision you want to be in?")
answer_d = await client.wait_for('message', check=check)
answer_d = answer_d.content
await channel.send('thank you for applying! a officer will do your application as soon as they can - respound with "submit" to submit your application')
msg = await client.wait_for('message', check=check)
if "submit" in msg.content.lower():
submit_msg = f'''Application from {msg.author}
The answers are:
1. {answer_a}
2. {answer_b}
3. {answer_c}
4. {answer_d}'''
await submit_channel.send(submit_msg)
client.run(os.getenv('TOKEN'))
推荐阅读
- firebase - 如何使用云函数处理 CSV 并返回前端?
- javascript - 如何以及在何处将“Esc”侦听器添加到 litElement 组件?
- c++ - 使用 CImage VS2019 C++ 调整图像大小
- javascript - 通过一维数组对二维数组进行分组和连接
- python - Python:比较两个excel表之间的列字符串并找到匹配的列并写入另一个数据框
- visual-studio - Visual Studio 2019 在某些文件上模糊
- prometheus - 比较 prometheus 中只有一个标签不同的两个指标
- java - 如何使用java在谷歌上搜索?
- android - 在 Wear OS 上从后台收听物理按钮按下
- c# - 如何在表单提交时触发 Razor 代码隐藏