The CMD error:
C:\Users\<name>\Desktop\Discord Bots\AaryanBot\commands\kick.js:21
if(member.hasPermission('MANAGE_MESSAGES')) return message.reply('You cannot kick this person!');
^
TypeError: member.hasPermission is not a function
at Object.execute (C:\Users\<name>\Desktop\Discord Bots\AaryanBot\commands\kick.js:21:15)
And kick.js
var Discord = require('discord.js');
module.exports = {
name: 'kick',
description: 'Kick COmmand(ADMINS ONLY)',
execute(message, args) {
if(!message.member.hasPermission('KICK_MEMBERS')) return message.reply('You can\'t use that!');
var user = message.mentions.users.first();
if(!user) return message.reply('You didn\'t mention anyone!');
var member;
try {
member = message.guild.members.fetch(user);
} catch(err) {
member = null;
}
if(!member) return message.reply('They aren\'t in the server!');
if(member.hasPermission('MANAGE_MESSAGES')) return message.reply('You cannot kick this person!');
var reason = args.splice(1).join(' ');
if(!reason) return message.reply('You need to give a reason!');
var channel = message.guild.channels.cache.find(c => c.name === 'log');
var log = new Discord.MessageEmbed()
.setTitle('User Kicked')
.addField('User:', user, true)
.addField('By:', message.author, true)
.addField('Reason:', reason)
channel.send(log);
var embed = new Discord.MessageEmbed()
.setTitle('You were kicked!')
.setDescription(reason);
try {
user.send(embed);
} catch(err) {
console.warn(err);
}
member.kick(reason);
message.channel.send(`**${user}** has been kicked by **${message.author}**!`);
}
}
Related
So I'm trying to code a utility bot on my server and for some reason, this error popped out "Reference error"
ReferenceError: member is not defined
Every single time I solve a problem another one pops out. I know you guys will be asking me to save it (member) but I already did, like 7 times?
This is my current code:
const { Discord } = require("discord.js");
exports.run = async(client, msg, args) => {
if(!msg.member.hasPermission('BAN_MEMBERS')) return msg.reply('You do not have permission to use this command!')
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
if(member){
if(member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You cannot ban a fellow staff member!');
}
var reason = args.splice(1).join(' ');
if(!reason) return msg.reply('Please make sure to specify a reason for me to punish this user!')
var channel = msg.guild.channels.cache.find(c => c.name === 'mod-logs');
var verify = msg.guild.emojis.cache.find(emoji => emoji.name === 'white_check_mark')
var log = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been kicked by ${msg.author} for "**${reason}**"`)
channel.send(logs);
var userLog = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`You have been banned from the server! Thats sadge. You can appeal the ban by message a staff member!`)
try {
await user.send(userLog);
} catch(err) {
console.warn(err);
}
msg.guild.members.ban(user);
var confir = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been banned by ${msg.author} for "**${reason}**"`)
msg.channel.send(confir);
msg.delete();
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
const { Discord } = require("discord.js");
exports.run = async(client, msg, args) => {
if(!msg.member.hasPermission('BAN_MEMBERS')) return msg.reply('You do not have permission to use this command!')
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
if(member){
if(member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You cannot ban a fellow staff member!');
}
var reason = args.splice(1).join(' ');
if(!reason) return msg.reply('Please make sure to specify a reason for me to punish this user!')
var channel = msg.guild.channels.cache.find(c => c.name === 'mod-logs');
var verify = msg.guild.emojis.cache.find(emoji => emoji.name === 'white_check_mark')
var log = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been kicked by ${msg.author} for "**${reason}**"`)
channel.send(logs);
var userLog = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`You have been banned from the server! Thats sadge. You can appeal the ban by message a staff member!`)
try {
await user.send(userLog);
} catch(err) {
console.warn(err);
}
msg.guild.members.ban(user);
var confir = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been banned by ${msg.author} for "**${reason}**"`)
msg.channel.send(confir);
msg.delete();
}
You can try this code:
const CUser = message.mentions.users.first() || message.guild.members.cache.get(args[0]) || message.guild.member(message.author);
let member
if (message.mentions.members.first()) {
member = CUser
} else {
member = CUser.user
}
instead of your:
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
Please note this code was written on discord.js version 12.5.3
So im making a mute command which creates a mute role and gives it to the mentioned user, and currently i am getting an error: channel is not defined,
I do not know how to fix this error and i need help with this
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('MANAGE_ROLES')) return message.channel.send("I do not have Permission to mute members.");
let reason = args.slice(1).join(' ');
let roleName = 'Muted';
let muteRole = message.guild.roles.cache.find(x => x.name === roleName);
if (typeof muteRole === undefined) {
guild.roles.create({ data: { name: 'Muted', permissions: ['SEND_MESSAGES', 'ADD_REACTIONS'] } });
} else {
}
muteRole = message.guild.roles.cache.find(x => x.name === roleName);
channel.updateOverwrite(channel.guild.roles.muteRole, { SEND_MESSAGES: false });
const mentionedMember = message.mentions.member.first() || message.guild.members.cache.get(args[0]);
const muteEmbed = 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())
.setImage(message.mentionedMember.displayAvatarURL());
if (!args[0]) return message.channel.send('You must mention a user to mute.');
if (!mentionedMember) return message.channel.send('The user mentioned is not in the server.');
if (mentionedMember.user.id == message.author.id) return message.channel.send('You cannot mute yourself.');
if (mentionedMember.user.id == client.user.id) return message.channel.send('You cannot mute me smh.');
if (!reason) reason = 'No reason given.';
if (mentionedMember.roles.cache.has(muteRole.id)) return message.channel.send('This user has already been muted.');
if (message.member.roles.highest.position <= mentionedMember.roles.highest.position) return message.chanel.send('You cannot mute someone whos role is higher and/or the same as yours');
await mentionedMember.send(muteEmbed).catch(err => console.log(err));
await mentionedMember.roles.add(muteRole.id).catch(err => console.log(err).then(message.channel.send('There was an issue while muting the mentioned Member')));
}
}
I beileive that there could be even more errors than i think in this code as I am fairly new to coding.
You have used "channel" without declaring it. Hence, it is undefined.
check your line "channel.updateOverwrite(channel.guild.roles.muteRole, { SEND_MESSAGES: false });"
const { Client, Intents } = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.channel.send('pong');
});
});
Please refer the below links
Discord.JS documentation: https://github.com/discordjs/discord.js
Stack-overflow answer: Discord.js sending a message to a specific channel
I want to make an unban command, but I still get some errors along the way and I don't know what to do.
TypeError: message.member.hasPermissions is not a function that is my error that i get
Please help me. I am a beginner at discord.js and this is my unban.js code:
const Discord = require('discord.js');
const client = new Discord.Client();
const embed = new Discord.MessageEmbed()
.setColor('#0B15A3')
module.exports = {
name: 'unban',
description: "This unbans a user",
execute(message, args, client, Discord) {
if (message.member.hasPermissions("BAN_MEMBERS") ){
if (!isNaN(args[0])) {
const bannedMember = message.guild.members.cache.get(args[0]) // Get the `member` property instead to recall later.
var reason = args.slice(1).join(" ");
if(!reason) {
reason = "No reason given!"
}
if (bannedMember) {
bannedMember
message.guild.members.unban(bannedMember.id, reason)
.then(() => {
embed.setDescription(`Successfully unbanned **${bannedMember.user.tag}**`); // `user` is undefined.
message.channel.send(embed);
})
.catch(err => {
embed.setDescription('I was unable to unban the member');
message.channel.send(embed);
console.error(err);
});
} else {
embed.setDescription("That user isn't in this guild!");
message.channel.send(embed);
}
} else {
embed.setDescription("You need to provide an user ID to unban");
message.channel.send(embed);
}
} else {
embed.setDescription("You do not have `BAN_MEMBERS` permissions to unban this member");
message.channel.send(embed);
}
}
}
and this is my index.js code:
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});
const prefix = '-';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command)
}
client.once('ready', () => {
console.log(`${client.user.tag} is online!`);
client.user.setPresence({
status: 'online',
activity: {
name: "Prefix: -",
type: "PLAYING"
}
});
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
} else if(command === 'invite'){
client.commands.get('invite').execute(message, args, Discord);
} else if(command === 'help'){
client.commands.get('help').execute(message, args, Discord);
} else if(command === 'clear'){
client.commands.get('clear').execute(message, args);
} else if(command === 'nuke'){
client.commands.get('nuke').execute(message, args, Discord);
} else if(command === 'supersecretrules'){
client.commands.get('supersecretrules').execute(message, args, Discord);
} else if(command === 'support'){
client.commands.get('support').execute(message, args, Discord);
} else if(command === 'kick'){
client.commands.get('kick').execute(message, args);
} else if(command === 'ban'){
client.commands.get('ban').execute(message, args);
} else if(command === 'unban'){
client.commands.get('unban').execute(message, args);
}
});
client.login('token');
Can you help me? Thanks in advance!
You made a typo mistake. It is hasPermission() and not hasPermissions():
const Discord = require('discord.js');
const client = new Discord.Client();
const embed = new Discord.MessageEmbed()
.setColor('#0B15A3')
module.exports = {
name: 'unban',
description: "This unbans a user",
execute(message, args, client, Discord) {
if (message.member.hasPermission("BAN_MEMBERS") ){
if (!isNaN(args[0])) {
const bannedMember = message.guild.members.cache.get(args[0]) // Get the `member` property instead to recall later.
var reason = args.slice(1).join(" ");
if(!reason) {
reason = "No reason given!"
}
if (bannedMember) {
bannedMember
message.guild.members.unban(bannedMember.id, reason)
.then(() => {
embed.setDescription(`Successfully unbanned **${bannedMember.user.tag}**`); // `user` is undefined.
message.channel.send(embed);
})
.catch(err => {
embed.setDescription('I was unable to unban the member');
message.channel.send(embed);
console.error(err);
});
} else {
embed.setDescription("That user isn't in this guild!");
message.channel.send(embed);
}
You have made a simple typography error. The correct function name is GuildMember#hasPermission.
Change:
message.member.hasPermissions("BAN_MEMBERS")
To:
message.member.hasPermission("BAN_MEMBERS")
I cannot ban a GuildMember using its ID, only with a mention. There are no errors in the console.
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.channel.send("Invalid Permissions");
}
let User = message.guild.member(message.mentions.users.first()) || message.guild.members.cache.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}`);
You are missing cache, introduced in discord.js V12 - please look here
let userPending = message.guild.members.cache.fetch(message.mentions.members.first().id) || message.guild.members.cache.fetch(args[0]);
let user = await userPending;
This will ensure the bot searches for the correct guildMember
I would like to know how to make a ban, kick and clear commands for my bot. I will show you the code but before that I have been doing reasearch and nothing been working. I use node.js and i am a beginner so here's what i need you to tell me.
-The code for the command (No external links)
-How to to get the code to work.
-Where to put the code.
Okay, heres the code.
const Discord = require('discord.js');
const bot = new Discord.Client();
const { MessageEmbed } = require("discord.js");
bot.once('ready', () => {
console.log('Ready!');
});
bot.on('message', message => {
if(message.content === '!help') {
let embed = new MessageEmbed()
.setTitle("Ratchet Commands")
.setDescription("!getpizza, !shutup, !playdead, !server-info, !myname, !banhammer, !yourcreator, !annoy, !youare")
.setColor("RANDOM")
message.channel.send(embed)
}
});
bot.on('message', message => {
if (message.content === '!getpizza') {
message.channel.send('Welcome to Ratchets Pizza!!! Heres your pizza and have a nice day!!! :pizza:');
}
});
bot.on('message', message => {
if (message.content === '!shutup') {
message.channel.send('Okay, I am sorry.');
}
});
bot.on('message', message => {
if (message.content === '!server-info') {
message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
}
});
bot.on('message', message => {
if (message.content === '!myname') {
message.channel.send(`Your username: ${message.author.username}`);
}
});
bot.on('message', message => {
if (message.content === '!rocket') {
message.channel.send('3..2..1..Blast Off!!! :rocket:');
}
});
bot.on('message', message => {
if (message.content === '!youare') {
message.channel.send(`I am <#!808773656700256318>`);
}
});
bot.on('message', message => {
if(message.content === '!yourcreator') {
let embed = new MessageEmbed()
.setTitle("Ratchets Creator")
.setDescription("My creator is <#!765607070539579402>")
.setColor("RANDOM")
message.channel.send(embed)
}
});
bot.on('message', message => {
if(message.content.startsWith(`!annoy`)) {
const mentionedUser = message.mentions.users.first();
if(!mentionedUser) return message.channel.send("You have to mention someone to continue annoying someone :rofl:");
mentionedUser.send('You have a problem with me?');
message.channel.send("Annoyed " + mentionedUser + "! (Oh wait, I annoyed them 2 times!)");
}
});
module.exports = {
name: '!kick',
description: "this command kicks people",
execute(message, args){
const member = message.mentions.users.first();
if(member){
const memberTarget = message.guild.member(member);
memberTarget.kick();
message.channel.send("bing bong he is gone!");
}else{
message.channel.send('you couldnt kick that person');
}
}
}
bot.login('TOKEN');
I know I need a command handler but I don't know how to do that neither.
Thank you!!!
So first you did a very big error, you always recall the message event, optimize your code like this:
const Discord = require('discord.js');
const bot = new Discord.Client();
const { MessageEmbed } = require("discord.js");
bot.once('ready', () => {
console.log('Ready!');
});
bot.on('message', message => {
if(message.content === '!help') {
let embed = new MessageEmbed()
.setTitle("Ratchet Commands")
.setDescription("!getpizza, !shutup, !playdead, !server-info, !myname, !banhammer, !yourcreator, !annoy, !youare")
.setColor("RANDOM")
message.channel.send(embed)
if (message.content === '!getpizza') {
message.channel.send('Welcome to Ratchets Pizza!!! Heres your pizza and have a nice day!!! :pizza:');
}
if (message.content === '!server-info') {
message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
}
if (message.content === '!myname') {
message.channel.send(`Your username: ${message.author.username}`);
}
if (message.content === '!rocket') {
message.channel.send('3..2..1..Blast Off!!! :rocket:');
}
if (message.content === '!youare') {
message.channel.send(`I am <#!808773656700256318>`);
}
if(message.content === '!yourcreator') {
let embed = new MessageEmbed()
.setTitle("Ratchets Creator")
.setDescription("My creator is <#!765607070539579402>")
.setColor("RANDOM")
message.channel.send(embed)
}
if(message.content.startsWith(`!annoy`)) {
const mentionedUser = message.mentions.users.first();
if(!mentionedUser) return message.channel.send("You have to mention someone to continue annoying someone :rofl:");
mentionedUser.send('You have a problem with me?');
message.channel.send("Annoyed " + mentionedUser + "! (Oh wait, I annoyed them 2 times!)");
}
});
module.exports = {
name: '!kick',
description: "this command kicks people",
execute(message, args){
const member = message.mentions.users.first();
if(member){
const memberTarget = message.guild.member(member);
memberTarget.kick();
message.channel.send("bing bong he is gone!");
}else{
message.channel.send('you couldnt kick that person');
}
}
}
bot.login('TOKEN');
then to do a !ban #mention you can do:
if (message.content.startsWith('!ban ')) {
if (message.mentions.members.first()) {
if (!message.mentions.members.first().bannable) {
message.channel.send('I can\'t ban the specified user')
return
}
try {
message.mentions.members.first().ban()
}finally {
message.channel.send('The member was correctly banned')
}
}
}
and for a !kick
if (message.content.startsWith('!kick ')) {
if (message.mentions.members.first()) {
if (!message.mentions.members.first().kickable) {
message.channel.send('I can\'t kick the specified user')
return
}
try {
message.mentions.members.first().kick
}finally {
message.channel.send('The member was correctly kicked')
}
}
}
and finally for the clear command
if (message.content.startsWith('!clear ')) {
let toClear = args[1]
for (let i = 0; i < toClear; i++) {
m.channel.lastMessage.delete()
}
}
Hope i helped you ^^