From 448f0ce3bd1dfa57a9113c6765b7925d83395859 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Wed, 10 Nov 2021 00:06:28 +0100 Subject: [PATCH] Using room map instead of list, fixing user join registration. --- src/app.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app.js b/src/app.js index 728c0ba..7c98efd 100644 --- a/src/app.js +++ b/src/app.js @@ -105,7 +105,7 @@ function onRooms({ rooms, users }, bot) { logger.info(`Joined ${rooms.map((room) => room.name).join(', ')}`); /* eslint-disable no-param-reassign */ - bot.rooms = rooms; + bot.rooms = rooms.reduce((acc, room) => ({ ...acc, [room.id]: room }), {}); bot.users = { ...bot.users, ...users }; /* eslint-enable no-param-reassign */ @@ -120,22 +120,22 @@ function onRooms({ rooms, users }, bot) { /* eslint-disable no-param-reassign */ function onJoin(data, bot) { - if (bot.rooms[data.roomId] && !bot.rooms[data.roomId]?.includes(data.user.id)) { + if (bot.rooms[data.roomId] && !bot.rooms[data.roomId].userIds?.includes(data.user.id)) { bot.users[data.user.id] = data.user; - bot.rooms[data.roomId].push(data.user.id); + bot.rooms[data.roomId].userIds.push(data.user.id); } } function onLeave(data, bot) { if (bot.rooms[data.roomId]) { - bot.rooms[data.roomId].users = bot.rooms[data.roomId].users.filter((userId) => userId !== data.userId); + bot.rooms[data.roomId].userIds = bot.rooms[data.roomId].userIds.filter((userId) => userId !== data.userId); } } function onMessage(message, bot, games) { const [, command, subcommand] = message.body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || []; const user = bot.users[message.userId]; - const room = bot.rooms.find((roomX) => roomX.id === message.roomId); + const room = bot.rooms[message.roomId]; if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) { getLeaderboard(games[command], { user, room });