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!
Related
I am building a discord bot using slash commands with discord.js V14.7.1.
I have a command that sets up a listener for images from the command user. I want this command to stop collecting images when the same command is used again by the command user so only one instance of it is active at a time.
In discord.js, I have my command files separate from my command handler which I basically copied from the discord.js guide and am running commands via command.execute(). Currently, I am passing in the client as an argument for #execute and then within the commands separate file I am using the following code to shut down the image collector.
client.on(Events.InteractionCreate, async i => {
if (!i.isChatInputCommand()) return;
const command = client.commands.get(i.commandName);
const isSameCommand = command.data.name == interaction.commandName;
const isSameUser = i.user.id == interaction.user.id;
if (isSameCommand && isSameUser) {
imageCollector.stop();
}
});
After several people use the command I get the following warning: (node:20760) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 22 interactionCreate listeners added to [Client]. Use emitter.setMaxListeners() to increase limit. I think I can tell why this is occurring and presume it to be a problem if the bot is left running for a very long time, but I don't know how else to solve the problem or clean up the listeners.
I think it would be easier to not create a listener for every user.
Assuming the users are sending images in normal messages, you can put this logic in the messageCreate event listener.
let usersToCollect = [] //The slash command pushes and removes images to this array, you can use database later
client.on('messageCreate', async function (message) {
if (!message.attachements) return // Your logic for determining if its an image here
if (usersToCollect.includes(message.author.id) {
// save image
}
}
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!
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>.`
);
}
});
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.
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.