Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
how to make dm command with role ? / Discord js V12

i wonder how can i make the dm command but with a role, so I made this code,

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(!args[0]) return message.channel.send("who do you want to send a message to?")
        const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0]);
        if(!user) return message.channel.send("no user found?")
        const reason = args.slice(1).join(" ")
        if(!reason) return message.channel.send("what's the message?");
        try {
            await user.send(reason);
            return message.channel.send("done");

        } catch {
            return message.channel.send("i cant send the dm");
        }



        
       }   
}

okay so i tried this code but the bot says "i cant send the dm" which means the bot has an error

and i tries this code "https://stackoverflow.com/a/61618962/16849847" the code is:

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 (!args[0]) return message.reply('you need to provide a role')
if (!args[1]) return message.reply('you need to provide a message')
const role = message.mentions.roles.first()
message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args[1]))

        
       }   
}

and yes this code works but only sent 1 sentence, the next sentence was not sent, like thisenter image description here

enter image description here

I hope u guys can help me, it's up to u, u want to fix the first code or second code thank you, guys!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issues is to do with the way you are sending the message

You are sending the message by sending args[1], meaning you will only send the first word.

To fix this do as you sent the message content in the first code snippet:

// Replace args[1] with args.slice(1).join(" ")
message.guild.roles.cache.get(role.id).members.forEach(member => member.send(args.slice(1).join(" ")))
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda