Ben Keez
Ben Keez

Reputation: 57

DISCORD PY The command for deleting author messages isn't executing

I want it so that it autodeletes the author's message (*answer84) after it is typed, here is my code:

@client.command()
async def on_message(message):
    await client.process_commands(message)
    if message.content.startswith('*answer84'):
        await ctx.message.delete()

For some reason it just doesn't delete the message,(there is no traceback error message) any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 130

Answers (1)

earningjoker430
earningjoker430

Reputation: 468

There are just a few errors with your code:

Event

You need to use the event decorator rather than a command() decorator. This is "A decorator that registers an event to listen to.". Read more about event references here.

Deleting Message

Since the on_message event only takes in message as a parameter, there is no such thing as ctx. Remove the ctx, and it works!

Code

@client.event #changed decorator to event
async def on_message(message):
    if message.content.startswith('*answer84'):
        await message.delete() #removed ctx

    await client.process_commands(message)

Edit

Moved client.process_commands to the bottom of the event, good practice to do so.

Upvotes: 3

Related Questions