Bot Crashed When you dont mention - discord

I have this command on my bot where you can mute someone using the command g!mute where the 'user' is mentioned using #. However, if you don't mention e.g GeoGeo instead of #GeoGeo, it causes the bot to crash. I know you need to put .catch(console.error); somewhere, but I'm not sure where. Thanks in advance. The Error is
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
^
TypeError: message.guild.members.get is not a function
Code:
const Discord = require('discord.js');
const ms = require('ms');
module.exports = {
name: 'mute',
description: "this is mute command",
execute(message, args){
if(!message.member.roles.cache.find(r => r.name ==="Staff", "Head Staff", "Owner", "Co-Owner")) return message.channel.send(`YOU DO NOT HAVE PERMISSION TO DO THAT`)
let members = args[0];
if(!members) return message.reply("g!mute <user> <time>")
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("That person is not in the server!");
let mainrole = message.guild.roles.cache.find(role => role.name === "Fans");
let muterole = message.guild.roles.cache.find(role => role.name === "muted");
if(!muterole) return message.reply("That role does not exist");
let time = args[1];
if(!time){
return message.reply("g!mute <user> <time>");
}
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has now been muted for ${ms(ms(time))}`)
.setColor(0x01B8FF)
message.channel.send(embed);
setTimeout(function(){
person.roles.add(mainrole.id)
person.roles.remove(muterole.id)
const embed = new Discord.MessageEmbed()
.setTitle ("Muted:")
.setDescription (`${person.user.tag} has been unmuted`)
.setColor(0x01B8FF)
message.channel.send(embed);
}, ms(time));
}
}

When faced with an error like: TypeError: message.guild.members.get is not a function
The logical thing to do is check the docs to see that message.guild.members really has a function named get. Here's the docs: https://discord.js.org/#docs/main/stable/class/GuildMemberManager
No get. But there is a cache like you use elsewhere in the code. Just by checking over the docs you can tell that your existing code is wrong (it's outdated) and you need to use cache like you do elsewhere in your code:
message.guild.members.cache.get(args[1])

Related

Unban is not defined

const Discord = require('discord.js');
module.exports = {
name: 'unban',
aliases: ['uban', 'unban'],
category: 'misc',
permissions: ['BAN_MEMBERS'],
description:
'Use this command to permanately or temporary ban a server member from Sekai.',
/**
* #param {Discord.Message} message
* #param {Array} args
*/
async execute(message, args) {
if (message.mentions.users.size === 0)
return message.reply('Please mention a user to unban ❌');
const targetid = message.mentions.users.first().id;
if (targetid === message.client.user.id)
return message.reply(
"Me? Really? That's not very nice, I guess you failed 🤡"
);
const targed = await message.guild.members.cache.get(targetid);
let reason = [];
if (args.length >= 2) {
args.shift();
reason = args.join(' ');
} else reason = 'No Reason provided';
try {
let extra = '';
try {
const embed = new Discord.MessageEmbed()
.setTitle('Moderation message regarding on your **BAN**')
.setAuthor("Joony's Den")
.setDescription(
`you have been banned from **${message.guild.name} ✅ **\nReason for ban: **${reason}\n${extra}**`
)
.addField('Contact','If you believe that your ban was unjustified, please feel free to contact any of these staff members. **JOONY#9513** or any of administrators online.')
.setColor('#2243e6')
.addField('Appeal Accepted?','if your appeal was accepted, please join using this link. your link will expire after 1 use. **https://discord.gg/4yuCzUC7aw**')
.addField(
'Appeal',
'Because you have been banned from the server, you will have one chance to appeal 🔨. Your appeal will be processed to the administrators or appeal managers ✅ **[CLICK HERE TO APPEAL](https://forms.gle/atc75ZftpdfJhfH56)**'
);
targed.send(embed);
} catch (error) {
extra = 'Messaging the user has failed! ❌';
}
setTimeout(() => {
targed.unban(targed, [reason])
const embed = new Discord.MessageEmbed()
.setTitle('User unbanned')
.setDescription(
`${
targed.tag || targed.user.username
} has been sucessfully unbanned from **${
message.guild.name
} ✅ **\nReason for unban: **${reason}\n${extra}**`
)
.setColor('#FA2657');
message.channel.send(embed);
}, 2000);
} catch (error) {
message.channel.send(
`I could not unban the given member, make sure that my role is above member! ❌`
);
}
},
};
Hello! how do I unban the user using this format, it has an error saying "guild.unban is undefined"
it has an error saying
targed.unban([reason])
^
TypeError: targed.unban is not a function
at Timeout._onTimeout (C:\Users\Joon\Documents\GitHub\Discord-Bot\commands\misc\unban.js:49:16)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
You cannot unban a GuildMember (a banned user is not a member of a Guild). You should call unban on GuildMemberManager. See https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=unban

How to know if a user already has a role in an addrole command

I want to send an error message saying that the user already has the role if they do in an addrole command.My current command is:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "addrole",
description: "adds a role to a member",
execute: async (message , args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Invalid Permissions")
let member = message.mentions.members.first();
let role = message.mentions.roles.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!role) return message.reply("please mention a valid role of this server");
const highest = message.member.roles.highest;
if(highest.comparePositionTo(role) <= 0)
return message.channel.send(`You cannot add roles equal/higher to that member's highest role`)
let addroleembed = new MessageEmbed()
.setTitle(`:white_check_mark: ${role.name} has been added to ${member.user.tag}`)
.setFooter(`Action performed by ${message.author.tag}`)
.setColor('77B255')
addroleembed.setTimestamp();
member.roles.add(role);
message.channel.send(addroleembed)
}}
I have tried using
if(!message.member.roles.cache.some(r=>[`${role.name}`].includes(r.name)))
and
if (member.roles.has(role.id) {
But neither of these seem to work, Anyone know the reason behind this? If I can't use either of these methods, Can you suggest me a solution?
You are so close!
All you need is cache when using has()
member.roles.cache.has(<RoleID>);
Learn More Here
You need to combine your two tries.
if(message.member.roles.cache.has("role ID") {
//rest of your code
}

Discord.js problem searching in the roles of a user

I am trying to create a command, in this case it is activated with / attack, the mechanism I am looking for is that if the target user (mentioned) has the role (Lavander) which is a kind of shield, send an embed message saying defended and remove the role from you (break the shield) and if the target user (mentioned) does not have the shield role, just send a different message saying attacked. This is the code that I have been doing but it does not work for me even if it does not give errors, simply when using it, it ignores the role detection and sends both messages for some reason that I do not know, can someone help me?
if (message.content.startsWith('/attack')) {
let Lavander = message.guild.roles.cache.find(role => role.name == "Lavander");
let member = message.mentions.members.first();
if (message.member.roles.cache.has(Lavander)) return
member.roles.remove(Lavander);
message.channel.send(new Discord.MessageEmbed()
.setColor("GOLD")
.setTitle(message.author.username)
.setDescription("Defended"))
message.channel.send(new Discord.MessageEmbed()
.setColor("GOLD")
.setTitle(message.author.username)
.setDescription("Attacked"))
}
For me it seems like let Lavander = message.guild.roles.cache.find(role => role.name == "Lavander"); might be supposed to be let Lavander = message.guild.roles.cache.find(role => role.name === 'Lavander'); but without the info about the glitches and/or errors, I can't tell you anything else.
method collection.has require id as property. So you need somethink like this:
bot.on('message', (message) => {
if (message.content.startsWith('/attack')) {
let lavander = message.guild.roles.cache.find((role) => role.name === 'Lavander');
let member = message.mentions.members.first();
if (!member || !lavander) return message.reply('No role or member');
if (message.member.roles.cache.has(lavander.id)) {
member.roles.remove(lavander);
let embed = new Discord.MessageEmbed()
.setColor('GOLD')
.setTitle(message.author.username)
.setDescription('Defended');
} else {
let embed = new Discord.MessageEmbed()
.setColor('GOLD')
.setTitle(message.author.username)
.setDescription('Attacked');
message.channel.send(embed);
}
}
});

I need a AutoRole command when somebody joins my server

I need a AutoRole command discord.js when somebody join my discord server he gets the Discord Member role.
Ive tryied some code but it doesnt work.
const discord = require("discord.js");
const config = require('../config.json');
module.exports.run = async (bot, message, args) => {
let target = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
let reason = args.slice(1).join(' ');
let logs = message.guild.channels.find('name', config.logsChannel);
if (!message.member.hasPermission('BAN_MEMBERS')) return message.reply('you do not have permissions to use this command!s');
if (!target) return message.reply('please specify a member to ban!');
if (!reason) return message.reply('please specify a reason for this ban!');
if (!logs) return message.reply(`please create a channel called ${config.logsChannel} to log the bans!`);
let embed = new discord.RichEmbed()
.setColor('RANDOM')
.setThumbnail(target.user.avatarURL)
.addField('Banned Member', `${target.user.username} with an ID: ${target.user.id}`)
.addField('Banned By', `${message.author.username} with an ID: ${message.author.id}`)
.addField('Banned Time', message.createdAt)
.addField('Banned At', message.channel)
.addField('Banned Reason', reason)
.setFooter('Banned user information', target.user.displayAvatarURL);
message.channel.send(`${target.user.username} was banned by ${message.author} for ${reason}`);
target.ban(reason);
logs.send(embed);
};
module.exports.help = {
name: 'ban'
};
When they join they get the Discord Member role.
You can use the guildMemberAdd event to do actions on new users.
client.on("guildMemberAdd", (member) => {
member.addRole('ROLE ID HERE')
});

How can I prohibit the use of the command on certain roles

I want to write code that will check if the roles are in the list. If yes, then they can be issued, and if not, then no. I'm new in discord js.
if(!rMember.roles.has((['00000000000000', '00000000000000, '00000000000000', '00000000000000', '00000000000000']))) return message.reply("nope.");
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission("MANAGE_MEMBERS")) return message.reply();
let rMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
if(!rMember) return message.reply("I can't find player.");
let role = args.join(" ").slice(22);
if(!role) return message.reply("specify rang!");
let gRole = message.guild.roles.find(`name`, role);
if(!gRole) return message.reply("I can't find this rang!.");
if(rMember.roles.has(gRole.id)) return message.reply("I can't do it.");
if(!rMember.roles.has((['00000000000000', '00000000000000, '00000000000000', '00000000000000', '00000000000000']))) return message.reply("nope.");
await(rMember.removeRoles(['00000000000000', '00000000000000, '00000000000000', '00000000000000', '00000000000000']));
await(rMember.addRole(gRole.id));
try{
await rMember.send(`Your rang was changed to ${gRole.name}!`)
}catch(e){
}
}
module.exports.help = {
name: "role"
}
It works without check code, but if I add it, all times, when I will try to change role of user, it gives out "nope".
You could check whether the given role is in the list of allowed roles using Array.includes().
const allowed = ['id', 'anotherID', 'andAnother'];
if (!allowed.includes(gRole.id)) return;
// Returns if the role is *not* in the 'allowed' array.

Resources