If I don't define a person to ban, it shuts my bot off.
ERROR: TypeError: Cannot read property 'displayName' of undefined
if (msg.member.roles.cache.has()) {
let user = msg.mentions.members.first();
msg.channel.send(`🔨│ Banned #${user.displayName}!`);
user.ban();
}
Yes, if you don't give a mention, then it will generate an error and the bot will crash.
You should make a return statement where you prevent this from happening.
if (msg.member.roles.cache.has('BAN_MEMBERS')) {
let member = msg.mentions.members.first();
// prevents from not mentionning
if (!member) return msg.channel.send(`Give a user to ban !`);
// If the code continues, then there is a mention
try {
user.ban();
msg.channel.send(`🔨│ Banned #${member.user.displayName}!`);
} catch (err) {
msg.channel.send('I cannot ban this member !');
}
} else msg.channel.send('You do not have the permission to do this');
So I fixed many problems :
First, if you want the name of a member you'll have to use the user object. So you can't use member.displayName but member.user.username
Second, you need to prevent that if the bot can't ban the member. If for example, the member is an administrator or the owner of the server
Last, what you asked for, if there is no mention in the message, it will send a message telling you that you didn't pass a member to ban
I suggest you check out some tutorials like the discord.js guide
Related
I need to make that not everyone can kick members, but only who have permissions
Here is my kick system-
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.members.resolve(user);
if (member) {
member
.kick({
reason: 'They were bad!',
})
.then(() => {
message.channel.send(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.channel.send('I was unable to kick the member');
console.error(err);
});
} else {
message.channel.send("That user isn't in this guild!");
}
} else {
message.channel.send("You didn't mention the user to kick!");
}
}
});
Welcome to StackOverflow! And, from the look of things, the beginning of your bot-making journey.
You can use message.mentions.members.first() to skip a step and simplify your code somewhat.
As mentioned above, you'll want to use member.permissions.has() to check if a member has the MANAGE_MEMBERS permission, or whatever other permission you want to check for. (See Permissions.FLAGS)
Also, consider moving away from using message-based commands, if possible. For a small personal bot, it's not really an issue, but Discord is really pushing the new Interactions (aka slash commands) system, which you can read more about here (discord.js guide) and here (official, discord.com).
in order to make only mods use a command I recommend you using message.member.permissions.has() which allows the bot to check if the person who ran the command has a certain permission in the guild or not. Then to return, if the person doesn't have permission, you can use it in an if statement like that:
if (!message.member.permissions.has("KICK_MEMBERS")) {
return message.reply({content: `You need permissions to use command`})
}
And of course you can always add in your messageCreate event a userPermissions to add on the top of your command.
So the event file would have:
if (!message.member.permissions.has(<command>.userPermissions || [])) return message.channel.send({content: 'You need permissions to run this command'})
then you can just add in your command under name or description userPermissions: ['KICK_MEMBERS'],
This is the easiest way to handle user permissions
There are a lot of to ban a user in a server, you can do it manually or with another bot. I want my bot to scan a banlist, and if someone will get banned, it will send a message "user (id) has been banned
I started writing smt but now, I had no idea what can I do to check it.
EDIT: It's working now, but it still need to find this user id.
client.on('guildBanAdd', message => {
client.user.cache.find() //idk how to define it
const channel1 = client.channels.cache.find(channel => channel.id === "158751278127895");
channel1.send('12512');
})
You don't have to fetch bans because the guildBanAdd event parses a GuildBan object,which als includes a User object.
client.on('guildBanAdd', ban => {
console.log(ban.user.username); // Username
})
message.channel won't work because there isn't a message object.
If you want to get the executor (moderator) who performed the ban, kick etc.
There is a good guide for this Working with Audit Logs
I want to make my discord bot send different messages when anyone types a command in a normal channel or NSFW channels.
I followed the documentation, which I didn't quite get it. I wrote the testing commands below:
client.on('message', message => {
if (command === 'testnsfw') {
if (this.nsfw = Boolean(true.nsfw)) {
return message.channel.send('yes NSFW');
} else return message.channel.send('no NSFW');
}
})
I think it is not working. The bot only responds "no NSFW" on both channels.
You cannot refer to the TextChannel using this in the anonymous function. (Also, this is always undefined in an arrow function) You can access the TextChannel using the Message class, stored in the message variable.
client.on("message", message => {
// Making the sure the author of the message is not a bot.
// Without this line, the code will create an infinite loop of messages, because even if a message is sent by a bot account, the client will emit the message event.
if (message.author.bot) return false;
// message.content is a String, containing the entire content of the message.
// Before checking if it equals to "testnsfw", I would suggest to transform it to lowercase first.
// So "testNSFW", "testnsfw", "TeStNsFw", etc.. will pass the if statement.
if (message.content.toLowerCase() == "testnsfw") {
// You can get the Channel class (which contains the nsfw property) using the Message class.
if (message.channel.nsfw) {
message.channel.send("This channel is NSFW.");
} else {
message.channel.send("This channel is SFW.");
}
}
});
What I encourage you to read:
Message
TextChannel
now you can just do this instead
if(!message.channel.nsfw) return message.channel.send('a message to send to channel')
this is using
discord.js verson 13.6.0
I'm fairly new to javascript and have been using Discord.js to make one or two Discord Bots. I'm working on a feature that sends a message when I'm pinged in my discord server. I've tried a few things and none have worked.
I have this so far, it detects when any user is pinged, not just me.
client.on('message', (message) => {
if (message.mentions.members.first()) {
message.channel.send('Do not ping this user.');
}
});
You can compare User IDs. How to get User IDs.
if (message.mentions.users.first().id === 'Your ID') // if the person mentioned was you
return message.channel.send('Do not mention this user');
Furthermore, as the name suggests, Collection.first() will fetch the first element of a collection. This means that the if statement will only return true if the first mention was you. For example:
User: 'Hello #you' // detected
User: 'Hello #notYou and #you' // not detected
To circumvent this, you can use Collection.has():
// will return true if *any* of the mentions were you
if (message.mentions.users.has('Your ID'))
return message.channel.send('Do not mention this user');
I'm trying to make my discord.js bot send a message when it is pinged. I was unsure how to do this so I referred to this code:
client.on('message', message => {
if (message.content === '<#745648345216712825>') {
message.channel.send('Message Here');
}
});
However, this doesn't work.
Also, is it possible that my bot responds when a person mentions a specific user for example if I am mentioned by the user anywhere in a message the bot responds? If yes, can you show me how to do it?
Message has a property called mentions, which contains all the channels, members, roles, and users mentioned in the message. You can use the method .has(data, [options]) of MessageMentions to see if your bot was mentioned.
client.on("messageCreate", (message) => {
if (message.author.bot) return false;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return false;
if (message.mentions.has(client.user.id)) {
message.channel.send("Hello there!");
}
});
The message event has been renamed to messageCreate in Discord.JS v13. Using message will still work, but you'll receive a deprecation warning until you switch over.
discord.js just got updated you can use
client.on('message', message => {
if (message.mentions.has(client.user)) {
message.channel.send('your message');
}
});
One of the best ways to check if only your bot is mentioned in the entire message is, regex. You can use this regular expression to check if only the client is mentioned:
/^<#!?${<client>.user.id}>( |)$/
You can check message by using the match method of String. In our case, String is message.content:
if (message.content.match(/^<#!?${client.user.id}>( |)$/)) {
return message.channel.send("Thanks for mentioning me! my prefix is ...");
};