如何删除discord bot在特定通道中发送的以前的消息?

2024-09-29 23:21:58 发布

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

我正在尝试制作一个discord机器人,它在一个特定的通道中发送一条消息,告诉用户我的Minecraft服务器何时开启或关闭。我只需要发送2个嵌入,一个用于启动,另一个用于关闭 像这样:

enter image description here

我正在尝试删除机器人发送的旧邮件,但我不知道如何删除

import discord
import sys
import os
from datetime import datetime
now = datetime.now()
Ntime = now.strftime("%H:%M:%S")
txtStart = ()
client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    channel = client.get_channel(800317242910834699)
    embed= discord.Embed(title="[ Server Status ]", description="Running...", color=discord.Color.green())
    embed.add_field(name="success At :", value=Ntime, inline=True)
    file = discord.File(os.path.join("D:\Scripts\Discord/ServerStarted.gif"), filename="ServerStarted.gif")
    embed.set_thumbnail(url="attachment://ServerStarted.gif")
    message = await channel.send(file=file, embed=embed)

    embed= discord.Embed(title="[ Server Status ]", description="shutting down...", color=discord.Color.red())
    embed.add_field(name="success At :", value=Ntime, inline=True)
    file = discord.File(os.path.join("D:\Scripts\Discord/ServerStoped.gif"), filename="ServerStoped.gif")
    embed.set_thumbnail(url="attachment://ServerStoped.gif")
    await channel.send(file=file, embed=embed)

Tags: importclientdatetimeoschannel机器人embedgif
1条回答
网友
1楼 · 发布于 2024-09-29 23:21:58

您可以使用TextChannel.purge方法:

await channel.purge(limit=1)

记住在之后发送消息,而不是在之前发送消息

请注意,此方法将删除频道中最早的消息,如果要删除bot发送的最新消息,可以使用检查功能:

def is_me(m):
    return m.author == client.user

await channel.purge(limit=1, check=is_me)

参考资料:

相关问题 更多 >

    热门问题