Error with discord.py and delete messages function

Question:

I am trying to make a command that will delete the specified amount of messages. I am getting this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 38, in deleteMessages
    await ctx.purge(int(amount))
AttributeError: 'Context' object has no attribute 'purge'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'purge'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    ret = await coro(*args, **kwargs)
TypeError: deleteMessages_error() takes 0 positional arguments but 2 were given

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 979, in on_message
    await self.process_commands(message)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 976, in process_commands
    await self.invoke(ctx)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 943, in invoke
    await ctx.command.dispatch_error(ctx, exc)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 424, in dispatch_error
    await injected(ctx, error)
  File "/home/runner/Erlc-Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 77, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: deleteMessages_error() takes 0 positional arguments but 2 were given


I get that error when I run the command !deleteMessages 1 Here is the code that runs the delete messages function:

@has_permissions(kick_members=True)
async def deleteMessages(ctx, amount):
  await ctx.purge(int(amount))

@deleteMessages.error
async def deleteMessages_error(ctx):
  await ctx.send("You do not have permissions.")

Here is the full code:

from discord.ext import commands
import os
from discord import Member
from discord.ext.commands import MissingPermissions
from discord.ext.commands import has_permissions


channels={
  "general": 1008885573555069029,
  "rules": 1009474834725605386,
  "staffChat": 1008885573555069029,
  "announcements": 1009474859203563594
  
}

intents = discord.Intents.default()
intents.members=True

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

#######Events######
@client.event
async def on_ready():
  print("Bot is up and ready!")
  await client.change_presence(activity=discord.Game('ERLC'))

@client.event
async def on_member_join(member):
  await member.send("Welcome to the mafia!")
  

######Commands#######

@client.command()
@has_permissions(kick_members=True)
async def deleteMessages(ctx, amount):
  await ctx.purge(int(amount))

@deleteMessages.error
async def deleteMessages_error(ctx):
  await ctx.send("You do not have permissions.")

@client.command()
async def getCustomBot(ctx):
  ctx.send("Please email me at my email and I will be happy to make you a custom bot")

  
@client.command()
@has_permissions(ban_members=True)
async def rulesEmbed(ctx):
  embed = discord.Embed(title="Rules", description ="1. Do not spam.n2. Don't be racist.n3. No NSFW. Anywhere.n4. Swearing is allowed, just nothing racist.n5. No Ads. n6. No server raiding. Your discord account will be banned because it is against TOS.n7. NO COPS. If you are on this server. You will be kicked on the police team.", color=0xFFFF00)
  await client.get_channel(channels["rules"]).send(embed=embed)

@rulesEmbed.error
async def rulesEmbed_error(ctx, error):

    await ctx.send("You do not have permissions to announce the rules.")

@client.command()
@has_permissions(ban_members=True)
async def announce(ctx, *announcement):
  embed = discord.Embed(title="Announcement", description=" ".join(announcement[:]), color=0xFFFF00)
  embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
  await ctx.send(embed=embed)

@announce.error
async def announce_error(ctx, error):

    await ctx.send("You do not have permissions to announce.")




@client.command()
@has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, reason):
  embed = discord.Embed(title="Kicked", description="You have been kicked.", color=0x00FFFF)
  embed.set_thumbnail(url='https://cdn-icons.flaticon.com/png/512/4116/premium/4116543.png?token=exp=1660745700~hmac=49c8d32b9cc08000555d098f07e5f4ff')
  embed.add_field(name="Who was I kicked by?", value="You have been kicked by "+ctx.author.name+".")
  embed.add_field(name="What was I kicked for?", value="You were kicked for "+reason+".")
  
  await member.send(embed=embed)
  await member.kick(reason=reason)
  await ctx.send(f"User {member} has been kicked for "+reason)
  
  
@kick.error
async def kick_error(ctx, error):
  if isinstance(error, commands.MissingPermissions):
    await ctx.send("You do not have permissions to kick.")

@client.command()
@has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, reason):
  
  embed = discord.Embed(title="Banned", description="You have been banned", color=0xE10600)
  embed.set_thumbnail(url='https://cdn-icons-png.flaticon.com/512/486/486256.png')
  embed.add_field(name="Who was I banned by?", value="You have been banned by "+ctx.author.name+".")
  embed.add_field(name="What was I banned for?", value="You were banned for "+reason+".")
  await member.send(embed=embed)
  
  
  await member.ban(reason=reason)
  await ctx.send(f"User {member} has been banned for "+reason)
  
  
@ban.error
async def ban_error(ctx, error):
  if isinstance(error, commands.MissingPermissions):
    await ctx.send("You do not have permissions to ban.")


  
  





client.run(os.getenv("TOKEN"))

Asked By: Ipad 6456

||

Answers:

Error 1:

Context has no attribute purge. You are probably looking for purge.

Replace ctx.purge with ctx.channel.purge

Error 2:

An error handler accepts two parameters, the context and the error raised.

Add a parameter to the function deleteMessages_error which corresponds to the error raised by the command.

Answered By: Sandy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.