From 2f037a56f5e95b73d3e1402dd60f810cd3450956 Mon Sep 17 00:00:00 2001 From: Stefan Le-Minh Date: Sat, 17 Jun 2023 19:32:56 +0200 Subject: [PATCH] Force cache refreshs --- src/commands/clear.ts | 2 +- src/commands/endmatch.ts | 4 ++-- src/commands/listteams.ts | 5 +++++ src/commands/randomize.ts | 23 +++++++++++++---------- src/commands/startmatch.ts | 4 ++++ src/modules/functions.ts | 10 +++++++--- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/commands/clear.ts b/src/commands/clear.ts index 0b144c9..e16f8d6 100644 --- a/src/commands/clear.ts +++ b/src/commands/clear.ts @@ -48,7 +48,7 @@ export const command: Command = { } promises.concat( - clearTeamRoles(interaction.guild.roles.cache, firstTeamRoleId, secondTeamRoleId) + clearTeamRoles(interaction.guild.roles.cache, firstTeamRoleId, secondTeamRoleId, interaction.guild.members) ) await properties.guaranteedPlayersNextRoundIds.set(interaction.guild.id, []) diff --git a/src/commands/endmatch.ts b/src/commands/endmatch.ts index 114ba0c..1f1b2fc 100644 --- a/src/commands/endmatch.ts +++ b/src/commands/endmatch.ts @@ -55,8 +55,8 @@ export const command: Command = { }) } - await Promise.allSettled(clearTeamRoles(interaction.guild.roles.cache, firstTeamRoleId, secondTeamRoleId)) - await Promise.allSettled(promises) + await Promise.all(clearTeamRoles(interaction.guild.roles.cache, firstTeamRoleId, secondTeamRoleId, interaction.guild.members)) + await Promise.all(promises) await interaction.editReply('GG!') } } diff --git a/src/commands/listteams.ts b/src/commands/listteams.ts index 24d730d..2f1c1de 100644 --- a/src/commands/listteams.ts +++ b/src/commands/listteams.ts @@ -30,10 +30,15 @@ export const command: Command = { properties.firstTeamVcs.get(interaction.guild.id), properties.secondTeamVcs.get(interaction.guild.id) ]) + + // Update cache with new roles + await interaction.guild.members.fetch() + const lobbyVcMembers: Collection = (interaction.guild.channels.cache .get(lobbyVcId)! .members as Collection) .filter(member => !member.user.bot) + const embeds = createTeamEmbeds(lobbyVcMembers, firstTeamRoleId, secondTeamRoleId, spectatorRoleId, interaction.guild, firstTeamVcId, secondTeamVcId, lobbyVcId) await interaction.editReply({ embeds }) } diff --git a/src/commands/randomize.ts b/src/commands/randomize.ts index 5de521e..8bca9ab 100644 --- a/src/commands/randomize.ts +++ b/src/commands/randomize.ts @@ -1,7 +1,7 @@ import { type Properties } from '../types/properties.js' import path from 'path' import { logging } from '../logging/winston.js' -import { type CommandInteraction, type Collection, type GuildMember, type Role, type Guild } from 'discord.js' +import { type CommandInteraction, type Collection, type GuildMember, type Role, type Guild, type GuildMemberManager } from 'discord.js' import { SlashCommandBuilder } from '@discordjs/builders' import { clearTeamRoles } from '../modules/functions.js' import { fileURLToPath } from 'url' @@ -29,8 +29,8 @@ export const command: Command = { ]) const roles = await interaction.guild.roles.fetch() - await Promise.allSettled( - clearTeamRoles(roles, firstTeamRoleId, secondTeamRoleId) + await Promise.all( + clearTeamRoles(roles, firstTeamRoleId, secondTeamRoleId, interaction.guild.members) ) const lobbyVcId = await properties.lobbies.get(interaction.guild.id) @@ -39,6 +39,7 @@ export const command: Command = { const lastRoundSpectatorIds: string[] = await properties.guaranteedPlayersNextRoundIds.get( interaction.guild.id ) + const lobbyVcMembers: Collection = (interaction.guild.channels.cache .get(lobbyVcId)! .members as Collection) @@ -68,13 +69,10 @@ export const command: Command = { ) } const randomizedPlayerPool = shuffle(playerPool) - await Promise.allSettled( - createTeams(randomizedPlayerPool, firstTeamRoleId, secondTeamRoleId) + await Promise.all( + createTeams(randomizedPlayerPool, firstTeamRoleId, secondTeamRoleId, interaction.guild.members) ) - // Update cache with new roles - await interaction.guild.members.fetch() - logger.info('==========randomize end==========') await listTeams.command.execute(interaction, properties) @@ -154,13 +152,13 @@ function shuffle (array: GuildMember[]): GuildMember[] { return result } -export function createTeams (players: GuildMember[], firstTeamRoleId: string, secondTeamRoleId: string): Array> { +export function createTeams (players: GuildMember[], firstTeamRoleId: string, secondTeamRoleId: string, members: GuildMemberManager): Array> { logger.info(`Creating teams with parameters: ${players.map(member => member.user.username)}, ${firstTeamRoleId}, ${secondTeamRoleId}`) if (players.length > MAX_AMOUNT_OF_PLAYERS) { logger.warn(`More players in pool of size ${players.length} than allowed size of ${MAX_AMOUNT_OF_PLAYERS}! Any players beyond that will be cut off.`) players = players.slice(0, MAX_AMOUNT_OF_PLAYERS) } - const promises: Array> = [] + const promises: Array> = [] const teamSize = Math.ceil(players.length / 2) for (let i = 0; i < teamSize; i++) { @@ -174,5 +172,10 @@ export function createTeams (players: GuildMember[], firstTeamRoleId: string, se } } + promises.push( + // Update cache with new roles + members.fetch() + ) + return promises } diff --git a/src/commands/startmatch.ts b/src/commands/startmatch.ts index 41d986c..cb4ebb4 100644 --- a/src/commands/startmatch.ts +++ b/src/commands/startmatch.ts @@ -31,6 +31,10 @@ export const command: Command = { const firstTeamRoleId = await properties.firstTeamRoleIds.get( interaction.guild.id ) + + // Update cache with new roles + await interaction.guild.members.fetch() + const lobbyVcMembers = interaction.guild.channels.cache .get(lobbyVcId)! .members as Collection diff --git a/src/modules/functions.ts b/src/modules/functions.ts index 6a176fa..37a64b5 100644 --- a/src/modules/functions.ts +++ b/src/modules/functions.ts @@ -1,4 +1,4 @@ -import Discord, { type Collection, type Role, type Guild, type GuildMember, type HexColorString, type User } from 'discord.js' +import Discord, { type Collection, type Role, type Guild, type GuildMember, type HexColorString, type User, type GuildMemberManager } from 'discord.js' import path from 'path' import { logging } from '../logging/winston.js' import { fileURLToPath } from 'url' @@ -34,8 +34,8 @@ export function createEmbed (list: User[], title: string, color: HexColorString, return embed } -export function clearTeamRoles (roles: Collection, firstTeamRoleId: string, secondTeamRoleId: string): Array> { - const promises: Array> = [] +export function clearTeamRoles (roles: Collection, firstTeamRoleId: string, secondTeamRoleId: string, members: GuildMemberManager): Array> { + const promises: Array> = [] if (firstTeamRoleId) { const firstTeamRole = roles .get(firstTeamRoleId)! @@ -69,5 +69,9 @@ export function clearTeamRoles (roles: Collection, firstTeamRoleId ) }) } + promises.push( + // Update cache with new roles + members.fetch() + ) return promises }