Ban module, Discord bot error.. discord.js - javascript

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);
});

Related

Ban member from other guild if banned in main guild

So I want it where, if a user gets banned from this specified guild, it will ban them in every other guild that the bot is in. Do I use fetch bans to do this?
Before we continue you need to make sure that you are requesting the following intents when creating your bot, as they are necessary to achieve your goal: GUILDS, GUILD_BANS.
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_BANS]
});
You should also set up a constant called MAIN_GUILD_ID or something along those lines:
const MAIN_GUILD_ID = 'ID';
Now you need to listen to the guildBanAdd event and check if the GuildBan.guild#id equals the main Guild#id (making sure the user was banned in the main Guild).
client.on('guildBanAdd', async ban => {
if (ban.guild.id !== MAIN_GUILD_ID) return;
});
Then you can loop through all of the Guilds your bot is in and ban the user there.
client.on('guildBanAdd', async ban => {
if (ban.guild.id !== MAIN_GUILD_ID) return;
// Getting the guilds from the cache and filtering the main guild out.
const guilds = await client.guilds.cache.filter(guild => guild.id !== MAIN_GUILD_ID);
guilds.forEach(guild => {
guild.members.ban(ban.user, {
reason: `${ban.user.tag} was banned from ${guild.name}.`
}).then(() => {
console.log(`Banned ${ban.user.tag} from ${guild.name}.`);
}).catch(err => {
console.error(`Failed to ban ${ban.user.tag} from ${guild.name}.`, err);
});
})
});
I'm assuming this is for a private bot that is in a few guilds, otherwise, if the bot is in a few hundred or more guilds it will get your application rate-limited pretty fast.

I can't understand why does it not send Discord.js v13

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.

discord.js Banning users outside server

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);

My kick command lets anyone in the server kick someone

I need my kick command for my discord bot only be able to work for moderators and admins. Does anyone have any more coding that could make it so only mods or admins could kick?
My coding for the kick command:
client.on('message', (message) => {
if (!message.guild) return;
if (message.content.startsWith('!kick')) {
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
message.reply(`Successfully kicked ${user.tag}`);
})
.catch((err) => {
message.reply('I was unable to kick the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
}
});
You can use GuildMember.hasPermission to check if a user has a certain permission. You can see the valid permission flags here, although I think you'll want to use KICK_MEMBERS in this case.
if (!message.member.hasPermission('KICK_MEMBERS'))
return message.channel.send('Insufficient Permissions');
You can also restrict access via the roles someone has, for which I urge you to read this existing answer

How to add permissions to user to channel by command? Discord.js

How to give permissions to a specific channel by command? Sorry, I’m new at discord.js so any help would be appreciated.
const Discord = require('discord.js');
module.exports = {
name: 'addrole',
run: async (bot, message, args) => {
//!addrole #user RoleName
let rMember =
message.guild.member(message.mentions.users.first()) ||
message.guild.members.cache.get(args[0]);
if (!rMember) return message.reply("Couldn't find that user, yo.");
let role = args.join(' ').slice(22);
if (!role) return message.reply('Specify a role!');
let gRole = message.guild.roles.cache.find((r) => r.name === role);
if (!gRole) return message.reply("Couldn't find that role.");
if (rMember.roles.has(gRole.id));
await rMember.addRole(gRole.id);
try {
const oofas = new Discord.MessageEmbed()
.setTitle('something')
.setColor(`#000000`)
.setDescription(`Congrats, you have been given the role ${gRole.name}`);
await rMember.send(oofas);
} catch (e) {
message.channel.send(
`Congrats, you have been given the role ${gRole.name}. We tried to DM `
);
}
},
};
You can use GuildChannel.updateOverwrites() to update the permissions on a channel.
// Update or Create permission overwrites for a message author
message.channel.updateOverwrite(message.author, {
SEND_MESSAGES: false
})
.then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
.catch(console.error);
(From example in the discord.js docs)
Using this function, you can provide a User or Role Object or ID of which to update permissions (in your case, you can use gRole).
Then, you can list the permissions to update followed by true, to allow, or false, to reject.
Here is a full list of permission flags you can use
This method is outdated and doesn't work on V13+ the new way is doing this:
channel.permissionOverwrites.edit(role, {SEND_MESSAGES: true }
channel.permissionOverwrites.edit(member, {SEND_MESSAGES: true }

Categories