Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
How to ignore the error in Discord.js V12

I made a command for DM the role.
You can see my code here:

const { Client, Message, MessageEmbed } = require('discord.js');
// const { join } = require('path');

module.exports = {
    name: 'dm-role',
    /** 
     * @param {Client} client 
     * @param {Message} message 
     * @param {String[]} args 
     */
    run: async(client, message, args) => {
        if (!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("admin only lol")

        if (!args[0]) return message.channel.send("where's the role?")
        const role = message.mentions.roles.first()

        message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args.slice(1).join(" ")))

        if(!role) return message.channel.send("where's the role?")

        const reason = args.slice(1).join(" ")
        if(!reason) return message.channel.send("the message ?");

        try {
            await role.send(reason);
            return message.channel.send("done");
        } catch {
            return message.channel.send("failed");
        }
    }   
}

Someone in the role has blocked my bot, so I got an error like this

throw new DiscordAPIError(request.path, data, request.method, res.status);
            ^

DiscordAPIError: Cannot send messages to this user

I just want to know how I can get past the error, and the bot still keeps sending DMs to people who haven't blocked the bot.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the .catch(...) method on the .send(...) function.

Example:

role.send(reason).catch((error) => console.log(error));

Full Example:

run: async(client, message, args) => {
    if (!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("admin only lol")

    if (!args[0]) return message.channel.send("where's the role?")
    const role = message.mentions.roles.first()

    message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args.slice(1).join(" ")))

    if (!role) return message.channel.send("where's the role?")

    const reason = args.slice(1).join(" ")
    if (!reason) return message.channel.send("the message ?");

    try {
        // Attempt to send the message, if failed log the error and continue
        await role.send(reason).catch(error => console.log(error))
        return message.channel.send("done");
    } catch {
        return message.channel.send("failed");
    }
}   
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda