I am trying to create a command that deletes a specific user's last message, so if 5 users sent 5 messages, I could use !>discard <usermention> (let's say the 3rd user) to delete user 3's last message.
I am having trouble with setting this command up and currently don't know how to make it with the skills I currently have.
You could have the on_message(msg): event that would place every message into a global list (maybe even a .txt file if you want to prevent losing the list when the bot shuts down)
#client.event
async def on_ready():
global messageList = []
#client.event
async def on_message(msg):
global messageList
messageList.append(msg)
And then delete the one you want with a command
#client.command()
async def discard(*args):
del messageList[int(args[0])+1]
Related
I want to make a word chaining bot on discord, and I want it to save all the messages in the channel so I can check for duplicates, is this possible?
Of course this is possible. And it is extremely easy to do.
import discord
client = discord.Client()
log = open("log.txt", "a")
def add_message(msg):
log.write(msg)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
add_message(message) # You can check the channel with message.channel.id
client.run('your token here')
This should work
Edit: If your concern is to check every message against every new message, as other said, switch to a database.
MySql is very easy to setup (compared to PostgreSQL) and connect to Python, and you can check for duplicates very easily.
I would like to add a command to lockdown an entire server and revert the lockdown. It works, but it changed ALL the channels send messages permission to False/True. I'm asking if there is a way to set the permissions to neutral and not to change channel permissions if it is already false or true. Here is my current code:
#Lockdown
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for channel in ctx.guild.channels:
await channel.set_permissions(ctx.guild.default_role, send_messages=False)
await ctx.send('The server is now on lockdown!')
#unlockdown
#client.command()
#commands.has_permissions(manage_channels=True)
async def unlockdown(ctx):
for channel in ctx.guild.channels:
await channel.set_permissions(ctx.guild.default_role, send_messages=True)
await ctx.send('Server is now unlocked')
Yes you can, passing None will delete the overwrites for the channels, putting it back to normal.
I need to make a command to delete every channel (text and voice) in a discord server with discord.py. Can someone provide me with the code to do this. Right now i have this, which only deletes text channels. How do i make it for both at the same time?
#bot.command(name="deleteall")
async def delete_channels(context):
[await channel.delete()
for channel in context.guild.text_channels]
This seems like an oddly destructive command, you should be careful with its use and definitely add permissions checks.
To delete the voice channels, you'd just repeat the code you've already written, but just use the voice_channels list.
e.g.
[await channel.delete() for voiceChannel in context.guild.voice_channels]
#bot.command()
async def delchannels(ctx):
for c in ctx.guild.channels: # iterating through each guild channel
await c.delete()
You can also vary this specifically for text channels or voice channels using:
#bot.command()
async def delchannel(ctx, channel: discord.TextChannel):
await channel.delete()
And alter the argument type for specific channel types: "VoiceChannel" or "CategoryChannel".
I'm trying to store messages with a discord bot, so that I can learn how the elements of messages vary between messages.
However i am new to some aspects of this coding- i.e. decorators. Currently the piece of my bots code that interacts with messages is this:
messages=[]
#bot.event
async def on_message(message,messages):
print("someone said something")
messages=messages+message
if message.author == bot.user:
return messages
I think this is wrong. What I am trying to do is add a message to messages each time the event happens, so that I can later go through that variable and see how the different elements of messages change.
How do i change the above to allow that?
You can use only 1 parameter in on_message event. Also, you can't append things to a list with +. And Also storing data in variable is not a good idea because whenever you restart the bot, it'll be deleted. You can simply store them in a txt file.
#bot.event
async def on_message(message):
print("someone said something")
file = open('messages.txt', 'a')
file.write(message.content + '\n')
file.close()
EDIT
If you want to store all the information of the message, you can do:
file.write(f'{message}\n')
or
file.write(str(message) + '\n')
I want to delete all the messages in a channel apart from the pinned ones, I tried this solution
On message delete message Discord.py
but it's the non-rewrite version so it's not working for me sadly.
You can only bulk delete (purge) a maximum of 100 messages at a time, but this should do the trick.
#client.command()
async def purge(ctx):
def not_pinned(msg):
return not msg.pinned
purged = await ctx.channel.purge(limit=100, check=not_pinned)
await ctx.send(f"Successfully removed {len(purged)} non-pinned messages!")
References:
TextChannel.purge()
Message.pinned