I need to make a lock command for a bot I'm making with discord.js but I don't know how. I need it to lock the channel when I do ".lock" by changing "Send messages" setting for the "Community" role to false, and set it back to True when I do ".unlock".
Does anyone know how to do this?
Here are the steps:
Get the "community" role's id. Store it in some variable (I'm calling it communityRoleId in this example).
Call <Message>.channel.permissionOverwrites.edit(communityRoleId, { SEND_MESSAGES: false }) when someone calls the lock command. This creates a permission overwrite for the community role saying that that role can't send messages in the channel.
For the unlock command, you can either delete the permission overwrite you made with the lock command or edit it so the community role can send messages again. To delete it, you can just <Message>.channel.permissionOverwrites.delete(communityRoleId), and to edit it, you can <Message>.channel.permissionOverwrites.edit(communityRoleId, { SEND_MESSAGES: true })
Edit: Be sure to also run checks to see if the person using the command has an "admin" role or sufficient permissions or something like that, you don't want random people being able to lock channels!
Related
I am currently trying to find a way to execute some code whenever a user is granted a specific role
However sadly I havent found any good resources on it
I imagine there could be a way of using the AUDIT log ,however Id like to avoid using the audit log due to security concerns
client.on('guildMemberUpdate', async(before, after) => {
const role = before.guild.roles.cache.get('ROLE_ID');
if(!before.roles.has(role) && after.roles.has(role)){
//code
}
})
Pretty simple, guildMemberUpdate event is fired on a guild member update, ie a role update.
#guildMemberUpdate
I’m making a command that should only be ran by people that can kick or ban, assuming that the role they have is some sort of mod or admin. I’ve only seen answers for previous versions, like .hasPermissions.
what do i do?
As said in this question (self-answered by me), .hasPermission() has been removed. Use .permissions.has() instead
if (message.member.permissions.has("KICK_MEMBERS")) {
// member HAS kick members permissions
}
You can also check multiple permissions at the same time. This is just an example that would probably not be used
if (message.member.permissions.has(["MANAGE_GUILD", "SEND_MESSAGES"])) {
// member HAS *BOTH* permissions to send messages and manage server
}
You can check all the permission flags at the discord.js docs for Permissions.FLAGS
It's really simple
if (message.member.permissions.has('KICK_MEMBERS') || message.member.permissions.has('BAN_MEMBERS')) {
//your code here
}
Let say I want to make a lock command (making only admins be able to say something in a channel) and I want a specific role to be overwrite (not #everyone). How could I do that?
To simply it. How can I disable SEND_MESSAGE for a specific role? /
You will be able to achieve this via GuildChannel.permissionOverwrites which returns the PermissionOverwriteManager class. Within that class, you will be able to use the .edit() method which edits permission overwrites for a user or role in this channel, or creates an entry if not already present.
In the first parameter of the .edit() method, you can input the role ID as a string, and in the options, you can input the property, or as known as the permission, with a value of true to enable or value false to disable.
An example of this is the following: (code according to the documentation from https://discord.js.org/#/docs/discord.js/stable/class/PermissionOverwriteManager)
// Edit or Create permission overwrites for a message author
message.channel.permissionOverwrites.edit(message.author, {
SEND_MESSAGES: false
})
.then(channel => console.log(channel.permissionOverwrites.cache.get(message.author.id)))
.catch(console.error);
You can use GuildChannel.permissionOverwrites.edit to set permission overwrite.
The code would be:
message.channel.permissionOverwrites.edit('replace with role id', {
SEND_MESSAGE: false
})
Slash command version:
interaction.channel.permissionOverwrites.edit('replace with role id', {
SEND_MESSAGES: false
})
I want to make a small if statement to see if my bot has administrator privileges in the server.
[bot = new Discord.Client();]
Any help?
I tried:
if(!bot.guild.hasPermission("ADMINISTRATOR") return msg.author.send(":x: I need administartor priviliages in"+bot.guild.name+"! :x:")
bot.guild (client.guild) doesn't exist.
client.guilds is a collection of all the guilds your bot is in, mapped by their IDs.
You can check if a member / the bot has a permission using GuildMember's .hasPermission method.
if (!message.guild.members.get(client.user.id).hasPermission("ADMINISTRATOR")) return message.reply("I need Administrator permissions!")
Shortest way possible would be
if (!message.guild.me.hasPermission("ADMINISTRATOR")) return;
So I've looked at a fair few forum posts and can't find something fitting my code.
I'm trying to do something like this...
case "setnick":
if (args[1]) {
if (message.guild.me.hasPermission("MANAGE_NICKNAMES")) {
if (message.member.hasPermission("CHANGE_NICKNAME"))
message.member.setNickname(args[1])
else message.channel.send("You do not have the permissions! **(CHANGE_NICKNAME)**")
}
else message.channel.send("The bot does not have the permissions! **(MANAGE_NICKNAMES)**")
}
else message.channel.send("There's no argument to set your nickname too! **Eg. f!setnick NICKNAME**")
break;
So it checks if there is a argument like
f!setnick NICKNAME
Then checks if the bot has permission MANAGE_NICKNAMES
if it doesn't it sends a chat message.
And it then checks for if the user has the permission CHANGE_NICKNAME
so i'm wondering why its not working.
Any reason why?
Have you tried checking if it works on other users besides yourself? Bots can not do administrator commands (like change someone's nickname) if you are higher/the same in the hierarchy than it, and seeing the owner is the ultimate power it is probably returning a permission error.
Try catching it and see what the error is
message.member.setNickname(args[1]).catch(e=>console.log(e))
if it returns DiscordAPIError: Privilege is too low... then my theory is correct. It should work for lower users.
message.member.me doesn't get the bot's user, it gets the user sending the message. Try message.guild.members.find("id", client.user.id).hasPermission("MANAGE_NICKNAMES")