Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!