在 discord.py 中删除机器人的消息

Cod*_*ath 5 python-3.x discord discord.py

我已经进行了所有正确的导入,并且我尝试从其他帖子中寻找答案,但它似乎不太适合我的问题。我正在尝试随机发送一条消息,我可以做到。但是,经过一段时间的冷却后,我似乎无法删除这些消息。然而,冷却时间不是问题。它正在删除机器人消息。我知道如何删除用户的消息,但我对如何删除机器人消息知之甚少。你能帮忙的话,我会很高兴。这是我的代码,但我的令牌 ID 和导入除外。

async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
    channel = client.get_channel('397920718031159318')
    messages = ["A random cat has appeared", "oh look its a cate"]
    await client.send_message(channel, random.choice(messages))
    time.sleep(3) #I am using this as the cool down time to delete the 
                  #message
    await client.delete_message(messages)
    await asyncio.sleep(4)
Run Code Online (Sandbox Code Playgroud)

Pat*_*ugh 6

while not client.is_closed:
    channel = client.get_channel('397920718031159318')
    messages = ["A random cat has appeared", "oh look its a cate"]
    message = await client.send_message(channel, random.choice(messages))
    await asyncio.sleep(3) 
    await client.delete_message(message)
    await asyncio.sleep(4)
Run Code Online (Sandbox Code Playgroud)

您必须捕获send_message产生的消息对象,然后将该对象发送到delete_message

  • 再次感谢您帮助我制作这个机器人。我不妨将您包括在文档中。 (3认同)

小智 5

现在(discord.pyv1.3.0),只需:

import discord

from discord.ext.commands import Bot


bot = Bot()

@bot.command()
async def ping(ctx):
    await ctx.send('pong!', delete_after=5)

bot.run('YOUR_DISCORD_TOKEN')
Run Code Online (Sandbox Code Playgroud)

  • 无论他们是否使用它,这个答案都是 2020 年最相关的。 (2认同)