Ban command is not being recognized - javascript

The command requires a reason to work, however, it still does not ban even if I mention someone and give a reason. It's like the command isn't recognized!
bot.on('message', async message => {
if (message.content == prefix + "ban") {
if (!message.member.roles.some(r => ["Administrator", "Co-owner"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");
let member = message.mentions.members.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
var reason = args.slice(1).join(' ');
if (!reason) reason = "No reason provided";
await member.ban(reason);
}
});

Finally got it to work! This was my code at the end:
bot.on('message', message => {
let member = message.mentions.members.first();
if (message.content.startsWith(prefix + "ban")) {
if (!message.member.hasPermission('BAN_MEMBERS'))
return message.reply("Sorry, you don't have permissions to use this!");
if (!member)
return message.reply("Please mention a valid member of this server");
if (!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
// V This line has been changed V
var reason = message.content.split(' ').slice(2).join(' ');
if (!reason) return message.reply("Please specify a reason!");
member.ban(reason);
}
});
It was all because of the reason! Thanks to everyone who helped me, this opened doors for a lot more commands for me.

For the kick command, you have to put an argument as a reason. Like this:
var reason = args.slice(1).join(' ');
member.kick(reason);
This is just like the ban command in the second image.
If you need more help or clarification, ask me.
If this doesn't work, make sure your bot has a high enough role in the role hierarchy.

Related

Ban outside server users in Discord.js 12.5.1

I am trying to make a ban command that can ban someone using his ID and it works great, the problem is that when I try to ban a user that isn't on the server (using his ID) or only use 1 digit, the command doesn't work and the bot crashes. I think the problem is caused by the if statement but I'm not sure.
Here's my code :
let args = messageArray.slice(1);
if(message.content.startsWith(prefix + 'ban')) {
let toBan = message.guild.members.cache.get(args[0]) || message.mentions.members.first() || message.guild.members.cache.find(x => x.user.username.toLowerCase() === args.slice(0).join(" ") || x.user.username === args[0]);
if(!args) return message.channel.send('You need to mention a user!')
if (!message.member.hasPermission("VIEW_AUDIT_LOG")) return message.channel.send("You don't have enough permissions.")
if (!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send("I don't have enought permissions.")
if(!toBan.bannable) return message.channel.send("I can't ban that user!")
if(toBan === message.author) return message.channel.send("No.")
if(toBan.roles.highest.position >= message.member.roles.highest.position) return message.channel.send("You can't ban anyone that have a high or same role that you have.")
const reason = args[1] || "Undefined";
member.ban(`${toBan}`, {
reason: reason
})
message.channel.send(`${toBan} Has been banned of the server by ${message.author}!`)
}
You can use GuildMemberManager.ban(), which takes a UserResolvable. It can be a User, a GuildMember, or even just a Snowflake (ID).
Simple Example:
message.guild.members.ban("12345678901234567");

display message when trying to kick itsellf (the bot)

i'm making responses in commands. I made a response when trying to kick myself and works, but now i'm trying to make a response when trying to kick the bot and it doesnt work.
kick command code:
module.exports = {
name: 'kick',
aliases: ["k"],
description: "kick a member",
execute(message, Discord, client) {
if(message.member.permissions.has("KICK_MEMBERS")){
const member = message.mentions.members.first();
let mention = message.mentions.members.first();
if (message.mentions.members.size < 1) return message.reply('You must mention someone to kick them.').catch(console.error);
if (message.mentions.members.first().id === message.author.id) return message.reply("I can't let you do that, self-harm is bad:facepalm:");
if (message.mentions.members.id === client.member.id) return message.reply("You pleblord, how can you use a bot to kick itself?:joy:");
if (message.mentions.members.first().id === "521311050193436682") return message.reply("You can't kick my Developer:wink:");
if (reason.length < 1) reason = 'No reason supplied';
if (!message.guild.member(user).kickable) return message.reply('I cannot kick that member');
const embed = new Discord.MessageEmbed()
.setColor(0x0000FF)
.setTimestamp()
.addField('Action:', 'Kick')
.addField('User:', `${user.username}#${user.discriminator} (${user.id})`)
.addField('Moderator:', `${message.author.username}#${message.author.discriminator}`)
.addField('Reason', reason)
.setFooter(`© Cryptonix X Mod Bot by ${customisation.ownername}`);
if(user.bot) return;
message.mentions.users.first().send({embed}).catch(e =>{
if(e) return;
});
message.guild.member(user).kick();
let logchannel = message.guild.channels.cache.find(x => x.name = 'logs');
if (!logchannel){
message.channel.send({embed});
}else{
client.channels.cache.get(logchannel.id).send({embed});
message.channel.send({embed});
}
if(user.bot) return;
message.mentions.users.first().send({embed}).catch(e =>{
if(e) return ;
});
}
else{
let newEmbed = new MessageEmbed()
.setColor('#e4e265')
.setTitle('')
.setURL('')
.setDescription("Eeeh wait! You can't use that command <a:yepaa:797528125894295567>")
.setImage('')
.setFooter('');
message.channel.send(newEmbed).then(r => r.delete({ timeout: 10000 }));
}
}
};
and this is the error i get when trying to run the command in discord chat
TypeError: Cannot read property 'member' of undefined
at Object.execute (C:\Users\ayman\Desktop\sharky music\commands\kick.js:15:56)
(kick.js:15:16 is the line of the bot response when trying to kick the bot)
if (message.mentions.members.id === client.member.id)
This if condition is wrong because a property member doesn't exist on the client.
You have 2 ways to fix your problem:
Replace client.member.id with client.user.id (The ids are the same)
// The other code from above this if statement
if (message.mentions.members.id === client.user.id) return message.reply("You pleblord, how can you use a bot to kick itself?:joy:");
// The following code
Replace client.member.id with message.guild.me.id me is the Guild#GuildMember object from the client
// The other code from above this if statement
if (message.mentions.members.id === message.guild.me.id) return message.reply("You pleblord, how can you use a bot to kick itself?:joy:");
// The following code
if you look closely you have defined member at
const member = message.mentions.members.first(); and let mention = message.mentions.members.first();
!message.guild.member(user).kickable after you have changed the user value from their code you missed to change the (user).kickable which returned member undefined as the bot cannot find the "user" not the "mention"
member and mention are all defined value in djs. so i would say change it to something like mentioned.
you can try to :
delete const member = message.mentions.members.first();
change to:
const mentioned = message.mentions.members.first();
!message.guild.member(mentioned).kickable
if(mentioned.bot) return;
message.mentions.mentioned.first().send({embed}).catch(e =>{if(e) return;});
and finally kick it by message.guild.member(mentioned).kick();
didnt know it worked or not but hopefully afterall member is going to be defined!
think James might be answered as he have already posted in comments but i just leave what i think here. If im wrong, correct me.

Banning users using user ID discord.js

I'm kinda new to coding discord bots and I have a problem. I want to have a ban command that would ban the mentioned user in the command or the user that has an ID that was provided in the command. For example:
&ban #User#0001 would ban User#0001
but
if the command looks like this:
&ban 123456789123456789 (let's say that's the ID of User#0001)
it would still ban User#0001 (as it is the user's ID).
I have this code, it works if I mention the user, but it doesn't work if I enter the ID.
const Discord = require('discord.js');
module.exports = {
name: 'testban',
description: "Executer will ban the mentioned user",
execute(message, args){
if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
let User = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
if (!User) return message.channel.send("Invalid User")
if (User.hasPermission("BAN_MEMBERS")) return message.reply("Can't ban that one, he also can ban")
let banReason = args.join(" ").slice(22);
if (!banReason) {
banReason = "None"
}
console.log(`USER = ${User}`)
User.ban({reason: banReason})
var UserID = User.id
console.log(`USER ID = ${UserID}`)
}
}
The error I get when entering the ID is this:
message.guild.members.get is not a function
How could I make it ban the person even if I only provide the ID?
If you're using the newer versions of discord.js you need to use the cache, you just need to change it to this:
message.guild.members.cache.get(args[0])

I got a problem in removeRole in the discord.js timed mute command (removeRole is not a function)

the remove role error
my code:
bot.on('message', message => {
let prefix = "t?";
let args = message.content.substring(prefix.length).split(" ");
switch (args[0]) {
case 'mute':
let person = message.mentions.users.first(); message.guild.members.fetch(args[1])
if(!person) return message.reply("couldnt find member");
let mainrole = message.guild.fetch(role => role.name === "member");
let muterole = message.guild.fetch(role => role.name === "muted");
if(!muterole) return message.reply("couldn't find mute role");
let time = (args[2]);
if(!time) {
return message.reply("how long? <:Thonk:582005026470363137>");
}
person.removeRole(mainrole.id);
person.addRole(muterole.id);
channel.send(`#${person.user.tag} has now been muted for ${ms(ms(time))}`);
setTimeout(function(){
person.addRole(mainrole.id);
person.removeRole(muterole.id);
message.channel.send(`#${person.user.tag} has been unmuted.`);
}, ms(time));
break;
}
});
and it pops up and error that says remove Role is not a function
do anyone know how to solve these kinds of problem in discord
You are confusing GuildMember with User.
A user is a person on Discord, a GuildMemebr is a person on discord as pertaining to a specific guild. For example, you can DM a user or a guild member because direct messages don't have anything to do with guilds, but you can't remove a role from a user because a user isn't associated with a guild, there are no roles to remove.
So, you need to get message.mentions.members not message.mentions.users
The documentation for this is here in MessageMentions

Adding/Removing Roles is not working (sometimes)

On some Discord servers my code to add/remove roles, however, on some it doesn't. I checked and they all have the correct permissions, so I'm kind of stumped.
Whenever I run the >addrole or >removerole command I always get the same Discord error message. It is ":x: Couldn't find mute role. Make sure you didn't make a typo (roles are case-sensitive too!)". I set this for when a user makes a typo while typing what role they want to add.
The format for the commands are as follows:
addrole [#User] [RoleName]
removerole [#User] [RoleName]
const Discord = require("discord.js");
exports.run = async(bot, message, args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send(":x: Insufficient permission.").then(msg => msg.delete(4000));
let rolemember = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!rolemember) return message.channel.send(":x: Could not find user.").then(msg => msg.delete(4000));
let role = args.join(" ").slice(22);
if (!role) return mesage.reply(":x: Specify a role.");
let gRole = message.guild.roles.find(`name`, role);
if (!gRole) return message.channel.send(":x: Couldn't find mute role. Make sure you didn't make a typo (roles are case-sensitive too!)");
if (!rolemember.roles.has(gRole.id)) return message.channel.send(`:x: User does not have role "${gRole.name}".`).then(msg => msg.delete(4000));
await (rolemember.removeRole(gRole.id));
try {
rolemember.send(`:white_check_mark: Your role "${gRole.name}" in`, message.guild.name, "has been removed :confused:.");
} catch (e) {
message.channel.send(`:white_check_mark: ${rolemember} Your role "${gRole.name}" has been removed :confused:.`);
}
let removeroleEmbed = new Discord.RichEmbed()
.setDescription("Role Changes")
.setColor("RANDOM")
.addField("Role Removed", gRole)
.addField("Removed From", rolemember)
.addField("Removed By", message.author);
let logChannel = message.guild.channels.find(`name`, "logs-reports");
if (!logChannel) return message.channel.send(":x: Couldn't find logs channel.").then(msg => msg.delete(4000));
logChannel.send(removeroleEmbed);
}
exports.help = {
name: "removerole"
}
I expect that the role should be added, however, it is not and the same error message is what I get every time.
As .find('name', 'name') is DEPRECATED. That thing you use may not work.. Instead, use let gRole = message.guild.roles.find(r => r.name === role). I am new to this community. So please fogive if I do something wrong.
Try replaceng the let gRole = message.guild.roles.find("name", role); with let gRole = message.guild.roles.find(r => r.name === role) This should work because it is how it is supposed to be, sorry I am not very good at explainin
Note:
Collection.find() is not totally deprecated, just format Collection.find("name", "yourName") is deprecated!

Categories

Resources