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

时间:2018-03-07 19:30:56

标签: 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)

4 个答案:

答案 0 :(得分:2)

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)

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

答案 1 :(得分:2)

这是重写版本,但仍然有效:

channel = client.get_channel('397920718031159318')
messages = ["A random cat has appeared", "oh look its a cate"]
await(await client.send_message(channel, random.choice(messages))).delete(delay=3)
await asyncio.sleep(4)

答案 2 :(得分:1)

对于Discord.py 1.0.0及更高版本,现在为: 我了解您不是在询问如何删除发件人的邮件,而是在这里...

import asyncio
channel = 397920718031159318 #get the channel
## send the message
message = await ctx.send('message')
## wait for 4 seconds
await asyncio.sleep(4)
## delete the message
await message.delete()

## ^^ To delete the Bots message ^^ ##
## vv To delete the senders message vv ##

## get the message
message = ctx.message
## wait for 4 seconds again
await asyncio.sleep(4)
## delete the message
await message.delete()

#############################

## You can edit the message in about the same way:

message = await ctx.send('Old Message')
await asyncio.sleep(0.5)  # this is so the message has time to be read
await message.edit(content="New Message")

答案 3 :(得分:0)

现在(discord.py v1.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')
相关问题