I am trying to make system, that will dm mentioned users and author of the message with one of the random messages for each*. But I wonder if there is easier way then make it with if .
*Example:
UserNameAuthor: Test2
userNameMentioned[0]: Test4
userNameMentioned[1]: Test1
userNameMentioned[2]: Test3
(It just doesn't happen that someone has the same message as someone else)
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])
A much more regulated and simplistic way to achieve your goal is by using the Array#splice()
method that removes or replaces a preset amount of elements in your array, with the additional option of replacing them with another element (Something that is not necessary at this point).
In your code, we can form a forEach()
iterator to iterate through the mentioned members, send them a random message, and have the same message deleted from our array.
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
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.