diff --git a/schema.prisma b/schema.prisma index 4bdd288c..80d424e5 100644 --- a/schema.prisma +++ b/schema.prisma @@ -73,20 +73,22 @@ model Setting { } model Team { - id String @id - name String - tournamentId String - captainId String @unique - lockedAt DateTime? - createdAt DateTime @default(now()) - updatedAt DateTime @default(now()) @updatedAt - discordRoleId String? - pokemonPlayerId String? - captain User @relation(fields: [captainId], references: [id]) - tournament Tournament @relation(fields: [tournamentId], references: [id]) - askingUsers User[] @relation("teamAskingUsers") - users User[] @relation("teamUsers") - enteredQueueAt DateTime? + id String @id + name String + tournamentId String + captainId String @unique + lockedAt DateTime? + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) @updatedAt + discordRoleId String? + discordTextChannelId String? + discordVoiceChannelId String? + pokemonPlayerId String? + captain User @relation(fields: [captainId], references: [id]) + tournament Tournament @relation(fields: [tournamentId], references: [id]) + askingUsers User[] @relation("teamAskingUsers") + users User[] @relation("teamUsers") + enteredQueueAt DateTime? @@unique([name, tournamentId]) @@index([id]) diff --git a/src/operations/team.ts b/src/operations/team.ts index 537b2509..ce097bb8 100644 --- a/src/operations/team.ts +++ b/src/operations/team.ts @@ -12,7 +12,7 @@ import { } from '../types'; import nanoid from '../utils/nanoid'; import { formatUser, userInclusions } from './user'; -import { sendDiscordTeamLockout, sendDiscordTeamUnlock, setupDiscordTeam } from '../utils/discord'; +import { deleteDiscordTeam, sendDiscordTeamLockout, sendDiscordTeamUnlock, setupDiscordTeam } from '../utils/discord'; import { fetchTournament } from './tournament'; const teamInclusions = { @@ -281,7 +281,6 @@ export const unlockTeam = async (teamId: string) => { const updatedTeam = await fetchTeam(teamId); const tournament = await fetchTournament(updatedTeam.tournamentId); - // We freed a place, so there is at least one place left // (except if the team was already in the queue, but then we want to skip the condition, so that's fine) if (tournament.placesLeft === 1) { @@ -313,7 +312,9 @@ export const unlockTeam = async (teamId: string) => { } } - await sendDiscordTeamUnlock(updatedTeam, tournament); + // TODO : understand why we can't put awaits here + deleteDiscordTeam(updatedTeam); + sendDiscordTeamUnlock(updatedTeam, tournament); return updatedTeam; }; diff --git a/src/services/discord.ts b/src/services/discord.ts index c0f4a169..e18ce443 100644 --- a/src/services/discord.ts +++ b/src/services/discord.ts @@ -110,6 +110,14 @@ export const createDiscordChannel = async (requestBody: DiscordCreateChannelRequ return response.data; }; +export const deleteDiscordChannel = async (channelId: string) => { + const response = await bot.delete(`channels/${channelId}`); + + return response.data; +}; + +export const deleteDiscordRole = (roleId: string) => bot.delete(`guilds/${env.discord.server}/roles/${roleId}`); + /** * Create a discord role */ diff --git a/src/utils/discord.ts b/src/utils/discord.ts index 752eb560..6af8da43 100644 --- a/src/utils/discord.ts +++ b/src/utils/discord.ts @@ -10,6 +10,8 @@ import { addMemberRole, fetchGuildMember, removeMemberRole, + deleteDiscordChannel, + deleteDiscordRole, } from '../services/discord'; import { DiscordChannelPermission, @@ -73,7 +75,7 @@ export const setupDiscordTeam = async (team: Team, tournament: Tournament) => { logger.debug(`Create discord channels for ${team.name}`); // Create the channels and update in the database the role. - await Promise.all([ + const [textChannel, voiceChannel] = await Promise.all([ createDiscordTeamChannel( team.name, DiscordChannelType.GUILD_TEXT, @@ -88,8 +90,11 @@ export const setupDiscordTeam = async (team: Team, tournament: Tournament) => { role, tournament.discordVocalCategoryId, ), - database.team.update({ data: { discordRoleId: role.id }, where: { id: team.id } }), ]); + database.team.update({ + data: { discordRoleId: role.id, discordTextChannelId: textChannel.id, discordVoiceChannelId: voiceChannel.id }, + where: { id: team.id }, + }); } }; @@ -174,6 +179,16 @@ export const removeDiscordRoles = async (fromUser: User) => { } }; +export const deleteDiscordTeam = async (team: Team) => { + if (!env.discord.token) { + logger.warn('Discord token missing. It will skip discord calls'); + return; + } + await deleteDiscordRole(team.discordRoleId); + await deleteDiscordChannel(team.discordTextChannelId); + await deleteDiscordChannel(team.discordVoiceChannelId); +}; + export const getWebhookEnvFromString = (name: string) => { let envValue = env.discord.webhooks.channel_other; switch (name) {