Discord.js bot looping using || or operator - javascript

I am having an issue when I use the or opperator || the bot will send 5 messages at once, and continue to loop. Without using the or operator it works fine. It will also accept some letters like c, or h to start the loop, which is odd.
This is the code it will print out onto the discord.
Hello #thehiddencheese! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact #thehiddencheese.
Hello #thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact #thehiddencheese.
Hello #thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact #thehiddencheese.
Hello #thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact #thehiddencheese.
Hello #thehiddencheese's test bot! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact #thehiddencheese.
Here is the code
client.on('message', (message) => {
let targetMember = message.member.user;
if (message.content === '!help' || '!command') {
message.channel.send(
`Hello ${targetMember}! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact <#248030367666274304>.`
);
}
});

The statement if (message.content === '!help' || '!command') { has two blocks: message.content === '!help' and '!command'. The || or operator says: "either of those things should be true".
So it ignores the first block because the second block will always be truthy.
What you're trying to do is this:
client.on('message', (message) => {
let targetMember = message.member.user;
if (message.content === '!help' || message.content === '!command') {
message.channel.send(
`Hello ${targetMember}! This bot is currently used for testing only. Features will be added in the future, however for more info, please contact <#248030367666274304>.`
);
}
});

Related

discord.js mention command not picking up users but picks up itself

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!

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