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.
Related
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
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")
}
I've started my bot after a while and for some reason the mention command is not working. Code and response to the command is below:
client.on('message', message => {
if (message.content.startsWith('L!bite')) {
let targetMember = message.mentions.members.first();
if (!targetMember) return message.reply('you need to tag a user in order to bite them!!');
// message goes below!
message.channel.send(`${targetMember.user}, You just got a bitten!`);
message.channel.send({files:["./nom/"]})
//let embed = new Discord.RichEmbed()
//embed.setImage(`https://media1.tenor.com/images/4f5666861516073c1c8015b2af7e2d15/tenor.gif?itemid=14720869`)
message.channel.send(embed);
}
});
Response on Discord: (it does picks itself up as a user but not anyone else)
·._.•🎀 𝐹𝐵𝐼𝐿💗𝐿𝐼𝒢𝐼𝑅𝐿 🎀•._.·Today at 1:18 PM
L!bite #·._.•🎀 𝐹𝐵𝐼𝐿💗𝐿𝐼𝒢𝐼𝑅𝐿 🎀•._.·
._.·🎀𝐹𝐵𝐼𝐿♡𝓁𝒾𝔟𝓸𝓣🎀·._.BOT Today at 1:18 PM
#·._.•🎀 𝐹𝐵𝐼𝐿💗𝐿𝐼𝒢𝐼𝑅𝐿 🎀•._.·, you need to tag a user in order to bite them!!
I'm just confused on why it's saying that I'm or other people are not users, but it picks up itself? E.g.:
·._.•🎀 𝐹𝐵𝐼𝐿💗𝐿𝐼𝒢𝐼𝑅𝐿 🎀•._.·Today at 1:18 PM
L!bite #._.·🎀𝐹𝐵𝐼𝐿♡𝓁𝒾𝔟𝓸𝓣🎀·._.
._.·🎀𝐹𝐵𝐼𝐿♡𝓁𝒾𝔟𝓸𝓣🎀·._.BOT Today at 1:18 PM
#._.·🎀𝐹𝐵𝐼𝐿♡𝓁𝒾𝔟𝓸𝓣🎀·._., You just got a bitten!
GIF HERE
Try converting the message.mentions.members to array and then filter your bot! -
const mentions = message.mentions.members.array() // will return members array of GuildMember class
const firstMention = mentions.filter(mention=> !mention.user.id === "BOT_ID")[0]
Alternatively, there's already a parameter you can implement in your code where it can automatically check if the message came from a bot. Although, this process may not work if you want to have other bots automatically tag it, but I think that's unlikely.
if (message.author.bot) return; FYI, not sure of which version of discord.js you're using, but the embed naming convention has changed. Instead of RichEmbed(), you should use MessageEmbed().
So the place you would implement this is right after your first if conditional block, like this:
if (message.content.startsWith('L!bite')) {
//the thing that checks if the user who posted was the bot
if (message.author.bot) return;
Hopefully this helps!
client.on("message", (message) => {
if(message.content.startsWith("!Bonk")){
let userToMention = message.mentions.users.first();
message.channel.send("bonks "+userToMention.toString() + " https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
}
});
What this does is when you run the command !bonk #devildog the bot displays a gif + #devildog.
But when you run the command !bonk alone the bot randomly crashes. Is there any way you can solve this issue with if else?
Error:
message.channel.send("bonks "+userToMention.toString() + "https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
^
TypeError: Cannot read property 'toString' of undefined
Since in your example, the invocation command doesn't include any mentions, the call to message.mentions.users.first(); will cause a crash. When no users are mentioned (like in your example), the first() method won't be able to find the first user in users, because there are no users mentioned at all! Thus, calling toString() on a null/undefined object will result in an exception.
Check for users in your initial if statement to prevent this behavior:
client.on("message", (message) => {
if(message.content.startsWith("!Bonk") && message.mentions.users.array.length > 0){
let userToMention = message.mentions.users.first();
message.channel.send("bonks "+userToMention.toString() + " https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
}
});
Hello again #Blissy89!
Yes, you can fix it by just using:
if(!userToMention) return message.say('Mention a user to run this command!')
Put it ABOVE message.channel.send
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.