提问者:小点点

为什么discord.client.wait_for在有事件的情况下请求事件?


我遇到的问题是代码始终无法通过“0004”,因为它卡在wait_for上,而wait_for需要附加的位置参数:'event',从我在discord.py站点的示例中看到的示例中,它应该是旁边括号中的'message'。

'''

class Test(commands.Cog):
    def __init__(self, spreadsheeter):
        self.spreadsheeter = spreadsheeter

    @commands.command()
    async def apply(self, ctx):
        a_list = []
        submit_channel = spreadsheeter.get_channel(718698172293316608)
        channel = await ctx.author.create_dm()

        def check(m):
            return m.content is not None and m.channel == channel

        for question in q_list:
            print("0001")
            sleep(.5)
            print("0002")
            await channel.send(question)
            print("0003")
            msg = await Client.wait_for('message', timeout=60, check=check)
            print("0004")
            a_list.append(msg.content)
            print("0005")

        submit_wait = True
        print("0006")
        while submit_wait:
            print("0007")
            await channel.send("End of questions 'submit' to finish.")
            print("0008")
            msg = await Client.wait_for("message", check=check)
            print("0009")
            if "submit" in msg.content.lower():
                print("0010")
                submit_wait =False
                print("0011")
                answers = "\n".join(f"{a}. {b}" for a, b in enumerate(a_list, 1))
                print("0012")
                submit_msg = f"Apllication from {msg.author} \nThe answers are:\n{answers}"
                print("0013")
                await submit_channel.send(submit_msg)
                print("0014")

discord.client.wait_for('message',check=检查)

  • 错误:Discord.Client没有属性“wait_for”
  • 对于discord.member、ctx.member、ctx.client

将“message”替换为“message”(不改变任何内容)

围绕“消息”的位置移动

  • 引发大量其他错误...

开始时给出行括号(self,“message”,check=检查)

  • 错误:“test”对象没有属性“loop”

@client.event而不是cog样式的命令。命令

  • 错误:未解析引用“client”

代码的意图:作为命令从DM中调用,然后从该DM中的q_list中启动一系列问题,然后将它们存储到A_LIST中。然后,在完成之后,它应该将a_list作为submit_msg提交到discord通道中。

What it does so far?:
Asks first question from q_list


q_list = [
    "Question one",
    "Question two ha-ha-haaa",
    "Question three ha-ha-haaa"
]

“”“

之后,它在尝试等待答案后立即给出错误。


共2个答案

匿名用户

2事情在开始,你应该只做一个dm频道,如果没有任何还没有。ctx.author.chreate_dm()不返回通道,因此您可以使用ctx.author.dm_channel检查它是否在正确的通道中,而不是将dm通道分配给通道

@commands.command()
async def apply(self, ctx):
    a_list = []
    submit_channel = self.spreadsheeter.get_channel(718698172293316608)
    if not ctx.author.dm_channel:
        await ctx.author.create_dm()

    def check(m):
        return m.content is not None and m.channel == ctx.author.dm_channel

使用spreadsheeter代替Client,因为它是您的Client

msg = await self.spreadsheeter.wait_for('message', timeout=60, check=check)

匿名用户

我想明白了,问题是它可能会在齿轮上工作,但我想它不会,或者是缺少了什么...我让它工作的方法是把它用在

  • @client.event(在我的示例中为@spreadsheeter.event)
  • 而不是CTX。我使用了有效载荷。

“”“

@spreadsheeter.event
async def on_raw_reaction_add(payload):
    print("Hell-Yeah")
    message_id = payload.message_id
    if message_id == 739153263907176480:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, spreadsheeter.guilds)
        print (payload.emoji.name)

        if payload.emoji.name == "No1":
            role = discord.utils.find(lambda r: r.name == 'RAIDERS', guild.roles)
            print(role)
            if role in payload.member.roles:
                print("You really want to leave?")
            else:
                print("Kick")
                await payload.member.kick()
        elif payload.emoji.name == "Yes1":
            print("Yo, we got a potential raider over here..")
            channel = await payload.member.create_dm()
            await channel.send("This is still Work/Bot is under Development (Not actuall application yet).")
            await channel.send("Whenever you are ready, please start replaying to the questions.")
            print("0000")
            submit_channel = spreadsheeter.get_channel(718698172293316608)

            if not payload.member.dm_channel:
                await payload.member.create_dm()

            channel = payload.member.dm_channel
            print(channel)

            def check(m):
                return m.content is not None and m.channel == channel

            for question in q_list:
                print("0001")
                sleep(.5)
                print("0002")
                await channel.send(question)
                print("0003")
                msg = await spreadsheeter.wait_for(event='message', timeout=60, check=check)
                print("0004")
                a_list.append(msg.content)
                print("0005")

            submit_wait = True
            print("0006")
            while submit_wait:
                print("0007")
                await channel.send("End of questions 'submit' to finish.")
                print("0008")
                msg = await spreadsheeter.wait_for("message", timeout=60, check=check)
                print("0009")
                if "submit" in msg.content.lower():
                    print("0010")
                    submit_wait = False
                    print("0011")
                    answers = "\n".join(f"{a}. {b}" for a, b in enumerate(a_list, 1))
                    print("0012")
                    submit_msg = f"Apllication from {msg.author} \nThe answers are:\n{answers}"
                    print("0013")
                    await submit_channel.send(submit_msg)
                    print("0014")

        elif payload.emoji.name == "Need_more_info":
            print("Yo, we got a potential diplomat over here..")
            channel = await payload.member.create_dm()
            await channel.send("This is still Work/Bot is under Development (Not actuall application yet).")
            await channel.send("Whenever you are ready, please start with \n_apply")

“”“

它仍然不能处理多个用户,但此时一次处理一个用户很好,这就解决了wait_for不等待其他任何东西然后超时用完的原始问题。