How can I delete bot message in DM? - python-3.x

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)

Related

how to make a command that only works for a certain role in a certain channel, otherwise it sends them a message to ask for a role

I'm currently coding a discord bot on the side and I'm stuck on a command that I'm trying to only make certain role to use it, when the wrong role type the command he got a reply to get the right role and I'm wondering how I can mix those 2 commands in 1
thanks for your help, im a noob since i just start coding
#commands.has_any_role('Sluts', 'Customer')
#commands.cooldown(1, 17992, commands.BucketType.user)
async def generate(ctx):
if ctx.message.channel.name == 'Channel_id':
emoji2 = bot.get_emoji(932456512180338759)
await ctx.reply('Initialized')
await ctx.message.add_reaction(emoji1)
await ctx.message.add_reaction(emoji2)
#await main.script()
await ctx.author.send('DM: Done! Enjoy!')
else
await ctx.send('You can not use this command here: checkout #channel_id')
and this line
#bot.command()
#commands.has_any_role('Leaker', 'Customer')
#commands.cooldown(1, 300, commands.BucketType.user)
async def generate(ctx):
emoji2 = bot.get_emoji(877247467459084298)
await ctx.message.add_reaction(emoji2)
await ctx.send('You can not use this command here,open a ticket to get the right role')
await ctx.author.send('DM: ')
I suggest not using commands.has_any, because you need to use on_command_error which is not the nicest thing.
You should check if. a certain role is in the author
if special_user_id in [role.id for role in ctx.author.roles]

Python - How do I make a join command

I'm trying to create a ".join" command for my bot but everything I've tried didn't work & I tried a ton of things
Here's something I've tried, I tried it in a few other ways too:
#client.command(pass_context=True)
async def join(ctx):
channel=ctx.message.author.voice.VoiceChannel
await client.join_VoiceChannel(channel)
It gives this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'VoiceChannel'
join_voice_channel
works for older versions of discord.py
This code block should work:
#client.command(pass_context=True)
async def join(ctx):
message = ctx.message
channel_id = ctx.message.author.voice.channel.id
channel = client.get_channel(channel_id)
await channel.connect()
You use join_voice_channel to make the bot join a voice channel as it says in the Async docs
Try this:
#client.command(pass_context=True)
async def join(ctx):
author=ctx.message.author
await bot.join_voice_channel(author.voice_channel)

Deleting specific messages that was sent by the bot and sent by the user. Discord.py rewrite

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

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.
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')

discord.py module how do i remove the command text afterwards?

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)

Resources