【问题标题】:How do i delete the previous messages that my discord bot sent in a specific channel?如何删除我的不和谐机器人在特定频道中发送的先前消息?
【发布时间】:2023-03-16 08:57:01
【问题描述】:

我正在尝试制作一个 discord 机器人,它在特定频道中发送消息,以告诉用户我的 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)

【问题讨论】:

    标签: python discord bots discord.py chatbot


    【解决方案1】:

    您可以使用TextChannel.purge 方法:

    await channel.purge(limit=1)
    

    记得发送消息之后,而不是事前

    请注意,此方法会删除频道中最旧的消息,如果您想删除机器人发送的最新消息,可以使用检查功能:

    def is_me(m):
        return m.author == client.user
    
    await channel.purge(limit=1, check=is_me)
    

    参考:

    【讨论】:

    • 好吧,如果有帮助记得采纳答案
    猜你喜欢
    • 2020-12-23
    • 2021-08-14
    • 2021-06-17
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多