Skip to content

Commit

Permalink
Force cache refreshs
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanleminh committed Jun 17, 2023
1 parent 0f622d0 commit 2f037a5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand Down
4 changes: 2 additions & 2 deletions src/commands/endmatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
}
}
5 changes: 5 additions & 0 deletions src/commands/listteams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, GuildMember> = (interaction.guild.channels.cache
.get(lobbyVcId)!
.members as Collection<string, GuildMember>)
.filter(member => !member.user.bot)

const embeds = createTeamEmbeds(lobbyVcMembers, firstTeamRoleId, secondTeamRoleId, spectatorRoleId, interaction.guild, firstTeamVcId, secondTeamVcId, lobbyVcId)
await interaction.editReply({ embeds })
}
Expand Down
23 changes: 13 additions & 10 deletions src/commands/randomize.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -39,6 +39,7 @@ export const command: Command = {
const lastRoundSpectatorIds: string[] = await properties.guaranteedPlayersNextRoundIds.get(
interaction.guild.id
)

const lobbyVcMembers: Collection<string, GuildMember> = (interaction.guild.channels.cache
.get(lobbyVcId)!
.members as Collection<string, GuildMember>)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -154,13 +152,13 @@ function shuffle (array: GuildMember[]): GuildMember[] {
return result
}

export function createTeams (players: GuildMember[], firstTeamRoleId: string, secondTeamRoleId: string): Array<Promise<GuildMember>> {
export function createTeams (players: GuildMember[], firstTeamRoleId: string, secondTeamRoleId: string, members: GuildMemberManager): Array<Promise<GuildMember>> {
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<Promise<GuildMember>> = []
const promises: Array<Promise<any>> = []
const teamSize = Math.ceil(players.length / 2)

for (let i = 0; i < teamSize; i++) {
Expand All @@ -174,5 +172,10 @@ export function createTeams (players: GuildMember[], firstTeamRoleId: string, se
}
}

promises.push(
// Update cache with new roles
members.fetch()
)

return promises
}
4 changes: 4 additions & 0 deletions src/commands/startmatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, GuildMember>
Expand Down
10 changes: 7 additions & 3 deletions src/modules/functions.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -34,8 +34,8 @@ export function createEmbed (list: User[], title: string, color: HexColorString,
return embed
}

export function clearTeamRoles (roles: Collection<string, Role>, firstTeamRoleId: string, secondTeamRoleId: string): Array<Promise<GuildMember>> {
const promises: Array<Promise<GuildMember>> = []
export function clearTeamRoles (roles: Collection<string, Role>, firstTeamRoleId: string, secondTeamRoleId: string, members: GuildMemberManager): Array<Promise<GuildMember>> {
const promises: Array<Promise<any>> = []
if (firstTeamRoleId) {
const firstTeamRole = roles
.get(firstTeamRoleId)!
Expand Down Expand Up @@ -69,5 +69,9 @@ export function clearTeamRoles (roles: Collection<string, Role>, firstTeamRoleId
)
})
}
promises.push(
// Update cache with new roles
members.fetch()
)
return promises
}

0 comments on commit 2f037a5

Please sign in to comment.