[英]Command to DM the mentioned users
Here's my code to DM the mentioned members.这是我给提到的成员的 DM 代码。
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "dm",
category: "moderation",
usage: "DM <@mention> <message>",
description: "DM server members!",
run: async (client, message, args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) {
return message.channel.send(
"Access restricted!"
);
}
const targets = message.mentions.members;
if (!targets) {
return message.channel.send(
"Please Mention the person to who you want to DM!"
);
}
if (message.mentions.users.bot) {
return message.channel.send("You cannot DM bots");
}
const reason = args.slice(0).join(" ");
if (!reason) {
return message.channel.send(
"Place provide the message to send!"
);
}
message.channel.send(`DM sent succesfully!`);
let embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`Message from ${message.guild.name}`)
.setDescription(`${reason}`)
.setThumbnail('https://i.ibb.co/PCnBZ8w/IMG-20210205-191207.jpg')
.setFooter(`Sent by ${message.member.user.tag}`)
targets.forEach(target => target.send(embed));
message.delete();
}
};
It is used like so: prefixdm the message @mention1 @mention2...
.它的使用方式如下:
prefixdm the message @mention1 @mention2...
。 But in the DM received by the users they also get the mentions of the command due my declaration of reason
.但是在用户收到的 DM 中,由于我的
reason
声明,他们也提到了该命令。 How can I fix it so that they don't see the mentions in the message?我该如何解决它,以便他们看不到消息中的提及?
You can clean up the message using regex.您可以使用正则表达式清理消息。
MessageMentions
has a USERS_PATTERN
property that contains the regular expression that matches the user mentions (like <@82043902196179341>
). MessageMentions
有一个USERS_PATTERN
属性,其中包含与用户提及匹配的正则表达式(如<@82043902196179341>
)。 You can use it with JavaScript's .replaceAll()
method to replace all occurrences of the mention with an empty string:您可以将它与 JavaScript 的
.replaceAll()
方法一起使用,以将所有出现的提及替换为空字符串:
const reason = args
.slice(0)
.join(' ')
.replaceAll(MessageMentions.USERS_PATTERN, '')
.trim();
Don't forget to import MessageMentions
from discord.js
:不要忘记从
discord.js
导入MessageMentions
:
const { MessageEmbed, MessageMentions } = require("discord.js");
Update: You can also use .replace()
instead of .replaceAll()
as the regex globally matches user mentions.更新:您也可以使用
.replace()
而不是.replaceAll()
因为正则表达式全局匹配用户提及。 Source . 来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.