問題描述
有沒有辦法讓命令使用 discord.js 向 discord 組的所有成員發送私人消息?
Is there any way to make a command send a private message to all members of the discord group using discord.js?
示例:/private TEST
此消息將通過私人聊天而不是頻道聊天發送給群組中的每個人.
This message is sent to everyone in the group in private chat instead of channel chat.
推薦答案
你可以遍歷 Guild.members
.
當您收到以 /private
開頭的消息時,您會使用 Guild.members.forEach()
.
這是一個簡單的例子:
You can iterate through Guild.members
.
When you receive a message that starts with /private
, you take the rest and send it to every member of the guild by using Guild.members.forEach()
.
Here's a quick example:
client.on('message', msg => {
if (msg.guild && msg.content.startsWith('/private')) {
let text = msg.content.slice('/private'.length); // cuts off the /private part
msg.guild.members.forEach(member => {
if (member.id != client.user.id && !member.user.bot) member.send(text);
});
}
});
這只是一個基本的實現,您顯然可以在命令檢查中使用這個概念,或者通過添加額外的文本等來修改它.
This is just a basic implementation, you can obviously use this concept with your command checks or modify that by adding additional text and so on.
希望這可以為您解決問題,如果您有任何其他問題,請告訴我:)
Hope this solves the problem for you, let me know if you have any further questions :)
這篇關于是否有向群組的所有成員發送私人消息的命令?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!