I want to set when someone mention me bot will respond - javascript

I'm trying to make my discord.js bot send messages when someone pings me. How can I do that??
I was trying with that code:
if(message.content === "<#723821826291138611>") {
message.channel.send("Hello, sup? ")
}
but that doesn't work. how can I do that?

The best - and most conventional - way to do this is to check the MessageMentions#(users|members) collection.
const {
mentions:{
users,
repliedUser
}
} = message;
if (users.has("723821826291138611") && !repliedUser) {
// Your code
}
This will return true if the mentioned is found in any order not only first, it's possible for the API to emit the mentions in different orders if multiple mentions were given. I wouldn't recommend searching the message.content string either.

You have to use if(message.content.includes("<#723821826291138611>")) instead of if(message.content === "<#723821826291138611>") to make it work!
But also you can use this code to do it:
let mentioned = message.mentions.members.first();
if(mentioned && mentioned.id == "723821826291138611") {
if(message.mentions.repliedUser) return;
message.channel.send("YOUR_TEXT")
}

Related

Mention bot for respond discord.js v14

I'm trying to make a command when you mention the bot, it will send a message saying “Slash Command: /help”. I have tried to make it and I tried to find an answer but I can’t find anything that works for me.
Here is the code (I use the code in index.js):
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!");
}
});
I added the GatewayIntentBits.Guilds intent to my intents array. I don't get any errors, it just doesn’t work.
If you're only using the GatewayIntentBits.Guilds intent, you won't receive messages. You'll also need to add GatewayIntentBits.GuildMessages. It allows you to receive messages if the client/bot is mentioned.
If you also want to receive the message content when the bot is not mentioned, you'll also need to add GatewayIntentBits.MessageContent. However, if you only want to see if the bot is mentioned, adding GatewayIntentBits.GuildMessages to your intents array will be enough.
I recommend you use RegExp, it would be done like this:
client.on("messageCreate", (message) => {
if (message.author.bot) return;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return;
if (message.content.match(new RegExp(`^<#!?${client.user.id}>( |)$`))) {
message.channel.send("Hello there!");
}
});
The RegExp object is used for matching text with a pattern. So if it matches what you've typed (in this case the #bot ), send the message

Name shows up as "undefined"

So, I have been working on a kick command, and I pretty much got the whole thing working, except when I have the bot send a message telling us who was kicked, it just shows up as "undefined was kicked"
client.on("message", message => {
if (message.content.startsWith('grimm!kick')) {
var member = message.mentions.members.first();
if (message.member.hasPermission('KICK_MEMBERS')) {
const kicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.mentions.members.first.username} Was kicked`)
message.channel.send(kicked)
member.kick()
} else {
const notKicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.author.username}! you do not have permission to kick people! you must have the "Kick Members" Permission to use this command`)
message.channel.send(notKicked)
}}});
I have tried "member.username" and that still wont work
Could someone tell me what exactly I'm doing wrong, so I wont make this same mistake again? Thanks!
There are 2 problems in this code.
You are using first as a property, not a method.
the property username doesnt exist on The GuildMember class.
what you are looking for is GuildMember.user.username.
replace
.setDescription(`${message.mentions.members.first.username} Was kicked`)
with
.setDescription(`${message.mentions.members.first().user.username} Was kicked`)

Words filter stopped working - Discord.JS 12

Hello!
So I'm currently updating my bot to version 12.3.1 of DiscordJS. Unfortunately, I got stuck on a problem that I can't really find a workaround. So, my bot has a module to filter out all of the bad words, like profanity, racial slurs, etc.
It's currently working just fine on 11.4, but cannot get it to work on 12.3.1
For some reason, the bot just does not react at all to given message.
I had two "filters" one for words, one for invites. Both of them stopped working.
bot.on('message', async message => {
// Events Listeners
if (message.author.bot) return;
if (message.channel.type === 'dm') return;
let messageArray = message.content.split(' ');
let command = messageArray[0];
let args = messageArray.slice(1);
if (!command.startsWith(prefix)) return;
let cmd = bot.commands.get(command.slice(prefix.length)) || bot.aliases.get(command.slice(prefix.length));
if (cmd) cmd.run(bot, message, args);
// First filter
var array = ['testing', 'yes', 'no'];
if (array.includes(message.content.toLocaleLowerCase())) {
message.channel.send('test')
}
// Second filter
if (message.content.includes('discord.gg/') {
message.delete()
}
}
That's the current one I found from another StackOverflow post, made 2 months ago.
Discord.js V12 Rude words filter not working
I'd really love to get some help if possible, as I can't find any reason why this feature has stopped working.
Thank you! :)
Your filters are after your command handling logic.
You have the line:
if (!command.startsWith(prefix)) return;
early in your code, and this causes message handling to terminate immediately on any message which is not a command. Due to this, the code will never reach your filters unless the message starts with your bot's prefix, at which point the message content cannot possibly be equal to any of the words and is extremely unlikely to contain discord.gg/.
Simply move your filters to the beginning of your message handler. Or alternatively separate the command handling and filter handling into separate functions, so that the return statement above only exits the command handling and the filter handling will still run.

How do I find a role with a specific permission?

So I'm trying to create a discord bot and one of the commands for the bot needs me to filter roles by the permissions they're allowed (or, at least this would be the easiest way), and I can't seem to figure out how to do that. I've tried way too many things to even count, and at this point, I'm looking for a different way. But if anyone has the answer to this, that would be amazing.
Basically I wanna do this:
let staff = message.channel.guild.roles.cache.filter(s => s has this permission)
(Pseudo-code obviously)
But as I said, I just can't seem to figure it out.
case 'staff':
let staff = chan.guild.roles.cache.filter(m => m.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES));
//console.log(staff)
message.guild.channels.create(Channel, 'text').then (
function() {
let ch = message.guild.channels.cache.find(ch=>ch.name === Channel)
ch.updateOverwrite(message.guild.id, { VIEW_CHANNEL : false });
ch.updateOverwrite(staff, { VIEW_CHANNEL : true })
}
)
type.stop()
break;
Also, 'chan' is a shortened version of 'message.channel'
Is this what you're looking for?
let rolesWithManageMessagesPermission =
message.channel.guild.roles.cache.filter(r => r.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)

javascript discord case insensitive error

I'm new at JavaScript. I'm making a discord bot but I always get error saying that message.startsWith is not a function the reason is I wan't to make the bot chat respond to be not case insensitive. I'm on discord.js version 12.2.0 and
this is my code:
client.on("message", (message) => {
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (message.startsWith(prefix + "laugh")) {
message.author.send ("```haha lmao```")
}
}
I've been searching my problem on the internet but none of them solve my problem. It's been 3 days since I got this error.
Maybe startsWith is not supported. Check if String.prototype.startsWith is true.
Also you can use indexOf instead.
if (message.indexOf(prefix + "laugh") === 0) {
message.author.send ("```haha lmao```")
}
You have to replace message to message.content if you want to retrieve the full message sent by the user.
The message constructor contains much more than the user's message, see the docs.

Categories

Resources