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.
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()
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')
Related
How can I delete a single message sent by a user using TwitchIO?
#bot.event
async def event_message(ctx):
await ctx.content.delete() # Does not work
await ctx.content.remove() # Does not work
await ctx.channel.timeout(ctx.author, 1) # Does not work
The question is older, but I'll answer it anyway.
Twitchio doesn't directly support this.
But you can delete individual messages in Twitch Chat, see the Twitch IRC documentation.
CLEARMSG (Twitch Commands)
You need the message ID for this. You get the ID in the message tags.
Message tags
Code example:
async def event_message(message):
if not message.author.name == self.bot.nick:
message_id = message.tags['id']
await message.channel.send(f"/delete {message_id}")
If you want to timeout someone, do the following:
await message.channel.timeout(message.author.name, 120, f"reason")
Twitchio documentation Channel.timeout
I am making a poll command, the bot will send a ctx message and will say the poll question. I want to make it so when the poll message is sent, the bot adds two reaction, a thumbs up and a thumbs down. I have tried several different ways but non of them work. Here is the code from my latest try (everything is already imported)
reactions = ["👍", "👎"]
#bot.command(pass_context=True)
async def poll(self, ctx, message,*, question):
poll_msg = f"Poll: {question} -{ctx.author}"
reply = await self.bot.say(poll_msg)
for emoji_id in reactions:
emoji = get(ctx.server.emojis, name=emoji_id)
await message.add_reaction(reply, emoji or emoji_id)
The code is all over the place because I tried putting different solutions together to see if it would work but it doesn't work at all.
It looks like you're operating from some old examples. You should read the official documentation to find examples of the modern interfaces.
from discord.ext import commands
from discord.utils import get
bot = commands.Bot("!")
reactions = ["👍", "👎"]
#bot.command()
async def poll(ctx, *, question):
m = await ctx.send(f"Poll: {question} -{ctx.author}")
for name in reactions:
emoji = get(ctx.guild.emojis, name=name)
await m.add_reaction(emoji or name)
I'm making a bot with a command that send a file with all the previously executed commands in DM but I can't find a way to delete bot messages. Is there a way to do it or it's just impossible ?
I've tried to make a clear command for this specific case, I've tried this : https://www.reddit.com/r/Discord_Bots/comments/c1tf6t/dm_message_deletion_scriptbot/
but it didn't work for me.
The reddit code :
#client.command()
async def clear_dm(ctx):
user_dm = (client.get_user(610774599684194307)).dm_channe
messages_to_remove = 1000
async for message in user_dm.history(limit=messages_to_remove):
if message.author.id == client.user.id:
await message.delete()
await asyncio.sleep(0.5)
The bot messages should be deleted but when I run the command I get an exception AttributeError: 'ClientUser' object has no attribute 'dm_channel' the others methodes that I've tried raised similar errors (but I can't show you the code since I've delted it :c)
Users have a User.history attribute that you can use directly.
#client.command()
async def clear_dm(ctx):
messages_to_remove = 1000
async for message in client.get_user(610774599684194307).history(limit=messages_to_remove):
if message.author.id == client.user.id:
await message.delete()
await asyncio.sleep(0.5)
I'm having a lot of trouble with discord.py rewrite and its migration. I looked at the migrating to v1.0 site and it said to put in message.delete() and so I did but I realised that wasn't working so I put in ctx aswell. But that put it to an error. There are two commands with this error at the moment.
I have already tried putting the message into a variable.
#client.command()
async def clear(ctx, amount=100):
message = ctx.message
channel = ctx.message.channel
messages = []
await ctx.channel.purge(limit=int(amount+1))
mymessage = await channel.send('Messages deleted')
await ctx.message.delete(mymessage)
#client.command()
async def verify(ctx, *, arg):
print(ctx.message.channel.id)
print(ctx.message.author)
if ctx.channel.id == 521645091098722305:
role = await ctx.guild.create_role(name=arg)
await ctx.message.author.add_roles(role)
mymessage = await ctx.send('Done! Welcome!')
await ctx.message.delete(mymessage)
await ctx.message.delete(ctx.message)
I expected the output to delete the message. For the clear one it deletes it and then gives it back. for the verify one it just keeps it as the same and shows the error:
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: delete() takes 1 positional argument but 2 were given
Also my role giving verify sometimes goes 3 times. I have went into task manager and killed all of the python too but it still did that. Once when I was clearing it said Done! Welcome as well. If you can answer this question as well, I would be pleased! Thank you in advance.
Message.delete doesn't take any arguments. It's a method that you call on the message you want to delete. Change
await ctx.message.delete(mymessage)
await ctx.message.delete(ctx.message)
to
await mymessage.delete()
await ctx.message.delete()
#client.command(pass_context=True)
async def delete(ctx, arg):
arg1 = int(arg) + 1
await client.purge_from(ctx.message.channel, limit=arg1)
!delete 10 - delete the last 10 posts
from discord.ext.commands import Bot
import secrets
from time import sleep
discordBot = Bot(command_prefix = ".")
listStrings = ['add a reaction', 'adnd']
#discordBot.event
async def on_read():
print('Client logged in')
#discordBot.command()
async def s(*args):
return await discordBot.say (" ".join(args))
#discordBot.command()
async def close():
quit()
discordBot.run(secrets.token)
I wanted to say ".s (text)" and that the bot says the text(works already) but deletes your message, how can i do this? i got the part working of on_message:
#discordbot.event
async def on_message(message):
await discordBot.delete_message(message)
What is the best way to do this? Thanks in advance.
I'm sorry you haven't got an answer for 26 days. If you are still looking for an answer, I hope I can provide some help. So I ran your code, and the bot ran the command, my message deleted, then the bot's message also deleted. One way to fix this is to provide an if statement which means the bot only deletes the message if the author is not a bot.
#test_bot.event
async def on_message(message):
if not (message.author).bot:
await test_bot.delete_message(message)
await test_bot.process_commands(message)
Another way to remove only messages that are the commands is to use another if statement. The following code is specific, so you only delete commands, not just all user's messages if they aren't a bot. The following code uses message.content which returns the message as a string.
#test_bot.event
async def on_message(message):
if message.content.startswith('.s'):
await test_bot.delete_message(message)
await test_bot.process_commands(message)