webneko
webneko

Reputation: 207

discord.py not able to delete author messages

I am using Discord.py version 1.0.0. I am trying to write an echo command that, when given a message, will echo the message and delete the command from the chat. Here is an example of my code.

client = Bot(description="Test bot", command_prefix="&", pm_help = False)

@bot.command(pass_context=True)
async def echo(ctx):
  await client.send(ctx.message)
  await client.delete_message(ctx.message)

The errors I receive tell me that ctx does not have an attribute called "delete_message". I have tried with just delete(). I have looked at others having something of a similar issue, however the solutions did not help me. Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 3779

Answers (2)

Patrick Haugh
Patrick Haugh

Reputation: 60944

If you're on 1.0, you can lose pass_context and client.send should be ctx.send. You can also write the function signature of the command, using Keyword-Only Arguments, so that you only echo the message, ignoring the &echo

from discord.ext.commands import Bot

client = Bot(description="Test bot", command_prefix="&", pm_help = False)

@client.command()
async def echo(ctx, *, msg):
  await ctx.send(msg)
  await ctx.message.delete()

client.run('token')

Upvotes: 1

Sam Rockett
Sam Rockett

Reputation: 3205

In discord.py/rewrite (1.0.0), Delete is a method on the message, not on the client. This is the same for every function affecting a message/channel/guild etc.

Instead of doing

await client.delete_message(ctx.message)

try doing

await ctx.message.delete()

Upvotes: 4

Related Questions