[英]How come my array isn't updating after a splice?
Here is my code这是我的代码
var reactions = ['👍','👎','🤔' ]; // Valid reactions for filter
var participants = []; // People attending the event
// Sending the embed back and then . . .
message.channel.send(eventEmbed)
.then(embedMessage => {
// Adding the reactions after the embed has been created
embedMessage.react("👍");
embedMessage.react("👎");
embedMessage.react("🤔");
// Reaction Collector to gather the users attending the event.
const filter = (reaction, user) => {
return !user.bot && reactions.includes(reaction.emoji.name);
};
rc = new Discord.ReactionCollector(embedMessage, filter);
rc.on('collect', (reaction, user) => {
if(reaction.emoji.name === "👍")
{
let new_user = true;
// Check if they are already on the list
for (let participant in participants)
{
if (user.id === participants[participant].id) // If user is found in the list, remove them and update.
{
console.log(participant);
console.log(participants);
//console.log(participants.splice(user,1)); // Remove user from the list
participants.splice(user,1);
console.log(participants);
reaction.users.remove(user); // Reset reaction count back to 1
// Update the embed
eventEmbed.fields.find(f => f.name === "Participants").value = participants; // Updating participants
embedMessage.edit(eventEmbed);
new_user = false;
break;
}
else // continue to loop through the whole list
{
continue;
}
}
// Blah blah blah . . .
I printed the splice line and it seems to be working, but for some reason its not updating?我打印了拼接线,它似乎在工作,但由于某种原因它没有更新? I am just curious as to why this is.
我只是好奇这是为什么。
Also, here is the output.此外,这里是输出。 In this case, a user object that is already in the participants list is found, and so the splice is executed.
在这种情况下,会找到已经在参与者列表中的用户对象,因此会执行拼接。
0 //Index 0
[ //Participants list
User {
id: '348298943014371338',
username: 'Caweb',
bot: false,
discriminator: '8740',
avatar: 'a_413ca01ea08214480c35aff45c54647a',
flags: UserFlags { bitfield: 256 },
lastMessageID: null,
lastMessageChannelID: null
}
]
[] // The console.log(participants.splice(user,1)) line. So it seems to look like its splicing successfully!
[ // But then it still shows the user here :(
User {
id: '348298943014371338',
username: 'Caweb',
bot: false,
discriminator: '8740',
avatar: 'a_413ca01ea08214480c35aff45c54647a',
flags: UserFlags { bitfield: 256 },
lastMessageID: null,
lastMessageChannelID: null
}
]
Array.prototype.splice()
actually returns an array of the elements they did splice, meaning your function is not working at all. Array.prototype.splice()
实际上返回他们所做的拼接元素的数组,这意味着你的功能不包括在所有工作。 This is probably because user
is a User
object instead of an index.这可能是因为
user
是一个User
对象而不是一个索引。
Syntax
句法
let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
If I'm reading your code correctly, you actually want the first argument to be participant
instead of user
, as participant
is the actual index you'd like to splice.如果我正确阅读您的代码,您实际上希望第一个参数是
participant
而不是user
,因为participant
是您想要拼接的实际索引。
participants.splice(participant, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.