I am trying to make a DM command in discord.js that dms a mentioned user. i wanted to allow support so you could either mention a user to dm, or just use their id. this is my code:
let nodmperm = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription("ERROR: Missing permissions: `MANAGE_MESSAGES`");
// if (!message.member.hasPermission("MANAGE_MESSAGES"))
// return message.channel.send(nodmperm);
let nospecdm = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription("ERROR: No valid user was specified to DM");
if (!args[1]) return message.channel.send(nospecdm);
let dUser =
message.guild.member(message.mentions.users.first()) ||
bot.users.cache.get(args[0]);
let nouserfound = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription(`ERROR: No user found by the name of ${args[1]}`);
if (!dUser) return message.channel.send(nouserfound);
let dMessage = args.join(" ").slice(25);
let nomsgsup = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription("ERROR: No message was supplied");
if (dMessage.length < 1) return message.channel.send(nomsgsup);
let dmsoff = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription(
`ERROR: Unable to DM ${dUser} because they either have DMs off, or they have blocked Outlet`
);
let sentsucc = new Discord.MessageEmbed()
.setColor("#8f82ff")
.setTitle("Message was successfully sent")
.addField("Target: ", `${dUser} (ID: ${dUser.id}) (Discriminator: ${message.mentions.users.first().discriminator})`)
.addField("Message Content: ", dMessage)
.setFooter(message.createdAt)
.setTimestamp();
let userDM = new Discord.MessageEmbed()
.setTitle("You received a DM")
.setColor("#8f82ff")
.addField("Sender: ", `${message.author} (ID: ${message.author.id})`)
.addField("Server sent from: ", message.guild.name)
.addField("Server Channel: ", "#" + message.channel.name)
.addField("Message Content: ", dMessage)
.addField("Message URL: ", message.url)
.setFooter(`${message.createdAt}`)
.setTimestamp();
dUser
.send(userDM)
.then(() => message.channel.send(sentsucc))
.catch(err => {
message.channel
.send(dmsoff)
.catch(err =>
console.error(`Error while sending error message...
`, err)
);
});
I cant seem to get it to work tho, i cannot get the args to fluctuate properly, im not too sure how i could fix this so you could either mention a user to dm, or use their id
question from:
https://stackoverflow.com/questions/66056954/discord-js-id-support-for-dm-command 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…