簡體   English   中英

如何在 Discord.py 中編輯消息?

[英]How to edit a message in Discord.py?

我正在使用它向服務器上的每個人發送 dm,

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

代碼很完美,它的工作也很好。 它在每個 dm 發送到 {member.name} 的 dm 之后發送日志,我想要的是在每個日志(即 msg 已發送到.....)之后,對於下一個日志,它應該編輯上一個消息。 (對不起,我不擅長解釋:p)

它像這樣在每個 dm 之后發送一條消息,發送到 {member.name} 然后下一個發送到 {member.name}

我想要的是它不應該一次又一次地發送它,而是應該為每個 dm 一次又一次地編輯第一條消息。

如果您能提供幫助,我將不勝感激!

您可以使用Message.edit 以下是如何讓它在您的代碼中工作:

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

還添加了一行來忽略機器人用戶。

要編輯消息,您可以使用Message.edit 這是一個例子:

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

此外,如果您查看文檔或其他 stackoverflow 問題,您可以輕松找到答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM