python - Discord Bot DM 问卷调查 discord py
问题描述
我基本上是在尝试制作一个应用程序/问卷调查不和谐机器人。触发后,机器人应该通过 dm 通知用户并询问用户是否要继续。用户回复“继续”,机器人从第一个问题开始,等待用户的回复。Bot 然后接受响应并存储它并继续下一个问题并循环直到它完成最后一个问题。我被困在机器人发送第一个问题的部分,当我回答时我没有得到任何回应。任何帮助,将不胜感激。
PS。我对python和discord py还很陌生
@bot.command()
async def apply(ctx):
Name = ''
Age = ''
user = ctx.author
name = ctx.author.display_name
q = ['What is your full real name?',
'What is your age?']
i = 0
#embed
message = 'Thank you ' + name + \
' for taking interest in Narcos City Police Department. You will be asked a series of question which you are required to answer to the fullest of your ability. Please reply with *proceed* to start your application.'
embedmsg =discord.Embed(title = 'NARCOS CITY POLICE DEPARTMENT', color = Police)
embedmsg.set_thumbnail(url = thumbnail)
embedmsg.add_field(name = 'Human Resources:', value = message)
#initial bot response
dmchannel = await ctx.author.send(embed=embedmsg)
#check
def check(m):
return m.author == ctx.author and m.channel == ctx.author.dm_channel
msg = await bot.wait_for('message', check=check)
if msg.content == 'proceed':
async def askq(q):
await ctx.author.send(q)
msg
return msg.content
Name = await askq(q[0])
Age = await askq(q[1])
解决方案
您的检查功能有点混乱。您可以尝试以下方法:
@bot.command
async def apply(ctx):
await ctx.author.send("Filler") # Sends a message to the author
questions = ["", "", ""] # Create your list of answers
answers = [] # Empty list as the user will input the answers
def check(m):
return ctx.author == m.author and isinstance(m.channel, discord.DMChannel) # Check that the messages were sent in DM by the right author
for i in questions:
await ctx.author.send(i) # Sends the questions one after another
try:
msg = await bot.wait_for('message', timeout=XXX, check=check)
except asyncio.TimeoutError:
await ctx.author.send("Timeout message.")
return # Will no longer proceed, user has to run the command again
else:
answers.append(msg) # Appends the answers, proceed
[OPTIONAL PART]
channel = bot.get_channel(ChannelID)
e = discord.Embed(color=ctx.author.color)
e.title = "New application"
e.description = f"First answer: {answers[0].content} [...]"
await channel.send(embed=e)
我在代码中做了什么?
- 内置不同的检查,以确保命令作者在 DM 频道中发送了答案
- 将答案放入列表 (
answers.append(msg)
) - 将答案嵌入到您选择的频道中
!!请注意,使用此方法,机器人将直接开始询问您的问题!
如果您想首先询问用户是否想从问题开始,您可以构建如下内容:
try:
preply = await self.bot.wait_for("message", check=check, timeout=43200)
while preply.content != "proceed": # If answer is not "proceed"
await ctx.author.send("If you want to proceed please run the command again and type `proceed`")
preply = await self.bot.wait_for("message", check=check, timeout=XXX)
await ctx.author.send("We will now proceed.") # If answer is "proceed"
[Rest of the code above in the right indentation.)
推荐阅读
- javascript - 分析脚本应该放在 React 应用程序的什么位置?
- selenium - 尝试访问 Parent 和 selenium 中的兄弟元素。不抛出任何元素异常
- php - Laravel 返回 response() 不会停止执行
- java - 简化/缩短 TextView 分配
- javascript - 当由 EventListener 调用时,useState 挂钩设置器仅运行一次
- machine-learning - 使用 Conv1D 层将 keras 代码转换为 pytorch 代码
- node.js - Node.js - 渲染一个未定义的变量
- sql-server - 如何使用 Visual Studio 调试密码加密的 SSIS 包?
- biztalk - 是否在 AS2 消息中加密了 MIME 标头?
- php - 如何解决 MySQLi 中的“重复条目”错误