I was able to ban users outside the servers easily but I'm facing trouble in banning members outside or not in the server, here is my code:
const Discord = require('discord.js');
module.exports = {
name: "ban",
description: "Kicks a member from the server",
async run (client, message, args) {
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send('You can\'t use that!')
if(!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send('I don\'t have the right permissions.')
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if(!args[0]) return message.channel.send('Please specify a user');
if(!member) return message.channel.send('Can\'t seem to find this user. Sorry \'bout that :/');
if(!member.bannable) return message.channel.send('This user can\'t be banned. It is either because they are a mod/admin, or their highest role is higher than mine');
if(member.id === message.author.id) return message.channel.send('Bruh, you can\'t ban yourself!');
let reason = args.slice(1).join(" ");
if(!reason) reason = 'Unspecified';
member.ban(`${reason}`).catch(err => {
message.channel.send('Something went wrong')
console.log(err)
})
const banembed = new Discord.MessageEmbed()
.setTitle('Member Banned')
.setThumbnail(member.user.displayAvatarURL())
.addField('User Banned', member)
.addField('Kicked by', message.author)
.addField('Reason', reason)
.setFooter('Time kicked', client.user.displayAvatarURL())
.setTimestamp()
message.channel.send(banembed);
}
}
I couldn't find out how to do son. Can you help me? Thanks in advance
The reason why your code will not work and run into errors is that the user is not a member, while you are trying to stick them into a GuildMember object, which is not possible.
The way to fix this is to use a user object instead.
guild.members.ban() is a method that accepts a user as a parameter.
Another thing to note is that getting msg.mentions.members.first() will create an error because you are not mentioning a member of your server. (again)
Therefore member needs to be changed to message.mentions.users.first() or client.users.cache.get(args[0])
And your ban code needs to be changed to:
message.guild.members.ban(member).then(user => {
message.channel.send(`Banned ${user.id}`);
}).catch(console.error);
Related
I can't understand why doesn't send the welcome message
Here's Code from index.js
client.on('guildMemberAdd', (member) => {
let chx = db.get(`welchannel_${member.guild.id}`);
if (chx === null) {
return;
}
client.channels.cache.get(chx).send(`Welcome to ${message.guild.name}`);
});
Here's Code From channel.js
module.exports = {
name: "channel",
description: "Help Command",
category: "Help",
execute(client, message, args, Discord) {
const db = require("quick.db")
let channel = message.mentions.channels.first() //mentioned channel
if(!channel) { //if channel is not mentioned
return message.channel.send("Please Mention the channel first")
}
db.set(`welchannel_${message.guild.id}`, channel.id)
const embed = new Discord.MessageEmbed()
.setColor('#b5b5b5')
.setTitle(`Channel set: ${channel.name} `)
message.channel.send({ embeds: [embed] });
}
}
EDIT: I found out the problem i didn't have a intent flag GUILD_MEMBERS
and also thanks UltraX that helped also
Basically, the reason is simple, you need to go to your dev portal then after choosing your bot/application just go to bot and you need to enable member intents Server Member Intent after that it should work, if it didn't just give it a 10 minute, then try again!
The best way to ensure you get a channel object within the event is to use the guild property off the emitted member.
client.on("guildMemberAdd", (member) => {
const { guild } = member;
let chx = db.get(`welchannel_${member.guild.id}`);
if(chx === null) {return}
const channel = guild.channels.cache.get(chx);
if (!channel) return;
channel.send(`Welcome to ${message.guild.name}`)
.catch(console.error);
})
You will need the Guild Member's intent enabled as stated in This Answer for the event to emit.
The only code I have is this:
module.exports = {
name: "kick",
description: "This command kicks a member!",
execute(message, args) {
const target = message.mentions.users.first();
if (target) {
const memberTarget = message.guild.members.cache.get(target.id);
memberTarget.kick();
message.channel.send("User has been kicked");
} else {
message.channel.send(`You coudn't kick that member!`);
}
},
};
Good Morning. So I'm trying to get it to message the person that got kicked the reason why they got kicked. (!kick user reason) I want it so the bot DMs the person what the reason was but I don't know how to do that.
You would only need to add the following:
const reason = args.splice(1).join(` `) || 'Not specified';
memberTarget.send(`You have been kicked: \nReason: ${reason}`)
module.exports = {
name: "kick",
description: "This command kicks a member!",
execute(message, args) {
const target = message.mentions.users.first();
if (target) {
const memberTarget = message.guild.members.cache.get(target.id);
const reason = args.splice(1).join(` `) || 'Not specified';
memberTarget.kick();
message.channel.send("User has been kicked");
memberTarget.send(`You have been kicked: \nReason: ${reason}`)
} else {
message.channel.send(`You coudn't kick that member!`);
}
},
};
The const reason = args.splice(1).join( ) || 'Not specified'; defines the 'reason' property, if there isn't a reason, it defaults to 'Not specified'.
memberTarget.send(You have been kicked: \nReason: ${reason})
Just sends the message to the targeted member.
Right off the bat, I see that you are getting the target using message.mentions.users and getting the memberTarget from the guild's cache. You should avoid this and use message.mentions.members.
You'll have to use the send method of GuildMember, but since it returns a Promise, you'll have to catch any errors. (e.g: the bot cannot DM the member)
// You should do some sanity checks in case you haven't. You don't want everyone to be able to kick members using the bot.
// Getting the first member in message.mentions.members.
const target = message.mentions.members.first();
// Making sure that target is defined.
if (!target) return message.channel.send('Please mention a member to kick.');
// Making sure a reason is provided. (args[0] is the mention)
if (!args[1]) return message.channel.send('Please provide a reason.');
// Making sure the bot can kick the target.
if (!target.kickable) return message.channel.send('Couldn\'t kick the target.');
// Trying to send a message to the target, notifying them that they got kicked, and catching any errors.
target.send(`You have been kicked from ${message.guild.name} for ${args.slice(1, 2000).join(' ')}`).catch(console.error).finally(() => {
// After the message was sent successfully or failed to be sent, we kick the target and catch any errors.
target.kick(`Kicked by ${message.author.tag} for ${args.slice(1, 2000).join(' ')}}.`).then(() => {
message.channel.send(`${target.user.tag} has been kicked for ${args.slice(1, 2000).join(' ')}.`);
}).catch(error => {
console.error(error)
message.channel.send(`Something went wrong...`);
});
});
This is working kick command with reason made by me (you can try it):-
It has every validation you should have in a kick command
const Discord = require('discord.js')
exports.kick = async(message , prefix , client) => {
if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send('Missing Permission! You need to have `KICK_MEMBERS` permissions in order kick this member.')
if(!message.guild.me.hasPermission("KICK_MEMBERS")) return message.channel.send('Missing Permission! I need to have `KICK_MEMBERS` permissions to kick this member.')
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
let member = message.mentions.members.first();
if(!member){
let err = "```css\n[ Agrument Error : You Have not mentioned the user on first args. ]\n```\n\n"
let embed = new Discord.MessageEmbed()
.setAuthor(`${client.user.username} Help Manual` , client.user.displayAvatarURL({format : "png"}))
.setTitle(`${message.guild.name}`)
.setDescription(err)
.addField('Help Command:' , `\`\`\`\n${prefix}kick #user#0001 Reason\n\`\`\``)
.setTimestamp()
.setColor('RED')
return message.channel.send(embed)
}
if(args[0] != `<#!${member.id}>`){
let err = "```css\n[ Agrument Error : You Have not mentioned the user on first args. ]\n```"
let embed = new Discord.MessageEmbed()
.setAuthor(`${client.user.username} Help Manual` , client.user.displayAvatarURL({format : "png"}))
.setTitle(`${message.guild.name}`)
.setDescription(err)
.addField('Help Command:' , `\`\`\`\n${prefix}kick #user#0001 Reason\n\`\`\``)
.setTimestamp()
.setColor('RED')
return message.channel.send(embed)
}
if(member.id === message.author.id) return message.channel.send(`Why? No Just Say Why Do you want to kick yourself?`)
let reason = args.slice(1).join(' ');
if(!reason || reason.length <= 1){
reason = "No Reason Was Provided."
}
if(!member.kickable){
return message.channel.send(`I Don't Have Permissions to Kick ${member.user.username}`)
}
member.kick().then(() => {
return message.channel.send(`Successfully Kicked ${member.user.username} for Reason ==> \`${reason}\``)
}).catch(() => {
return message.channel.send(`I Don't Have Permissions to Kick ${member.user.username}`)
})
}
So the code below is my Kick.js but I want it so that the mod role can only use the kick command
module.exports = {
name: 'kick', //command name run : async(client,
message, args) => {
if (!message.guild.me.hasPermission('KICK_MEMBER')) return message.channel.send('I do not have permission to use this command
:joy: ');
// Basically checks if the bot has permission to kick a member or not
//Lets define the guildmember to kick!
const Member = message.mentions.members.first() //checking for member mentions
if (!Member) return message.channel.send('Please specify a member to kick.....');
// if there is no user then it will return with that message
await Member.kick({ reason: args.slice(1).join(" ")})
// args.slice(1).join(" ") means it slices the Member Variable and takes the text after it.
message.channel.send(`${ Member.user.tag}
was kicked from the server!`)
// above message will be sent when the member is kicked from the server.
}
}
Check for the message.member permissions
if(!message.member.hasPermission("KICK_MEMBERS"))
return message.reply("You don't have enough permissions for kick")
If you want to check for specific role
if(!message.member.roles.cache.has(modRole))
return message.reply("You aren't a Mod")
I am making my first discord bot, and I asked my friend to test kick/ban commands, I jokingly, tried banning him, now he had admin privileges so he couldn't get banned but the bot still sent the message confirming the ban, both of us are confused and I want to fix this so I don't get this message in the future.
module.exports = {
name: "kick",
description: "kick command",
execute(message, args) {
if (!message.member.hasPermission("KICK_MEMBERS")) {
message.channel.send("You do not have the permission to execute this command");
return;
}
const member = message.mentions.users.first();
if (member) {
const memberTarger = message.guild.members.cache.get(member.id);
memberTarger.kick();
message.channel.send("User has been kicked!");
} else {
message.channel.send("You could not kick that member");
}
},
};
Replace
const memberTarger = message.guild.members.cache.get(member.id);
memberTarger.kick();
message.channel.send("User has been kicked!");
With
const memberTarger = message.guild.members.cache.get(member.id);
if(!memberTarger.hasPermission('KICK_MEMBERS')){
memberTarger.kick();
message.channel.send("User has been kicked!");
}
You can check if the bot can kick/ban a member with the .kickable or .bannable properties of a GuildMember.
See the example below and give it a try:
const memberToKick; // Should hold the value of the GuildMember you want to kick.
if (memberToKick.kickable) {
memberToKick.kick()
.then(() => {
message.channel.send('Member successfully kicked!');
}).catch((e) => {
message.channel.send(`I couldn't kick the member for some reason. Here is the error: ${e}`);
});
} else {
message.channel.send('Sorry, I don\'t have permission to kick this member...');
}
your code is not perfect but it should work
if you go to the discord developer portal, go to OAuth2 and then click bot and scroll down and select administrator or kick members
then kick the bot and invite it again
This is to make sure your bot has permission to kick
and finally go to your server settings and make sure that your bot's role is above the role you want to kick (i always fall for that)
1 day ago i publish an issue with for a discord bot that said that my id was not a property of null. Now its works. But it still not able to ban, and it gives me the error marked on the code: message.reply("I was unable to ban the member :(");
This is the code:
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'ban',
description: "ban peoples ;D",
execute(message, args, client) {
if (!message.member.hasPermission("BAN_MEMBERS") ||
!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("You don't have a permissions to do this, maybe later ;) ");
const user = message.mentions.users.first();
const member = message.guild.member(user);
if (!user) return message.channel.send("Please mention the user to make this action");
if (user.id === message.author.id) return message.channel.send("You can't ban yourself, I tried :(");
member.ban(() => {
message.channel.send('Successfully banned **${user.tag}**');
}).catch(err => {
message.reply("I was unable to ban the member :(");
})
}
}
i checked to see if the bot needs permissions, i gave it him but it still not working.
The issue is here
member.ban(() => {
message.channel.send('Successfully banned **${user.tag}**');
}).catch(err => {
message.reply("I was unable to ban the member :(");
})
You are passing an entire function into the ban method
Really you should be calling the function then using .then(() => {}) to handle the promise
It should look like this
member.ban()
.then(() => {
message.channel.send('Successfully banned **${user.tag}**');
})
.catch((err) => {
message.reply("I was unable to ban the member :(");
console.error(err);
});