Xenon
Xenon

Reputation: 11

How can I delete entire messages in a text channel in Discord with discord python library

I used a code from a asked question in stack overflow but the functions used in that seems to be changed.

Code :

from discord.ext.commands import Bot
.
.
@client.command(pass_context = True)
async def clear(ctx, number):

    mgs = [] #Empty list to put all the messages in the log
    number = int(number) #Converting the amount of messages to delete to an integer
    async for x in client(ctx.message.channel, limit = number):
        mgs.append(x)
    await client.delete_messages(mgs)

Error:

async for x in client(ctx.message.channel, limit = number): TypeError: 'Bot' object is not callable

and

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Bot' object is not callable

Upvotes: 1

Views: 3026

Answers (3)

NamelesKeed
NamelesKeed

Reputation: 21

A simpler way to do the clear command is this:

First of all replace from discord.ext.commands import Bot with from discord.ext import commands

Then replace

@client.command(pass_context = True)
async def clear(ctx, number):

    mgs = [] #Empty list to put all the messages in the log
    number = int(number) #Converting the amount of messages to delete to an integer
    async for x in client(ctx.message.channel, limit = number):
        mgs.append(x)
    await client.delete_messages(mgs)

with

@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=1):
    await ctx.channel.purge(limit=amount)

The 1 in amount is just the default value and you can change it to anything you want

Upvotes: 0

SBECK1
SBECK1

Reputation: 267

A really easy way to do it in the new versions of discord.py would probably look something like this:

@client.event
async def on_message(message):
    if '-clear all' in message.content and message.author.permissions_in(message.channel).manage_messages:
        deleted = await message.channel.purge(limit=10000, check=is_not_pinned)
    await message.channel.send('All messages deleted.'.format(deleted))
    await client.process_commands(message)

You could also use a regular command for this so you won't need await client.process_commands(message) but the function you are after will still be await message.channel.purge(limit=amount, check=is_not_pinned) That way only people with the manage messages permission will be able to use this command and it will not deleted pinned messages.

You can set the amount to an incredible high number so it will just delete close to, if not everything. I only tried it for around 300 and it worked perfectly (may take some time though).

Upvotes: 1

Kartikeya Gullapalli
Kartikeya Gullapalli

Reputation: 131

It looks like you are using an older version of discord.py

If you want to clear messages in a channel in the latest version, it is very simple. You simply have to use the 'discord.TextChannel.purge()' method.

@client.command()
async def clear(ctx, amount=None):
    if amount is None:
        await ctx.channel.purge(limit=5)
    elif amount == "all":
        await ctx.channel.purge()
    else:
        await ctx.channel.purge(limit=int(amount))

A difference you may notice is that you no longer have to use 'pass_context' in the 'client.command()' decorator.

Also, at the top of your code, you shouldn't import Bot directly, rather replace that from statement with

from discord.ext import commands

and instantiate your client with

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

Upvotes: 1

Related Questions