首页 > 解决方案 > Discord.py: How to wait for new message in channel and use its content for another embed

问题描述

I have been building a bot with some special commands recently.

APPEAL command is the one which has the problem right now.

What's the work of the command?

It has to send an embed and mention what all stuff has to be entered in channel for the BOT to get it. That is it for the bot.

Code:

@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

Problem:

The code above appealchannel variable works fine as it is correct but anything after it does not work. It just doesn't do anything. Any message posted after it not taken in or I don't know what happens. I am not getting where the error actually is.


The answers may not necessarily made on this code's type. Please just do anything to make it clear to me and working.

标签: pythondiscord.py

解决方案


The problem is, that your bot gets the newest message in the channel right when it wrote it message, and nobody can type so fast. To fix this, you can use wait_for(), add this before 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

This should fix your problem, since I don't know where you get the variable member_details from. So instead of member_details, use answer.content and remove the if member_details == await appealchannel.fetch_message(appealchannel.last_message_id): So your final code would be:

@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)

References:


推荐阅读