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 this
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!
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(" ")))