how can i make the bot create a role with specific permissions? (in this case it has to set SEND_MESSAGES to disabled), this is the code i made:
var muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!muteRole) message.guild.roles.create(
{ data: { name: 'Muted', reason: 'the role is needed', permissions: ["SEND_MESSAGES" = false]} });
You will have to create the role first, then modify the permissions, this cannot be done in one function call. Here's how you can do it:
var muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!muteRole) message.guild.roles.create({
data: {
name: 'Muted',
reason: 'the role is needed'
}
})
.then(role => role.setPermissions(["ANY_PERMISSION_HERE"])
.catch(error => /* catch error (optional) */);
Or if you want, you can use JavaScript's async/await API:
try {
var muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!muteRole) muteRole = await message.guild.roles.create({
data: {
name: 'Muted',
reason: 'the role is needed'
}
});
muteRole.setPermissions(["YOUR_PERMISSIONS"]);
} catch (error) {
// catch the error (optional)
}
Related
I'm making a discord bot but i can not figure out how to make a muted role without send messages permissions. Heres what i have so far:
var role = message.guild.roles.cache.find(r => r.name === 'muted');
if(!role) {
message.guild.roles.create({
data: {
name: 'muted',
color: 0,
permissions: [SEND_MESSAGES, false],
},
})
}
Any help would be great!
You should use the client/bot function? like:
var guild = client.guilds.cache.get('guild-id'); //<-- Change guild-id to guild id
var role = guild.roles.cache.find(role => role.name == 'muted');
if(!role) {
async function mutedR() {
let role1 = await guild.roles.create({
data: {
name: 'muted',
color: 0,
},
})
guild.channels.cache.forEach(async (channel, id) => {
await channel.createOverwrite(role1, {
SEND_MESSAGES: false,
});
});
message.channel.send(`Created Muted Role For Test ${role1}`) //<-- You can change this message as well
}
mutedR()
}
Also, I perfected the code a little bit.
You can also check the code here: https://cdn.discordapp.com/attachments/817529510948896772/841117091754016808/Recording_3.mp4
I'm making a mute command for my bot where it creates a muted role if there isn't one already and gives it to the user that is mentioned in the command, currently the error im getting is;
[INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
My best guess is that this error occurs because it doesn't create the role that it is supposed to create therefor it cannot give it to the mentioned member, i may be completely wrong though.
const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');
module.exports = class MuteCommand extends BaseCommand {
constructor() {
super('mute', 'moderation', []);
}
async run(client, message, args) {
if(!message.member.hasPermission("MUTE_MEMBERS")) return message.channel.send("You do not have Permission to use this command.");
if(!message.guild.me.hasPermission("MUTE_MEMBERS")) return message.channel.send("I do not have Permissions to mute members.");
const Embedhelp = new Discord.MessageEmbed()
.setTitle('Mute Command')
.setColor('#6DCE75')
.setDescription('Use this command to Mute a member so that they cannot chat in text channels nor speak in voice channels')
.addFields(
{ name: '**Usage:**', value: '=mute (user) (time) (reason)'},
{ name: '**Example:**', value: '=mute #Michael stfu'},
{ name: '**Info**', value: 'You cannot mute yourself.\nYou cannot mute me.\nYou cannot mute members with a role higher than yours\nYou cannot mute members that have already been muted'}
)
.setFooter(client.user.tag, client.user.displayAvatarURL());
let role = 'muted' || 'Muted';
let newrole = message.guild.roles.cache.find(x => x.name === role);
if (typeof newrole === undefined) {
message.guild.roles.create({
data: {
name: 'Muted',
color: '#ff0000',
permissions: {
SEND_MESSAGES: false,
ADD_REACTIONS: false,
SPEAK: false
}
},
reason: 'to mute people',
})
.catch(console.log(err)); {
message.channel.send('Could not create muted role');
};
};
let muterole = message.guild.roles.cache.find(x => x.name === role);
const mentionedMember = message.mentions.members.first() || await message.guild.members.fetch(args[0]);
let reason = args.slice(1).join(" ");
const banEmbed = new Discord.MessageEmbed()
.setTitle('You have been Muted in '+message.guild.name)
.setDescription('Reason for Mute: '+reason)
.setColor('#6DCE75')
.setTimestamp()
.setFooter(client.user.tag, client.user.displayAvatarURL());
if (!reason) reason = 'No reason provided';
if (!args[0]) return message.channel.send(Embedhelp);
if (!mentionedMember) return message.channel.send(Embedhelp);
if (!mentionedMember.bannable) return message.channel.send(Embedhelp);
if (mentionedMember.user.id == message.author.id) return message.channel.send(Embedhelp);
if (mentionedMember.user.id == client.user.id) return message.channel.send(Embedhelp);
if (mentionedMember.roles.cache.has(muterole)) return message.channel.send(Embedhelp);
if (message.member.roles.highest.position <= mentionedMember.roles.highest.position) return message.channel.send(Embedhelp);
await mentionedMember.send(banEmbed).catch(err => console.log(err));
await mentionedMember.roles
.add(muterole)
.then(() => message.channel.send("There was an error while muting the member"))
.catch((err) => console.log(err));
}
}
The line of code where the role is created is:
message.guild.roles.create({
data: {
name: 'Muted',
color: '#ff0000',
permissions: {
SEND_MESSAGES: false,
ADD_REACTIONS: false,
SPEAK: false
}
},
reason: 'to mute people',
})
You can use either async/await or .then, but i recommend using async/await. For example:
const mentionedMember = message.mentions.members.first() || await
message.guild.members.fetch(args[0]);
const newMutedRole = await message.guild.roles.create({
data: {
name: 'Muted',
color: '#ff0000',
permissions: {
SEND_MESSAGES: false,
ADD_REACTIONS: false,
SPEAK: false
}
},
reason: 'to mute people',
})
mendtionedMember.roles.add(newMutedRole)
I am trying to make a mute command for my bot and ran into these issues. I have been getting them for a very long time now.
const { Guild } = require("discord.js");
module.exports = {
name: "mute",
description: "Mutes a member",
execute(message,args,Mention){
if (message.member.hasPermission("ADMINISTRATOR") || message.author.username === "creepersaur"){
let targetMember = message.mentions.members.first();
const guild = message.guild
let myRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!myRole) {
newRole = message.guild.roles.create({
data: {
name: 'Muted',
color: 'BLUE',
Permissions: '',
},
reason: `Make a muted role if it doesn't exist.`
})
.then(console.log)
.catch(console.error);
//message.guild.roles.Muted.setPermissions({'SEND_MESSAGES': true})
targetMember.roles.add(newRole).catch(console.error);
message.channel.send(`${targetMember.username} has been muted!`);
} else if (myRole) {
targetMember.roles.add(myRole).catch(console.error);
message.channel.send(`${targetMember.username} has been muted!`);
}
}
}
};
It will see if there is a mute role in the server and if not, then it will create a mute role to give the user. Except the permissions don't work. And because of that, it is not giving the role itself to the user I want. :(
bump x1
I don't really see anything bad in the role section but this code should have the permissions fixed.
const { Guild } = require("discord.js");
module.exports = {
name: "mute",
description: "Mutes a member",
execute(message,args,Mention){
if (message.member.hasPermission("ADMINISTRATOR") || message.author.username === "creepersaur"){
let targetMember = message.mentions.members.first();
const guild = message.guild
let myRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!myRole) {
newRole = message.guild.roles.create({
data: {
name: 'Muted',
color: 'BLUE',
Permissions: '',
},
reason: `Make a muted role if it doesn't exist.`
})
.then(console.log)
.catch(console.error);
message.guild.channels.cache.forEach(ch =>
{
if(ch.type == "text") {
ch.updateOverwrite(
newRole, {
SEND_MESSAGES: false
}
targetMember.roles.add(newRole).catch(console.error);
message.channel.send(`${targetMember.username} has been muted!`);
} else if (myRole) {
targetMember.roles.add(myRole).catch(console.error);
message.channel.send(`${targetMember.username} has been muted!`);
}
}
}
};
I've been at this all morning. I'm currently working on a function that effectively creates a role, with one click on a reaction, but it has been throwing up the same error for a lifetime now.
Here's my code:
client.on('message', async message => {
let AdminRole = message.member.roles.cache.find(role => role.name === "Server Moderators")
let RulerRole = message.member.roles.cache.find(role => role.name === "The Supreme Boomers")
let RatRole = message.member.roles.cache.find(role => role.name === "Rat Majesty Robin")
if (message.member.roles.cache.has(AdminRole)) {
} else if (message.member.roles.cache.has(RulerRole)) {
} else if (message.member.roles.cache.has(RatRole)) {
}
if (message.content === `${prefix}createBumpRole`) {
message.channel.send("Do you want to create a role for bumping? Click on either the check mark or cross depending on your choice.")
message.react("✅") | message.react("❌")
}
const filter = (reaction, user) => {
return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '✅') {
message.guild.cache.roles.create({
data: {
name: 'bumping ping',
color: 'BLUE',
},
reason: 'We needed a role for people who regularly bump the server.',
})
} else if (reaction.emoji.name === '❌') {
message.reply('Thank you for the response. You have exited this program.');
}
})
});
I was trying to create a new role and add it to a member when running a command once, but it seems that I have to run the command twice (once: for the role to get created, twice: for the role to get added to the member).
I guess this would be due to this error: TypeError [INVALID_TYPE]: Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes.
if (command === 'test') {
if (!message.mentions.users.size) {
return message.reply('You need to tag a user!');
}
const member = message.mentions.members.first();
const testRole = message.guild.roles.cache.find(role => role.name === 'TestRole');
if (!testRole) {
message.guild.roles.create ({
data: {
name: 'TestRole',
color: 'RANDOM',
},
}).catch(() => {
message.reply('Unable to create role');
});
}
member.roles.add(testRole).catch((error) => {console.log(error);});
}
Would there be a workaround for this in order to add the role to the member as soon as it is created?
Since roles.create returns a promise, you can use .then() to add the role to the member
if (command === 'test') {
if (!message.mentions.users.size) {
return message.reply('You need to tag a user!');
}
const member = message.mentions.members.first();
const testRole = message.guild.roles.cache.find(role => role.name === 'TestRole');
if (!testRole) {
message.guild.roles.create ({
data: {
name: 'TestRole',
color: 'RANDOM',
},
})
.then((role) => {
member.roles.add(testRole)
})
.catch(() => {
message.reply('Unable to create role');
});
} else {
member.roles.add(testRole).catch((error) => {console.log(error);});
}
}