python - 如何删除我的不和谐机器人在特定频道中发送的先前消息?
问题描述
我正在尝试制作一个不和谐的机器人,它在特定频道中发送消息,以告诉用户我的 Minecraft 服务器何时打开或关闭。我只需要发送 2 个嵌入,一个用于启动,另一个用于关闭,如下所示:
我正在尝试删除机器人发送的旧消息,但我不知道如何。
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)
解决方案
您可以使用以下TextChannel.purge
方法:
await channel.purge(limit=1)
记得事后发消息,而不是事前。
请注意,此方法将删除频道中最旧的消息,如果要删除机器人发送的最新消息,可以使用检查功能:
def is_me(m):
return m.author == client.user
await channel.purge(limit=1, check=is_me)
参考:
推荐阅读
- javascript - HTTP解析器:抓取单页应用程序:许多GET,如何找出页面何时结束
- makefile - Makefile:将多个单元测试 *.out 文件编译为单个测试可执行文件 (C++)
- javascript - 查询记录及其与 sequelize 的关联
- swift - 如何覆盖子类中的静态变量?
- python - 如何标准化面部地标坐标以进行重建
- git - 当对本地文件进行更改时,如何更新 github.io 存储库?
- c - 我在尝试打开文件并将其写入另一个文件时遇到段错误
- javascript - number input.value 如果包含“e”字符,则返回空字符串
- kendo-ui - Kendo Uncaught TypeError:无法读取未定义的属性“表”
- sql - 我可以在存储过程的 SQL 更新中将列名作为输入参数传递吗