Mention bot for respond discord.js v14 - javascript

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

Related

I want to set when someone mention me bot will respond

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

Bot not responding when automoderation is disabled

On my Discord bot, users are able to choose for a discord invite detection automod if they want the bot to delete the message, kick the user, ban the user, or softban the user with all of this being stored on MongoDB. But when a user disables automod on their guild, it stops responding to all commands until it is turned back on manually by editing MongoDB files
here is my message.js code:
https://haste.pancake.gg/ludiritaga.js
It's actually a relatively simple mistake, believe it or not. I've made this mistake many times before, so you aren't alone!
Your command detection statement is enclosed within the if(linkDetect === `on`) {...} statement.
)}}}} //Add a closing bracket here. This will exclude the portion of code that operates command detection from the else-if statement that checks if invite detection is on, allowing commands to be used while the feature is disabled.
if (!message.content.toLowerCase().startsWith(prefix)) return;
const args = message.content.substring(prefix.length).split(' ');
const command =
client.commands.get(args[0].toLowerCase()) ||
client.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(args[0].toLowerCase())
);
if (!command) return;
try {
command.execute(message, args, client);
}
catch(err) {
console.log(err)
const errorEmbed = new MessageEmbed()
.setColor('RED')
.setDescription('**<:xmark:763662710130737152> There was an error, please report it in my [support server](https://discord.gg/q4dunRn).**\n\n' + `\`${err}\``)
message.channel.send(errorEmbed)
}
} //Remove this bracket, as it is unnecessary once you add an additional closing bracket as suggested above. This bracket originally closed the else-if statement that only ran when invite detection was enabled
}
Also, to avoid this in the future (if you don't already do this), you can use an IDE that allows you to see where both of a code block's brackets are, so you can troubleshoot this kind of issue easier in the future.
Hope I could help!

making a discord bot say the rules when aksed

hello I am trying to code a Discord bot, I am trying to make it so that if someone says $rules the rules will be sent, I can't send the code because I have a problem with posting something on this website, I have searched a lot on the internet but I can not find anything
edit: I have found out how I can post the code https://glitch.com/edit/#!/melted-messy-leotard
this is the link to see it
You have a syntax error in your code - you never close the listener with any brackets.
To fix this you can use this:
let Discord = require("discord.js");
let client = new Discord.Client();
client.on("message", message => {
if (message.content === "ping") {
message.channel.send("pong!")
}
if(message.content === "embed") {
let embed = new Discord.MessageEmbed()
.setTitle("this is Emded title")
.setDescription("this is Embed description")
.setColor("RANDOM")
.setFooter("This is embed footer")
message.channel.send(embed)
}
if (message.content.startswith("$rules")) {
let member = message.mentions.members.first()
if (!member) message.send("here are the rules, 1. no politics 2. be nice 3. respect others 4. think before you speak 5. no nsfw 6. don't leak personal info 7.don't argue abt opinions, 3 strikes and you're out")
}
});
client.login("token");
Also your line where you do if(!member) probably won't do what you want it to do - this will respond if it DMs the bot, if this is what you wanted then it is fine.
If you want to check if the member is in a guild, you can check if they have a common role (such as everyone) using this:
if (!member.roles.cache.some(role => role.name === '<role name>')) {
// member is not in guild
}

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.

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