【问题标题】:Discord Bot DM Questionnaire discord pyDiscord Bot DM 问卷调查 discord py
【发布时间】:2021-06-04 12:19:25
【问题描述】:

我基本上是在尝试制作一个应用程序/问卷调查不和谐机器人。触发后,机器人应该通过 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])

【问题讨论】:

    标签: python discord discord.py


    【解决方案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.)
    

    【讨论】:

    • 老兄!我可以在任何地方给你发消息吗?我真的被这个T_T卡住了
    • 你卡在哪里了?
    • 我刚刚看到您的输入,我现在正尝试在机器人上实现它。我可以取消超时吗?
    • 是的,我只是将其设置为XXX,因为您可能想设置自己的值。
    • 设置为 0 是否意味着它不会超时?
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2020-09-02
    • 2022-01-16
    • 2021-02-07
    • 2021-04-24
    • 2020-07-27
    • 2021-11-10
    相关资源
    最近更新 更多