With this snippet, you will be able to give roles without worrying about the members of the public orginial code credit goes to harshavarthan!
// authenticates you with the API standard library
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith('m!removerole')) {
let userString = context.params.event.content.split(' ')[1];
let roleString = context.params.event.content.split(' ')[2];
let canRemoveRole = false;
let guild = await lib.discord.guilds['@0.1.0'].retrieve({
guild_id: `${context.params.event.guild_id}`,
});
if (!userString || !roleString) {
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `You must call this command like this: **m!removerole @username @rolename**`,
});
return;
}
if (guild.owner_id === context.params.event.author.id) {
canRemoveRole = true;
} else {
let roles = await lib.discord.guilds['@0.1.0'].roles.list({
guild_id: `${context.params.event.guild_id}`,
});
roles = roles.filter((role) => {
return context.params.event.member.roles.indexOf(role.id) > -1;
});
for (let i = 0; i < roles.length; i++) {
let role = roles[i];
canRemoveRole =
(role.permissions & (1 << 3)) === 1 << 3 ||
(role.permissions & (1 << 28)) === 1 << 28;
if (canRemoveRole) {
break;
}
}
}
if (canRemoveRole) {
try {
await lib.discord.guilds['@0.1.0'].members.roles.destroy({
role_id: roleString,
user_id: userString,
guild_id: context.params.event.guild_id,
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Removed ${roleString} from ${userString}`,
});
} catch (e) {
console.log(e);
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: [
`There was a problem removing the role.`,
`Make sure your bot's role is above the role you're trying to remove.`,
].join('\n'),
});
}
} else {
await lib.discord.channels['@0.1.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `sorry you don't have permission`,
});
}
}
//Role lock 1//