How to disable sending messages to people with closed DM - bots

So I don't want to send messages to people with closed DM's.
Current Code:
message.guild.members.cache.forEach(member => {
if (member.id !== bot.user.id && !member.user.bot);
member.send(message);
});

Okay, you can't check if someone has DMs open, at least at this moment. What you can do is send the message and catch the error if someone has DMs closed.
Also, you put a semicolon right after the if, which basically voids the purpose of the if statement. Remove the semicolon and it should not send to any bots.
For example:
message.guild.members.cache.forEach(member => {
if (member.id !== bot.user.id && !member.user.bot) // remove the semicolon here
member.send(message).catch(() => {}); // catch an error if the user has their DMs closed
});
Hope this helps.

Related

How to ignore msg.delete() if not found discord.js

execute(client, message) {
message.channel.send(`Last heartbeat calculated ${ms(Date.now() - client.ws.shards.first().lastPingTimestamp, { long: true })} ago **${client.ws.ping}ms** 🛰️`).then(msg => {
setTimeout(() => msg.delete(), client.config.app.dtime)
});
Hi there, I was working on my discord bot and ran into a scenario like this, the code causing the problem is shown above. When this function runs, it sends the current ping of the bot to the message channel. and automatically deletes the msg after some time, but in case some person with admin privileges deletes the message manually. My bot will crash when it tries to delete so to avoid that how should I fix my code. Thanks in advance.
You can handle the promise rejection and void it as such:
msg.delete().catch(() => null);
Even when the promise rejects, no errors will appear nor will the process crash
Try going to your index file where you start the bot, and do this line
process.on("uncaughtException", (error) => {
console.log(error)
})
It will console log an error, and won't crash, and also every time your bot is about to crash, it will just console it :)

If a message is from 3 specific users , ignore them

I want to know is it possible to stop specific users from using the bot, e.g. user b can not use any commands if they are in the blacklist of the config.json . thank you
Here is something you could use
const bannedUsers = ['userid1', 'userid2', 'userid3'];
client.on('message', msg => {
if(bannedUsers.includes(msg.author.id)) return;
//command execution code here
})
You will have to put this in all your client.on('message') listeners.
You don't show any code, but you could try something like this where you process commands..
//when bot receives a message
if (message.author.id === 'blacklisted ids'){
return;
} else {
//process commands
}

I'd like to make an automated response with more than one line [duplicate]

This question already has answers here:
How do I do a multi-line string in node.js?
(9 answers)
Closed 2 years ago.
So I'm doing an automated response command but I need to to respond with more than one line.
(I use node.js/discord.js? and I'm coding a Discord bot)
Can somebody let me know what I would need to do make it more than one line? The following is what I have at the moment.
client.on('message', msg => {
if (msg.content === '<response trigger>') {
msg.channel.send('<Where the response would go>');
}
});
Creating multiline response with discord.js is completely unrelated to the library itself. You simply have to specify a string with a line break.
One possibility to start a new line is by inserting \n into your string. There are also other possibilities to do this, just take a look at this answer.
In your case a multiline response could look like this:
client.on('message', msg => {
if (msg.content === '<response trigger>') {
msg.channel.send('line1\nline2');
}
});

Trying to dedicate a channel to only sending pictures and reddit links

Update: Problem with double condition solved. The Code Still Does Not Function.
client.on("message", message => {
if (message.author.bot) return;
if (message.author === client) return;
if (message.channel.id === "605839623372931093") {
if (message.attachments.size > 0) {
if (!message.attachments.every(attachIsImage) || !message.content.includes("https://www.reddit.com/")) {
(bulkDelete(message))
(message.channel.send("This channel Only Supports Picture Messages or Reddit Links!"));
}}}});
It is worth noting that my code doesn't give out any errors, it just doesnt do anything.
If you would like to know more, just ask me and I'm willing to answer.
You can use the OR operator (||) to check if the message doesn't fullfil either of your conditions. Your if statement would then look like this:
if (!message.attachments.every(attachIsImage) || !message.content.includes("https://www.reddit.com/")) {
// Delete the message and let the user know about the channel rules
}

Bot replying multiple times when triggered by reaction

Right now I am making a discord bot (in version 12 of discord.js).
It works like this:
Someone sends and insult
If the insult is included in a list (stored in insultes.json) the bot send a message and adds a reaction
If we add the same reaction the bot sends another message
The problem I'm facing is that if I keep adding the reaction the bot keeps replying 2, 3, 4 times and so on: every time (n) I check the reaction it replies with n+1 messages.
This is the code:
bot.on('message', message => {
const insulte = require('./insultes.json');
for (let p = 0; p < insulte.length; p++) {
// Check if the insult is in the list and make sure it's not from the bot itself
if (message.content.toLowerCase().includes(insulte[p]) && message.author.id !== "711337757435363468") {
message.channel.send("First message").then(messageReaction => {
messageReaction.react("➡️");
});
bot.on('messageReactionAdd', (reaction, user) => {
if (reaction.emoji.name === "➡️" && user.id !== "711337757435363468") {
message.channel.send("Additional message");
}
});
}
}
});
I think your problem comes from the fact that you're using bot.on('messageReactionAdd', ...: that means that every time you run that part of the code the code will add another listener that adds up to the ones that you used before.
Also, that code will trigger when a reaction to any message is added, not just the one you sent.
From your question, I don't understand if the bot is supposed to reply with one message every time you hit the reaction on that message, or just do it once and then ignore the message. I'll assume it's the latter.
Here's my take on this:
bot.on('message', message => {
const insults = require('./insultes.json')
if (insults.some(i => message.content.toLowerCase().includes(i)) && message.author.id !== "711337757435363468") {
message.channel.send("First message").then(myMsg=> {
myMsg.react("➡️");
let reactionFilter = (reaction, user) => reaction.emoji.name === '➡️' && user.id !== "711337757435363468"
myMsg.awaitReactions(reactionFilter, { max: 1 }).then(() => {
myMsg.channel.send('Additional message')
})
});
}
})
As you can see, I'm using Array.some() to check whether any insult is in the message, instead of a for loop. I'm using Message.awaitReactions() to fetch the first reaction and respond to that: after that, the bot will just ignore any other reaction on that message, but will still work on others.
Feel free to let me know if something is not clear or doesn't work :)

Resources