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.
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 saw many tutorials online to check my code, but none of them seems to help my problem. Here is my code for joining a voice channel:
#commands.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
I'm not making anything too fancy right now, I just want a way to get this to work. Thanks.
Here's my code for joining a vc
channel = ctx.message.author.voice.channel
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)
await channel.connect()
If the problem still persists, I need you to send more part of the code (Initialisation and Execution)
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 using Discord.py how can I make the bot execute some operations when a user post a new message?
I highly recommend reading through the docs first before coming here to ask a question. However, you can use a client event and on_message to check every new message, for example:
#client.event
async def on_message(message):
if message.content == "Hi":
await message.channel.send("Hello!")
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]