Ban Command in Discord.js won't Compile - discord.js

I have a command which bans a user. I have tried changing the last part to several different variations yet if I try modChannel.message.send, it throws the error "modChannel already defined." I'm not sure what else to do.
module.exports = {
name: "ban",
description: "Ban the troublesome users!",
execute(message, args) {
let member = message.mentions.user.first();
if(!message.member.roles.some(r=>["Administrator"].includes(r.name))) {
return message.channel.send("You don't have the permissions to use this command!");
}
if(!member.bannable) {
return message.channel.send("I can\'t ban this person. Please try again. Make sure I have ban permissions.");
}
let reason = args.slice(7).join(" ");
if(!reason) {
reason = "No reason was provided.";
}
let modChannel = client.guilds.find(ch => ch.name === "mod-log");
const banEmbed = {
color: 225,
title: "User Banned",
description: `**User**: ${member.username}#${member.discriminator} (${member.id})
**Reason**: ${reason}
**Banned By**: ${message.author.username}#${message.author.discriminator}`,
},
modChannel.send({embed: banEmbed });
},
};

Try:
let modChannel = message.guild.channels.find(c => c.name === "logs)

Related

I want to check if someone has permission to be kicked, but the answer is wrong

I use msg.member.hasPermission('KICK_MEMBERS'), but even if the author of the message doesn't have this permission, the answer is true instead of false.
const Discord = require("discord.js");
const client = new Discord.Client();
channel = '858963360909099068';
module.exports ={
name: 'kick',
description: 'kick people',
execute(msg, args) {
let impostor = msg.mentions.members.first();
const mod = msg.author;
let botResponse;
if(msg.member.hasPermission('KICK_MEMBERS')){
console.log('true');
if(!impostor){
botResponse = 'You need to tag the person that you want to kick !';
impostor = 'Not specified';
}
else{
botResponse = `${impostor} was kicked !`;
impostor.kick();
}
}
else{
console.log('false');
botResponse = 'You are not able to kick people !';
}
let embed = new Discord.MessageEmbed()
.setColor('#f9ae0e')
.setTitle('Kick')
.setDescription(botResponse)
.addFields(
{ name: "Impostor", value: impostor},
{ name: "Member", value: mod}
);
msg.channel.send(embed);
}
}
This is the error:
TypeError: msg.member.hasPermission is not a function
I can't see what's wrong here, can you help me solve it?
You should change msg.member.hasPermission('PERM') to msg.member.permissions.has('PERM')

When i try to create a role it gives me an error (discord.js)

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.

discord.js v12 | TypeError: Cannot read property 'send' of undefined

Here is my entire code for my ban command. Good to note I am using Discord.JS Commando as well I have been struggling with this error but literally cannot figure out why I am getting it everything looks fine unless I have used a deprecated function. Would really appreciate someone to help me on this one I've been getting along quite well creating a rich featured bot before this happened.
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const db = require('quick.db');
module.exports = class banCommand extends Command {
constructor(client) {
super(client, {
name: 'ban',
memberName: "ban",
group: 'moderation',
guildOnly: true,
userPermissions: ['BAN_MEMBERS'],
description: 'Bans the mentioned user from the server with additional modlog info.'
});
}
async run(message, args) {
if (!args[0]) return message.channel.send('**Please Provide A User To Ban!**')
let banMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(r => r.user.username.toLowerCase() === args[0].toLocaleLowerCase()) || message.guild.members.cache.find(ro => ro.displayName.toLowerCase() === args[0].toLocaleLowerCase());
if (!banMember) return message.channel.send('**User Is Not In The Guild**');
if (banMember === message.member) return message.channel.send('**You Cannot Ban Yourself**')
var reason = args.slice(1).join(' ');
if (!banMember.bannable) return message.channel.send('**Cant Kick That User**')
banMember.send(`**Hello, You Have Been Banned From ${message.guild.name} for - ${reason || 'No Reason'}**`).then(() =>
message.guild.members.ban(banMember, { days: 7, reason: reason })).catch(() => null)
message.guild.members.ban(banMember, { days: 7, reason: reason })
if (reason) {
var sembed = new MessageEmbed()
.setColor('GREEN')
.setAuthor(message.guild.name, message.guild.iconURL())
.setDescription(`**${banMember.user.username}** has been banned for ${reason}`)
message.channel.send(sembed)
} else {
var sembed2 = new MessageEmbed()
.setColor('GREEN')
.setAuthor(message.guild.name, message.guild.iconURL())
.setDescription(`**${banMember.user.username}** has been banned`)
message.channel.send(sembed2)
}
let channel = db.fetch(`modlog_${message.guild.id}`)
if (channel == null) return;
if (!channel) return;
const embed = new MessageEmbed()
.setAuthor(`${message.guild.name} Modlogs`, message.guild.iconURL())
.setColor('#ff0000')
.setThumbnail(banMember.user.displayAvatarURL({ dynamic: true }))
.setFooter(message.guild.name, message.guild.iconURL())
.addField('**Moderation**', 'ban')
.addField('**Banned**', banMember.user.username)
.addField('**ID**', `${banMember.id}`)
.addField('**Banned By**', message.author.username)
.addField('**Reason**', `${reason || '**No Reason**'}`)
.addField('**Date**', message.createdAt.toLocaleString())
.setTimestamp();
var sChannel = message.guild.channels.cache.get(channel)
if (!sChannel) return;
sChannel.send(embed)
}
};
The reason you are getting the TypeError: args.slice(...).join is not a function error is because the slice method creates a new array of the sliced data, and so can not join(' ') since there is no space to join with. (i.e. it is not a string)
What you are looking for is args.slice(1).toString().replace(",", " ")
This removes the 2nd part of the args array object, then converts it to a string, then removes the commas in the string and replaces them with spaces.

Time Variable Is Not Defined in Discord.JS

My setTimeout() is used for a certain amount of time as the user states. So, if the user put in 20, the muted member would be muted for 20 minutes. The problem I am having is that the time says it is undefined at the end. I'm not sure why it's not working. The syntax would be something like !mute user time reason.
module.exports = {
name: "mute",
description: "Mute those members!",
execute(message, args) {
const {RichEmbed} = require("discord.js");
setTimeout(() => {
if(!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("You do not have sufficient permissions to perform this command. Please try again");
let muteMember = message.mentions.members.first() || message.members.get(args[0]);
if(!muteMember) return message.channel.send("You did not provide a member to mute. Please provide a user to mute");
let time = parseInt(args[2]) * 60000;
if(!time) return message.channel.send("You did not provide how long to mute this member. Please provide a time in minutes");
let reason = args.slice(2).join(" ");
if(!reason) reason = "No reason provided";
if(!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send("I do not have sufficient permissions to mute this member. Please try again");
let muteRole = message.guild.roles.find(role => role.name === "Muted");
if(!muteRole) {
try {
muteRole = message.guild.createRole({
name: "Muted",
permissions: []
})
message.guild.channels.forEach(async (channel, id) => {
await channel.overwritePermissions(muteRole, {
SEND_MESSAGE: false,
ADD_REACTIONS: false,
SEND_TTS_MESSAGES: false,
ATTACH_FILES: false,
SPEAK: false
})
})
} catch (e) {
console.log(e.stack);
}
}
muteMember.addRole(muteRole.id). then(() => {
message.delete();
muteMember.send(`You have been muted in ${message.guild.name} for ${reason}`).catch(err =>console.log(err));
message.channel.send(`${muteMember.user.username} has been successfully muted`);
let muteEmbed = new RichEmbed()
.setColor("#FF0000")
.addField("Moderation Action", "mute")
.addField("Muted Member", muteMember.user.username)
.addField("Moderator", message.author.username)
.addField("Reason", reason)
.addField("Date:", message.createdAt.toLocaleAString());
let modChannel = message.guild.channels.find(channel => channel.name === "mod-log");
modChannel.send(muteEmbed);
})
}, time)
},
};
You need to get the time argument first before the reason argument since at the moment you're putting the time variable into the reason variable
if (isNaN(args[1]) return message.reply('Please enter a valid number');
let time = parseInt(args[2]) * 60000;
let reason = args.slice(2).join(" ");
if (!reason) reason = "No reason provided";
if (!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send("I do not have sufficient permissions to mute this member. Please try again");

How to ban and kick a GuildMember

Well, I followed "The Source Code" "discord.js" tutorial, (even copy-pasted his code from GitHub) but the ban and kick commands he's shown don't work, I assume they got broken by a recent update. It sends the embed to the incidents channel but doesn't actually ban the player. Also, if you have any suggestions for me to change things, please suggest!
if (cmd === `${prefix}ban`) {
let bUser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!bUser) return message.channel.send("Can't find user!");
let bReason = args.join(" ").slice(22);
if (!message.member.hasPermission("MANAGE_MEMBERS")) return message.channel.send("No can do pal!");
if (bUser.hasPermission("MANAGE_MESSAGES")) return message.channel.send("That person can't be banned!");
let banEmbed = new Discord.RichEmbed()
.setDescription("Ban Management")
.setColor("#bc0000")
.addField("Banned User", `${bUser} with ID ${bUser.id}`)
.addField("Banned By", `<#${message.author.id}> with ID ${message.author.id}`)
.addField("Banned In", message.channel)
.addField("Time", message.createdAt)
.addField("Reason", bReason);
let incidentchannel = message.guild.channels.find(`name`, "incidents");
if (!incidentchannel) return message.channel.send("Can't find incidents channel.");
message.guild.member(bUser).ban(bReason);
message.delete().catch(O_o => {});
incidentchannel.send(banEmbed);
return;
}
message.guild.member(bUser).ban(bReason);
This will not ban the member. The message has a member property so you don't need to use message.guild.member you can just easily use message.member.
So it should look like this:
if (cmd === `${prefix}ban`) {
let bUser = message.guild.member(message.mentions.members.first() || message.guild.members.get(args[0]));
if (!bUser) return message.channel.send("Can't find user!");
let bReason = args.join(" ").slice(22);
if (!message.member.hasPermission("MANAGE_MEMBERS")) return message.channel.send("No can do pal!");
if (bUser.hasPermission("MANAGE_MESSAGES")) return message.channel.send("That person can't be banned!");
let banEmbed = new Discord.RichEmbed()
.setDescription("Ban Management")
.setColor("#bc0000")
.addField("Banned User", `${bUser.user.tag} with ID ${bUser.id}`)
.addField("Banned By", `<#${message.author.id}> with ID ${message.author.id}`)
.addField("Banned In", message.channel.name)
.addField("Time", message.createdAt)
.addField("Reason", bReason);
let incidentchannel = message.guild.channels.find(`name`, "incidents");
if (!incidentchannel) return message.channel.send("Can't find incidents channel.");
message.guild.member(bUser).ban({
reason: bReason
});
message.delete();
incidentchannel.send({
embed: banEmbed
});
return;
}
I changed a lot because a lot was outdated and could not work. It could be that I haven't seen one or the other mistake.
Let me know if it worked! :)
Best regards,
Monkeyyy11
This seems awfully complicated, I hope my command can make things a bit easier!
bot.on('message', message => {
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "kick") {
if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("Permissions invalid");
const userKick = message.mentions.users.first();
if (userKick) {
var member = message.guild.member(userKick);
if (member) {
member.kick({
reason: `This person was kicked using a bot's moderation system. We are so sorry if this caused problems.`
}).then(() => {
message.reply(`A user been kicked.`)
})
} else {
message.reply(`User not found`);
}
} else {
message.reply(`Please enter a name`)
}}})
bot.on('message', message => {
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "ban") {
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Permissions invalid");
const userBan = message.mentions.users.first();
if (userBan) {
var member = message.guild.member(userBan);
if (member) {
member.ban({
reason: `This person was banned using a bot's moderation system. We are so sorry if this caused problems.`
}).then(() => {
message.reply(`a user has been banned!`)
})
} else {
message.reply(`User not found`);
}
} else {
message.reply(`Please enter a name`)
}}})
If you use discord.js v12 or higher, then RichEmbed is now deprecated. Instead, use MessageEmbed

Resources