简体   繁体   中英

How to edit a message in Discord.py?

I am using this to send dm to everyone on a server,

@bot.command(pass_context = True)
@commands.has_permissions(manage_messages=True)
async def dm_all(ctx, *, args=None):
    sended_dms = 0
    rate_limit_for_dms = 20
    time_to_wait_to_avoid_rate_limit = 60

    if args != None:
        members = ctx.guild.members
        for member in members:
            try:
                await member.send(args)
                await ctx.channel.send(" sent to: " + member.name)

            except:
                await ctx.channel.send("Couldn't send to: " + member.name)
            sended_dms += 1
            if sended_dms % rate_limit_for_dms == 0: 
                asyncio.sleep(time_to_wait_to_avoid_rate_limit) 

    else:
        await ctx.channel.send("Please provide a message to send!")

The code is perfect, its working fine too. It sends the log after each dm that the dm have been sent to {member.name}, what I want is after each log ie msg have been sent to..... , for next log it should edit previous message. (sorry I am bad at explaining:p)

It send a message after each dm like this, sent to {member.name} then next one sent to {member.name}

what I want is that it shouldn't send that again and again instead it should edit the first message again and again for each dm.

I would be really grateful if you can help!

You can use Message.edit . Here's how you can get it working in your code:

@client.command(pass_context = True)
@commands.has_permissions(manage_messages=True)
async def dm_all(ctx, *, args=None):
    sended_dms = 0
    rate_limit_for_dms = 20
    time_to_wait_to_avoid_rate_limit = 60

    if args != None:
        members = ctx.guild.members
        firstTime = True
        msg = None
        for member in members:
            if not member.bot:
                try:
                    await member.send(args)
                    if firstTime:
                        msg = await ctx.channel.send(" sent to: " + member.name)
                        firstTime = False
                    else:
                        await msg.edit(content=" sent to: " + member.name)
                except Exception as e:
                    await msg.edit(content="Couldn't send to: " + member.name)
                if sended_dms % rate_limit_for_dms == 0: 
                    await asyncio.sleep(time_to_wait_to_avoid_rate_limit) 
    else:
        await ctx.channel.send("Please provide a message to send!")

Also added a line to ignore bot users.

For editing a message, you can use Message.edit . Here is an example:

random_message = await ctx.send("old message")
await random_message.edit("new message")

Also, if you look the documentation or other stackoverflow questions, you can easily find your answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM