这是我的第一个 Discord 机器人,我陷入了困境。 我希望这个机器人能够选出测验的获胜者。
所以,我试图从随机用户那里获取所有具有正确答案的消息。 但是,以下代码不会选择具有正确答案的用户。
你能告诉我该怎么做吗?
const chan = message.channel;
const answer = 'test';
//Select a random user
const user = message.channel.members.random();
const userid = user.id
//fetch messages from the user and contains answer
chan.fetchMessages().then(messages => {
const usermsgs = messages.filter(
m => m.author.id === `${user.id}` &&
m.content === answer);
//check if the answer is in the messages
if(usermsgs){
message.reply('winner is' + `${user.user}`);
}else{
message.reply('No Winner');
}
}).catch(console.error);
最佳答案
我不熟悉discord.js,但是稍微阅读了一下他们的文档就发现了这一点。
您想要随机化正确回答问题的用户,而不是随机化整个 channel 的用户群。由于discord.js为过滤器函数返回一个Collection对象,因此您可以在那里使用它的随机函数。
chan.fetchMessages().then(messages => {
const usermsgs = messages.filter(
m => m.content === answer);
if(usermsgs){
const randomCorrectUser = usermsgs.random().author;
message.reply('winner is' + `${randomCorrectUser.username}`);
}else{
message.reply('No Winner');
}
}
关于javascript - 不和谐js : Selecting a random user from users who posted right answer in a channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675098/