I know you can send a DM like this:
message.author.send("Go to example.com for help");
But some people have the setting that allows other server members to DM them off:
How do I find out if the DM actually sent or not? I know this is possible in the Discord API, since other bots do it.
If the user has that option, or isn't DMable, it will throw an error, namely:
DiscordAPIError: Cannot send messages to this user
Now, we can catch that error and run a command based on it, say replying in the channel that the user can't be DMed.
user.send(...).catch(async err => message.reply("I can't DM this user"));
// In the above, async is useless, but you can actually do what you want with it.
To run more than a single line of command, use a promise based catch.
user.send(...).catch(async err => {
console.log(err);
message.reply("I can't DM this user");
});
If a user has that option enabled, DMing them will return an error, so you'll be able to use a .catch() statement:
user.send().catch(() => console.log('Could not DM this user'));
You can get error notices aswell as information about the error using .catch()
In this example I log the error type and description
member.send(...).catch(error => {
console.error(`${error.name} :\n${error}`)
message.channel.send('There was an error when trying to DM this member')
})
Related
I am trying to implement a discord chatbot into my own bot, isn't good at handling some characters and stuff like the quotes of discord (type > and space to know that I mean). It often crashes becauses it thinks the message is empty. I wanted to use a try catch phrase so it won't crash. The complete function is asyncronous and I think that's why it isn't working how I want it to do. It still crashes without logging the thrown error.
client.on("message", async message => {
if(message.channel.id === "1006483461747527772" && !message.author.bot){
if(containsLetters(message.content)) {
//console.log(message.content.length)
try{
let reply = await chat.chat(message.content)
client.channels.cache.get("1006483461747527772").send(reply)
} catch(err){
client.channels.cache.get("1006483461747527772").send(err)
}
}
}
})
#Robotgozoooom's answer is correct that to send an error message to Discord you need to use error.message, but there's another problem: when Discord throws the Cannot send empty message error it is in an async function, so the try/catch doesn't actually catch the error. You need to add a .catch() function to the end of your async function instead of using a try/catch at all:
if (containsLetters(message.content)) {
let reply = await chat.chat(message.content);
let channel = client.channels.cache.get("1006483461747527772");
channel.send(reply).catch((err) => channel.send(err.message));
}
I think the problem that you are running into here is that the object err is not a String. Try appending .message to err so then it reads err.message. If that doesn't work (or even if it does), I would recommend against sending the error message in discord since that presents more variables to interfere with debugging (what if something was wrong with the client object or with the channel id string?). Instead, I normally use console.error(err). Hope this helps!
I'm currently coding a discord.js bot and I made this join command so the bot can join my voice channel
module.exports.run = async (client, message, args) => {
let membervc = message.member.voice.channel;
let botvc = message.guild.me.voice.channel;
if(!membervc) return message.send('You need to join a voice channel first.');
if(botvc){
if(!(botvc.members.size - 1)) return message.reply(`I am already on ${botvc}`);
if(membervc.id == botvc.id) return message.reply('We are already on the same voice channel.');
};
membervc.join();
}
The problem is that I have no idea how to make it so that if the last function gets an error or doesn't work at all It can send an error message to the user like
#User, I could not join the channel, "THE ERROR" :/ I don't want my bot to crash and having to run it again just because of one small detail. Is there a way to fix it? It would help me a lot! Thanks in advance!
I would not let the user know about the error, because it might just confuse them, but you can do try catch to check if it goes through and if not message is sent.
try {
membervc.join();
} catch(error){
console.log(error);
message.reply(`Something went wrong while joining voice channel`);
}
I am trying to implement for my discord server and my discord bot a possibility to send a PM message to a lot of users.
I created something like this:
client.on("message", message => {
mentiondm = message.mentions.users.first();
mentionMessage = message.content.slice(22)
mentiondm.send(mentionMessage);
console.log('Message Sent!')
})
It's working for first mentioned user, message is sending but there are several problems:
after sending first message bot is crashing down with this error Cannot read property 'send' of undefined
I tried to change message.mentions.users to message.mentions.roles but then message is not sending.
There is a way to do my functionality better or at least upgrade this what I have here to working properly?
It's because it tries to get the first mentioned user from the bot's message too. Check if the message author is a bot and if it is, don't send the message.
Also, try to check if there is someone mentioned and if not, simply return.
client.on('message', (message) => {
if (message.author.bot) return;
let mentionDM = message.mentions.users.first();
if (!mentionDM)
return console.log('No mentioned user');
let mentionMessage = message.content.slice(22);
mentionDM.send(mentionMessage);
console.log('Message Sent!');
});
Im trying to make a bot and have it have a ban command. without a reason, the ban command works great. But once I give it a reason, it just stops working and gives me this error:
{"title":"Go Vote for the Server!","description":"I've just finished posting the server to top.gg, one of the largest server listing sites. Please go vote for the server [here](https://top.gg/servers/754062115123232938/vote) so that more people can learn about it. It would really help the server grow. ","color":10426392,"footer":{"text":"© The Red Door, 2020"}}
If you have any idea how to fix this it would be greatly appreciated. Here is my current code for banning users:
if (message.member.roles.cache.some(role => role.name === config.modRole)) {
// Looks for the mentioned user
const user = message.mentions.users.first();
if(user){
// Checks if the user mentioned is in the server
const member = message.guild.member(user);
if(member){
// Bans the user
member.ban(config.banMessage).then(() =>{
message.reply(`sucessfuly banned ${user.tag}.`);
console.log(`Sucsessfuly banned ${user.tag} in ${message.guild}`)
}).catch(error =>{
// Catches any errors
message.reply(`I encountered an error when trying to ban ${user.tag}`);
console.log(`Error banning ${user.tag} in ${message.guild}`);
console.error(error);
})
} else {
message.reply(`That user isn\'t in the server.`)
}
} else {
message.reply(`you need to specify a valid user to ban!`)
}
} else {
message.reply('you do not have permission to use this command!')
console.log(`${message.author} tried to use the ban command in ${message.guild} but did not have sufficient permissions`)
}
},
Found my problem. The ban command has a different method of using a reason compared to the kick command. For the kick command, you can simply use message.author.kick("reason") as said here. But for the ban command you use message.author.ban({reason: "reason"}) as said here.
I have if (!message.guild.me.hasPermission("SEND_MESSAGES")) return; in my code but when I disable the bot's permission to Send Messages it gives me an error, (node:2504) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions. How could I possibly solve this issue so I won't get the error when my bot cannot send messages into a chat.
Do this before the message event
Try this:
if (message.author.bot) {
return;
} else {
//do soemthing if user is not a bot
}
You could possibly use a try catch block to catch the exception and then just don't print the error.
like :
try{
if (!message.guild.me.hasPermission("SEND_MESSAGES")) return;
}catch{}
It might be because the permission required isn't SEND_MESSAGES.
For example, if you are coding a ban command, you would also need to check if the bot has the BAN_MEMBERS permissions.
If it was an add-role command, you would have to check if the bot highest role is higher than the role it needs to assign.