I am trying to make a mute command using roles but I keep getting the error saying remove role is not a function. Im confused
let mainrole = message.guild.roles.cache.find(role => role.name === "Verified");
let role = message.guild.roles.cache.find(role => role.name === "Muted");
if (!role) return message.reply("Sorry! Couldn't find the mute role.")
let time = args[2];
if (!time) {
return message.reply("You didnt specify a time!");
}
person.removeRole(mainrole.id)
person.addRole(role.id);
message.channel.send(`#${person.user.tag} has now been muted for ${ms(ms(time))}`)
setTimeout(function() {
person.addRole(mainrole.id)
person.removeRole(role.id);
Error: TypeError: person.removeRole is not a function
Since discord.js v12 you need to use roles.add() and roles.remove() instead of addRole() and removeRole()
person.roles.remove(mainrole.id)
person.roles.add(role.id);
message.channel.send(`#${person.user.tag} has now been muted for ${ms(ms(time))}`)
setTimeout(function() {
person.roles.add(mainrole.id)
person.roles.remove(role.id);
Related
I'm trying to add a mute command
This is my current code:
if (message.member.roles.cache.some(role => role.name === 'Admin')) {
let target = message.mentions.members.first;
let role = message.member.guild.roles.cache.find(role => role.name === "muted");
target.roles.add(role);
message.channel.send('${user} has been muted by ${message.author.username}');
}
I'm getting this error:
(node:11052) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'add' of undefined
I recommend you use Roles#has for searching for a member role
if (message.member.roles.cache.has('role-id') { ... }
Collection#first is a method, not a property, add () to first
let target = message.mentions.members.first();
You are getting Cannot read property 'add' of undefined because you did not properly acces the first mention
Here is all the code fixed and put together:
if (message.member.roles.cache.has('ROLE-ID-HERE')) {
let target = message.mentions.members.first();
let role = message.member.guild.roles.cache.find(role => role.name === "muted");
target.roles.add(role);
message.channel.send('${user} has been muted by ${message.author.username}');
}
So i am trying to make a command that mute a member. I want my bot to check if there is role name Muted and if there is not, to create a role for Muted members.
This is the code:
module.exports = {
name: 'mute',
description: 'mutes a member',
execute(message, args, Discord, bot) {
let mutedRole = message.guild.roles.cache.find(r => r.name === 'Muted')
if(!mutedRole) {
Guild.roles.create({
data: {
name: 'Muted',
color: 'BLACK'
}
})
}
}
}
and the error is this:
C:\Users\ADRIAN\Desktop\ZerOne BOT\commands\mute.js:10
Guild.roles.create({
^
TypeError: Cannot read property 'create' of undefined
I don't think you should create a role via code. I think you should create a role in your Discord server (like actually just making one without code). Download the ms package. (npm i ms) Then, paste this code in:
const ms = require('ms');
module.exports = {
name: "mute",
description: "Mutes a member!",
execute(message, args, Discord, bot){
const target = message.mentions.users.first();
if (target) {
let mainRole = message.guild.roles.cache.find(role => role.name === 'Member');
let muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');
let memberTarget = message.guild.members.cache.get(target.id);
if (!args[1]){
memberTarget.roles.remove(mainRole.id);
memberTarget.roles.add(muteRole.id);
message.channel.send(`<#${memberTarget.user.id}> has been muted.`);
return;
}
if (isNaN(args[1]) || !args[1] === ' '){
message.channel.send("Please enter a real number! (If you have a space after the username and you don\'t want to set a time limit, delete the space. YOU HAVE TO.)");
return;
}
memberTarget.roles.remove(mainRole.id);
memberTarget.roles.add(muteRole.id);
message.channel.send(`<#${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);
setTimeout(function(){
memberTarget.roles.remove(muteRole.id);
memberTarget.roles.add(mainRole.id);
}, ms(args[1]));
} else {
message.channel.send("Can't find that member!");
}
}
}
Thanks to CodeLyon for this code. Hope this works for you! It even includes a time limit.
format 1: !mute #User
format 2: !mute #User 1000 (1000 can be changed, just make sure it's in milliseconds)
module.exports = {
name: "unmute",
description: "Unmutes a member!",
execute(message, args, Discord, bot){
const target = message.mentions.users.first();
if (target) {
let mainRole = message.guild.roles.cache.find(role => role.name === 'Member');
let muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');
let memberTarget = message.guild.members.cache.get(target.id);
memberTarget.roles.remove(muteRole.id);
memberTarget.roles.add(mainRole.id);
message.channel.send(`<#${memberTarget.user.id}> has been unmuted.`);
} else {
message.channel.send("Can't find that member!");
}
}
}
format: !unmute #User
Remember, this code only works if you have a role Member and a role Muted. So, in your server, add the role Member to everybody and add a role Muted (not to everybody).
Hope this works! If there's any need for clarification, please tell me in the comments.
I'm trying to find if any member having a particular role id is present in a particular voice channel. If even 1 member having the particular role id is present in that particular voice channel, then only those members can use the commands of music bot.
From my research, I have came to know that voiceStateUpdate may help but the problem is I don't know how to use it in my codes(cause I am new to JavaScript). Here is the link to the documentation:
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate .
Here is a part of my code:
client.on('voiceStateUpdate', (oldMember, newMember) => {
});
client.on('message', async message => {
if (message.author.bot) return
if (!message.content.startsWith(prefix)) return
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
const args = message.content.substring(prefix.length).split(' ');
const searchString = args.slice(1).join(' ')
const url = args[1] ? args[1].replace(/<(._)>/g, '$1') : ''
const serverQueue = queue.get(message.guild.id)
if() { //the conditional statement I am trying to put here but I don't know how to do it properly
if (message.content.startsWith(`${prefix}play`)) {
const voiceChannel = message.member.voice.channel
if (!voiceChannel) return message.channel.send("You need to be in a voice channel to play music")
.......
So the main thing is that I don't know what to write their exactly in the if statement that would make my code work properly.
client.on("message", async message => {
// Checking if the message author is a bot.
if (message.author.bot) return false;
// Checking if the message was sent in a DM channel.
if (!message.guild) return false;
// The role that can use the command.
const Role = message.guild.roles.cache.get("RoleID");
// The voice channel in which the command can be used.
const VoiceChannel = message.guild.channels.cache.get("VoiceChannelID");
if (message.content.toLowerCase() == "test") {
// Checking if the GuildMember is in a VoiceChannel.
if (!message.member.voice.channel) return message.reply("You need to be in a voice channel.");
// Checking if the GuildMember is in the required VoiceChannel.
if (message.member.voice.channelID !== VoiceChannel.id) return message.reply("You are in the wrong VoiceChannel.");
// Checking if the GuildMember has the required Role.
if (!message.member.roles.cache.has(Role.id)) return message.reply("You are not allowed to execute this command.");
// Execute your command.
return message.reply("Command executed");
};
});
So from what I can understand, you want the command to only execute when a user with the role 'Example Role' is in the channel 'Example Channel'
For this you will need the role id and the channel id.
client.on("message", async message => {
if (message.author.bot) return;
if (!(message.guild)) return;
var roleID = 'RoleID';
var vc = message.guild.channels.cache.get("VoiceChannelID");
if (message.content.toLowerCase() == "test") {
canUse = false;
vc.members.forEach((member) => {
if (member.roles.has(roleID)) {
canUse = True;
}
})
if (!(canUse)) { // nobody in the voice channel has the role specified.
return;
}
console.log("A member of the voice channel has the role specified")
}
});
so I am trying to make a command like $roletested #user which should give the user mentioned the specific role. I get an error called:_ "Cannot read property 'roles' of undefined... Help me out please, here's my code :D
if (message.content.startsWith(prefix + "roletested")) {
let testedUser = message.mentions.members.first()
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌');
declineMsg.delete({timeout: 5000});
let testedRole = message.guild.roles.cache.find(role => role.id === "724676382776492113");
testedUser.roles.add(testedRole);
message.channel.send("Added the role `TESTED` to user.")
})}})
Your code but fixed
if (message.content.startsWith(prefix + "roletested")) {
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
let testedRole = message.guild.roles.cache.get('724676382776492113');
let testedUser = message.mentions.members.first();
if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌')
declineMsg.delete({timeout: 5000});
});
testedUser.roles.add(testedRole);
message.channel.send("Added the role `TESTED` to user.")
}
I fixed your code :
I put in order something (the code is the same, I only changed something)
You can't use message.guild.roles.cache.find(...) so I changed it to message.guild.role.cache.get('Role ID')
Source : Link
Code :
if (message.content.startsWith(prefix + 'roletested')) {
const testedUser = message.mentions.members.first();
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
if (!testedUser) {
message.channel.send('You have to mention the person you want to assign the role to!').then(declineMsg => {
message.react('❌');
declineMsg.delete({ timeout: 5000 });
return;
});
}
const testedRole = message.guild.roles.cache.get('395671618912780289');
testedUser.roles.add(testedRole);
message.channel.send('Added the role `TESTED` to user.');
}
I tested this code on my bot and it worked as expected.
My problem is that I'm getting an error whenever I use the following code:
else if (cmd === "sayw") {
const embed2 = new Discord.RichEmbed()
.setDescription("❌ You can't make me say nothing! \nWait, you just did-")
.setFooter(`Requsted by ${message.author.tag}.`, message.author.avatarURL)
if(!args[0]) return message.channel.send(embed2)
const guild = message.guild
const embed = new Discord.RichEmbed()
.setAuthor("Say Webhook", bot.user.avatarURL)
.setDescription("❌ I need the `MANAGE_WEBHOOKS` permision first!" )
.setImage(`https://i.gyazo.com/d1d5dc57aa1dd20d38a22b2f0d4bd2f6.png`)
const member = guild.members.get(bot.id)
if (member.hasPermission("MANAGE_WEBHOOKS")) {
message.channel.createWebhook(message.author.username, message.author.avatarURL)
.then(webhook => webhook.edit(message.author.username, message.author.avatarURL))
.then(wb => {wb.send(args.join(" "))
setTimeout(() => {
bot.fetchWebhook(wb.id, wb.token)
.then(webhook => {
console.log("deleted!")
webhook.delete("Deleted!")
}, 5000)})})
} else {
message.channel.send(embed)
}}
I want the bot to tell the user if it can't create the webhook, but instead, I get this error when trying to use this command:
(node:10044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of undefined
How do I make this code work?
If you're using Discord.js v12 (the latest verison), guild.members.get(bot.id) won't work as guild.memebrs is now a GuildMemberManager.
Use
const member = guild.members.cache.get(bot.id)
or you could even use this as a shortcut:
const member = guild.me