Discord.js changing perms - discord

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
})

Related

Lock command for discord.js

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!

Trying to delete all messages sent by a user with a certain role unless the message is !verify

I'm trying to make a verification system. Members are supposed to type !verify. If they don't type that, I want my bot to delete the sent message.
module.exports = {
client.on('message', (message) => {
if (message.member.roles.cache.has('793205873949278279')){
if (!message.content === '!verify') {
message.delete({ timeout: 1 });
}
}
})
}
'message.member.roles' returns an array of role objects the message author has.
If you'd like to find out whether the user has the corresponding role or not, you would first want to get the role using its ID (or name if preferred) and only then check if the user has it.
const role = message.guild.roles.cache.get('role id here') // Gets the role object using it's ID
if (!message.member.roles.cache.has(role)) return // The command will not be executed if the user does not have the corresponding role
One more thing to note: It seems like you're trying to delete the message after one millisecond - Which let's be honest is quite useless since you'll never notice the difference if it's 0 or 1 milliseconds - So my advice is to either delete it after 1000 milliseconds (1 second) if that's what you wanted to do, or not set a timeout at all.

How to set permissions?

I need to set users permissions, i tried
chanel2.overwritePermissions(message.member,{'SEND_MESSAGES': true,'READ_MESSAGES': true})
but it overwrited all permissions (for everyone not just message.member), not just add specific permissions for message.member (but for the 1 member, it does what i need)
Its best if you refer to this guide for all of the information you need to know about setting user permissions in a channel. It looks like it is erroring because message.member is not grabbing the user id to set in the overwritePermissions. Ill explain it all here, but next time try adding some error information and context to your question and code :)
You need to make sure that you have a channel object, so you can grab that like so:
const channelID = message.channel.id //alternatively you can hardcode one like so: "588868740980932620"
const channel = message.guild.channels.find(c => {return c.id === channelID})
After you grab the channel object, you can now set the channel permissions like so:
// channel is whatever your constant variable is in the section above
// message.member.user.id is identical to message.author.id
channel.overwritePermissions(message.member.user.id, {
SEND_MESSAGES: true,
READ_MESSAGES: true,
ATTACH_FILES: true
});
You can find permission flags here.
Hopefully, this helps you. DiscordJS Guide and Discord JS Docs are very helpful areas to look for help.

Creating channel permission overwrites

I am creating a bot command so that when you type !setup it sets up the whole server (creating channels and roles, etc). I have already set it to create the roles and channels but there are some channels that only specific roles can type in, whereas, other roles can only read the channel. I don't know how to set up permission overwrites for certain roles.
I have already looked on the Discord.js documentation and it doesn't help much. I receive an error that supplied parameter was neither a user nor a role, but I don't know how to gain the role ID.
message.guild.createRole({
name: 'Admin',
color: '#2494ad',
permissions: ['ADMINISTRATOR', 'VIEW_AUDIT_LOG', 'MANAGE_GUILD', 'MANAGE_CHANNELS', 'SEND_TTS_MESSAGES', 'CREATE_INSTANT_INVITE', 'KICK_MEMBERS', 'BAN_MEMBERS', 'ADD_REACTIONS', 'PRIORITY_SPEAKER', 'READ_MESSAGES', 'SEND_MESSAGES', 'MANAGE_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES', 'READ_MESSAGE_HISTORY', 'MENTION_EVERYONE', 'USE_EXTERNAL_EMOJIS', 'CONNECT', 'SPEAK', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'USE_VAD', 'CHANGE_NICKNAME', 'MANAGE_NICKNAMES', 'MANAGE_ROLES', 'MANAGE_WEBHOOKS', 'MANAGE_EMOJIS']
});
message.guild.createRole({
name: 'Requesting Role',
color: '#1bb738',
permissions: ['READ_MESSAGES', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY',]
});
//Categories and Channels
message.guild.createChannel('clan-communications', {
type: 'category',
permissionOverwrites: [{
id:'25311096',
deny: ['SEND_MESSAGES']
}]
});
//Permissions
message.channel.overwritePermissions('25311096', {
SEND_MESSAGES: false
});
break;
I would like roles to have base permissions for the whole server. But certain roles have overwrites for different channels. Instead it says the supplied parameter was neither a user nor a role.
First and foremost, welcome to Stack Overflow. Hopefully we can be of help to you.
Let's walk through a solution step by step to achieve your desired result.
When you create your roles, you should declare a variable to store them. That way, you can use what the client just created later on. Since Guild.createRole() returns a promise, we can save the fulfilled result into a variable.
Note that the keyword await must be placed in async context (within an async function).
const vip = await message.guild.createRole('VIP', { ... });
Then, when you need to use that role, you can refer to the variable.
await message.guild.createChannel('VIPs Only', {
type: 'category',
permissionOverwrites: [
{
id: vip.id,
allow: ['READ_MESSAGES']
}, {
id: message.guild.defaultRole.id, // #everyone role
deny: ['READ_MESSAGES']
}
]
});
If the role already exists, you can retrieve it from the Collection of roles returned by Guild.roles.
const testRole = message.guild.roles.get('IDhere');
const adminRole = message.guild.roles.find(r => r.name === 'Admin');
Other simple improvements.
In your code snippet, I don't see any error handling. The methods you're using return promises. If an error occurs in any, the promise state will become rejected. Any rejected promises that aren't caught will throw errors. To fix this, attach catch() methods to each promise (better for individual handling) or wrap async code in a try...catch statement.
When creating your Admin role, know that the ADMINISTRATOR permission automatically grants all other permissions (and allows users to bypass channel-specific permissions). Therefore, there's no need to list any other permission, or change the role's permission overwrites in any channels.
Your IDs in your code don't appear to be valid. Discord IDs are snowflakes, represented by strings of 18 digits. You can access them through Developer Mode in Discord. However, you shouldn't have to hard-code them with this solution, unless they already exist.
In the permissions array for your Requesting Role role, you have an extra comma.

Using emotes to give roles Discord.js

In my discord server, as a verification method, I want my bot to have all users react to the message and then get given the verified role, and remove the old role. The current code I have doesn't grant or remove roles, but doesn't error.
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("name", setup.verify));
users.removeRole(users.guild.roles.find("name", setup.default));
});
There is a problem in the current code. The messageReactionAdd event returns a User object, which does not contain the property .guilds which you attempt to access: users.guilds.r- .
Instead of this perhaps you should the id of the User object and plug that into message.guild.members.get("id here"). Then use the returned Member object with your current code. That should work!
References:
Guild Member
, Message Reaction Event, User Object
Would something like this work?
I'm not sure it will I haven't tested it. This should add the role if the message they reacted to consists of the word "role1" and if they react to a different message it will remove the role. I don't know if this is what you're going for but in theory this should work.
client.on("MessageReactionAdd", function(users) {
if (message.content === "role1") {
users.addRole(users.guild.roles.find("name", setup.verify))
} else if (!message.content === "role1") {
user.removeRole(users.guild.role.find("name", setup.default))
}
});
Are the names of the roles; setup.verify & setup.default?
If not, that's why it is not working.
Try:
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("id", ROLEIDHERE));
users.removeRole(users.guild.roles.find("id", ROLEIDHERE));
});
where ROLEIDHERE put the role id to find the role id just tag the role and before you hit send put \ before the tag and it will supply the id

Resources