Dweller
Dweller

Reputation: 15

HOW TO USE PURGE COMMAND INSIDE ON_MESSAGE

I found a purge command, but it doesn't work for me because I use client = discord.Client(command_prefix = '$', description = ' ') and not client = commands.Bot(command_prefix = '$') And I use a lot of commands in

@client.event 
async def on_message(message):

so I wanted to use purge command in on_message too. Thanks!

Purge command:

@client.command()
async def clear(ctx, amount = 5):
    await ctx.channel.purge(limit = amount)

Upvotes: 0

Views: 159

Answers (2)

Aadi
Aadi

Reputation: 106

I deleted my first answer as I saw I could improve, I ended up finding you can do

ctx = await client.get_context(message)
split = message.content.split()
if split[0] == "$clear": #Checking if the message is the clear command, you can also use message.content.tolower().startswith("$clear"):
    if len(split) == 2:
        num = 0
        try:
            num = int(split[1]) #checking if the second param <amount> is an int
        except ValueError:
            await message.channel.send("<amount> in $clear <amount> must be a number")
            return
        await ctx.channel.purge(limit = num)
    else:
        await message.channel.send("Please enter the command as $clear <amount>")

It works fine for me. Also client has to be

client = commands.Bot(params)

Upvotes: 1

ARealWant
ARealWant

Reputation: 111

You have to do the same if-Statement as you're already doing it for other commands in the on_message event. Due to you're using it in an on_message event, you're not really able to set an amount on using the command, so you've to predefine it.

Define the amount and define a channel to purge in. You can get the channel by using discord.Client.get_channel or by choosing the channel the message has been sent to message.channel.

Upvotes: 1

Related Questions