[英]discord.js - How to randomly split messages between mentioned users?
我正在尝试制作系统,该系统将 dm 提到消息的用户和作者,每个消息都有一条随机消息*。 但我想知道是否有更简单的方法,然后使用if 。
*例子:
用户名作者:Test2
用户名提及[0]:Test4
用户名提及[1]:Test1
用户名提及[2]:Test3
(只是不会发生某人与其他人有相同的信息)
const userNameAuthor = message.author;
const userNameMentioned = message.mentions.users.map(u => u);
let replies1 = [
"Test1",
"Test2",
"Test3",
"Test4",
];
let replies2 = [
"Test1",
"Test2",
"Test3",
"Test4",
];
let replies3 = [
"Test1",
"Test2",
"Test3",
"Test4",
];
let replies4 = [
"Test1",
"Test2",
"Test3",
"Test4",
];
let result1 = Math.floor((Math.random() * replies1.length));
let result2 = Math.floor((Math.random() * replies2.length));
let result3 = Math.floor((Math.random() * replies3.length));
let result4 = Math.floor((Math.random() * replies4.length));
userNameAuthor.send(replies1[result1])
userNameMentioned[0].send(replies2[result2])
userNameMentioned[1].send(replies3[result3])
userNameMentioned[2].send(replies4[result4])
实现目标的一种更规范、更简单的方法是使用Array#splice()
方法,该方法删除或替换数组中预设数量的元素,并可选择用另一个元素替换它们(不必要的东西在此刻)。
在您的代码中,我们可以形成一个forEach()
迭代器来遍历提到的成员,向他们发送随机消息,并从我们的数组中删除相同的消息。
const mentioned = message.mentions.users
let replies = [
'test1',
'test2',
'test3',
'test4'
]
mentioned.forEach(user => {
const random = Math.floor(Math.random() * replies.length)
user.send(replies[random])
replies.splice(random, 1) // Removes the element from the array
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.