F
F
fdsaghoi2020-09-23 00:24:46
Python
fdsaghoi, 2020-09-23 00:24:46

How to clear messages?

I'm trying to clear messages in a text channel with a command. I can't figure out why it doesn't work.
The code:

from discord.ext import commands
import asyncio
import discord

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

@bot.command()
async def clear(ctx, numb,):
    channel = ctx.channel
    numb = int(numb)
    counter = 0
    async for i in channel.history(ctx.message.channel, limit=numb):
        if counter < numb:
            await channel.delete_messages(i)
            counter += 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KeySh1e, 2020-09-23
@fdsaghoi

What error does it give?
You can also use this command:

import discord
from discord.ext import commands
import asyncio

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

@bot.command()
async def clear (ctx, numb):
    channel = ctx.channel
    numb = int(numb)
    await channel.purge(limit=numb+1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question