Discord.py:如何在频道中等待新消息并将其内容用于其他嵌入

2024-04-25 17:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我一直在用一些特殊命令构建一个机器人

上诉命令是目前存在问题的命令

指挥部的工作是什么

它必须发送一个嵌入,并说明所有的东西都必须输入到通道中,机器人才能获得它。这就是机器人

代码:

@nd.command()
async def appeal(ctx):
    if ctx.channel.id == 836811104424296480:
        appealinfoembed=discord.Embed(title="Appeal", description=f"Hi {ctx.author.mention}, you are appealing for verification. Now, the verification system works like this, unless you are verified, you can't send messages in the server except this category. So, if you are a town member and you want to send messages and use other features, then get verified. Thus, use this command. \n \nYou need to submit the following data in order to get verified. This data will be looked over by our Town Council and they will verify you soon. This is the format: \n```Real Name: \nIn game name: \nAge: \nSex: M/F/T \n \nTown Joing Date: \nExperience: 'Add you experience in the Towny Servers, anywhere you played, in brief.'```", color=discord.Colour.random())
        appealinfoembed.set_author(name="xXMiaraXx")
        appealinfoembed.set_footer(text=datetime.datetime.now())
        await ctx.channel.send(embed=appealinfoembed)
        
        await ctx.channel.send("Your next message after this text will be considered as the information asked before. Please enter all the details in one single message, otherwise the verification will not work.")

        appealchannel = nd.get_channel(836811104424296480)
        
        if member_details == await appealchannel.fetch_message(appealchannel.last_message_id):
            memberdetailsembed=discord.Embed(title=f"Appeal by - {ctx.author.mention}", description=member_details, color=discord.Colour.random())
            memberdetailsembed.set_author(name="xXMiaraXx")
            memberdetailsembed.set_footer(text=datetime.datetime.now())
            await appealchannel.send(embed=memberdetailsembed)

    else:
        return

问题:

上面的代码AppalChannel变量工作正常,因为它是正确的,但它之后的任何代码都不工作。这根本没用。任何信息发布后,它没有被接受,或者我不知道会发生什么。我不知道错误到底在哪里


答案可能不一定是针对该代码的类型。请你做任何事情让我明白,然后开始工作


Tags: the代码inyousenddatetimechannelawait
1条回答
网友
1楼 · 发布于 2024-04-25 17:28:18

问题是,你的机器人在写消息的时候在频道中正确地获得了最新消息,没有人能这么快地输入。要解决此问题,您可以使用wait_for(),将其添加到appealchannel = nd.get_channel(836811104424296480)之前:

def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel
try:
    answer = await nd.wait_for("message", check=check, timeout=600.0)#you can add any timeout in seconds there, for an appeal bot I would recommend something like 15 minutes
except asyncio.TimeoutError:
    await ctx.send("Your time is up") #Do stuff here when the time runs out

这应该可以解决您的问题,因为我不知道您从哪里获得变量成员的详细信息。因此,使用answer.content代替member_details并删除if member_details == await appealchannel.fetch_message(appealchannel.last_message_id): 因此,您的最终代码是:

@nd.command()
async def appeal(ctx):
    if ctx.channel.id == 836811104424296480:
        appealinfoembed=discord.Embed(title="Appeal", description=f"Hi {ctx.author.mention}, you are appealing for verification. Now, the verification system works like this, unless you are verified, you can't send messages in the server except this category. So, if you are a town member and you want to send messages and use other features, then get verified. Thus, use this command. \n \nYou need to submit the following data in order to get verified. This data will be looked over by our Town Council and they will verify you soon. This is the format: \n```Real Name: \nIn game name: \nAge: \nSex: M/F/T \n \nTown Joing Date: \nExperience: 'Add you experience in the Towny Servers, anywhere you played, in brief.'```", color=discord.Colour.random())
        appealinfoembed.set_author(name="xXMiaraXx")
        appealinfoembed.set_footer(text=datetime.datetime.now())
        await ctx.channel.send(embed=appealinfoembed)
        
        await ctx.channel.send("Your next message after this text will be considered as the information asked before. Please enter all the details in one single message, otherwise the verification will not work.")
        def check(msg):
                return msg.author == ctx.author and msg.channel == ctx.channel
        try:
            answer = await nd.wait_for("message", check=check, timeout=600.0)#you can add any timeout in seconds there, for an appeal bot I would recommend something like 15 minutes
        except asyncio.TimeoutError:
            await ctx.send("Your time is up") #Do stuff here when the time runs out
        memberdetailsembed=discord.Embed(title=f"Appeal by - {ctx.author.mention}", description=answer.content, color=discord.Colour.random())
        memberdetailsembed.set_author(name="xXMiaraXx")
        memberdetailsembed.set_footer(text=datetime.datetime.now())
        appealchannel = nd.get_channel(836811104424296480)
        await appealchannel.send(embed=memberdetailsembed)

参考资料:

相关问题 更多 >