5
votes

I have this code in Python:

import discord
client = commands.Bot(command_prefix='!')

@client.event
async def on_voice_state_update(member):
    channel = client.get_channel(channels_id_where_i_want_to_send_message))
    response = f'Hello {member}!'
    await channel.send(response)

client.run('bots_token')

And I want the bot to delete its own message. For example, after one minute, how do I do it?

3

3 Answers

10
votes

There is a better way than what Dean Ambros and Dom suggested, you can simply add the kwarg delete_after in .send

await ctx.send('whatever', delete_after=60.0)

reference

2
votes

Before we do anything, we want to import asyncio. This can let us wait set amounts of time in the code.

import asyncio

Start by defining the message you send. That way we can come back to it for later.

msg = await channel.send(response)

Then, we can wait a set amount of time using asyncio. The time in the parentheses is counted in seconds, so one minute would be 60, two 120, and so on.

await asyncio.sleep(60)

Next, we actually delete the message that we originally sent.

await msg.delete()

So, altogether your code would end up looking something like this:

import discord
import asyncio

client = commands.Bot(command_prefix='!')

@client.event
async def on_voice_state_update(member, before, after):
    channel = client.get_channel(123...))
    response = f'Hello {member}!'
    msg = await channel.send(response) # defining msg
    await asyncio.sleep(60) # waiting 60 seconds
    await msg.delete() # Deleting msg

You can also read up more in this here. Hope this helped!

0
votes

This shouldn't be too complicated. Hope it helped.

import discord
from discord.ext import commands
import time
import asyncio
client = commands.Bot(command_prefix='!')

@commands.command(name="test")
async def test(ctx):
    message = 'Hi'
    msg = await ctx.send(message)
    await ctx.message.delete() # Deletes the users message
    await asyncio.sleep(5) # you want it to wait.
    await msg.delete() # Deletes the message the bot sends.
 
client.add_command(test)
client.run(' bot_token')