-2

So i have made a command $mute @name which basically mutes a player when typed. But what i want is when i type the command , after 5 or 10 seconds the command input line that is $mute @name get removed from the chat .

Smit
  • 1
  • 2

2 Answers2

0

That would mean that you would have to wait for 5-10 seconds after the command, and then delete it. Just make sure your bot has permissions to delete messages:

@client.command()
async def mute(ctx, member: discord.Member):
    muted_role = ctx.guild.get_role(YOUR_ROLE_ID)  # Make sure not to put it in a string
    await member.add_roles(muted_role)
    await asyncio.sleep(5)  # Make sure you import asyncio, also change the 5 to whatever seconds you would like
    await ctx.message.delete()

The code above will give the user the Muted role (whichever role you specify via the id of that role) and will give that role to the member. Then it will wait 5 seconds (you can change this to whatever you would like) and then delete the command message.

IPSDSILVA
  • 1,667
  • 9
  • 27
0

Here are two methods you could use.


The first way would be to use await asyncio.sleep using the asyncio import. The reason we don't use time.sleep would be because this would be a blocker. A blocker means that if you use this command in one place, you would stop the entire bot and no one else would be able to use commands until it's done. An example would be as below:

import asyncio

@client.command()
async def test(ctx):
    await ctx.message.delete() # deletes message sent by user
    # do some things here
    msg = await ctx.send("done")
    await asyncio.sleep(5) # waits for 5 seconds
    await msg.delete() # deletes message sent by bot, aka 'done'

The second way would be to use delete_after assuming you're only deleting the bot's message. I wasn't able to get the direct link to the docs, but they say:

delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent. If the deletion fails, then it is silently ignored.

The example is as follows:

@client.command()
async def test2(ctx):
    await ctx.send("done", delete_after=5)

Some other referral links:


Note: Both of these commands have been tested, and both commands work as expected.

Bagle
  • 2,326
  • 3
  • 12
  • 36