Deleting a bot's message in discord.py

自古美人都是妖i 提交于 2021-02-07 14:33:24

问题


I have already made all of the proper imports and I have tried looking for answers from other posts but it seems to not quite fit my issue. I am trying to randomly send a message, which I can do. However I can not seem to delete the messages after a certain cool down time. The cool down time is not the issue however. It is deleting the bots message. I know how to delete a user's message but I have very little idea on how I would delete the bots message. Any help would be nice. Here is my code with the exception of my token ID and imports.

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)

回答1:


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)

You have to capture the message object that send_message produces, and then send that object to delete_message




回答2:


This is for the rewrite version, but still works:

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)



回答3:


for Discord.py versions 1.0.0 and up it is now: i understand you did not ask how to delete a sender's message but its here anyway...

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")




回答4:


Now (discord.py v1.3.0), just:

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')


来源:https://stackoverflow.com/questions/49159850/deleting-a-bots-message-in-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!