Skip to content

Commit

Permalink
feat: channels are now deleted when a team is unlocked (#173)
Browse files Browse the repository at this point in the history
* feat: channels are now deleted when a team is unlocked

* fix: basically lint

* fix: wait wtf ?

---------

Co-authored-by: Teddy Roncin <[email protected]>
  • Loading branch information
TeddyRoncin and Teddy Roncin authored Oct 20, 2023
1 parent 7cc4007 commit 472fc2d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
30 changes: 16 additions & 14 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
7 changes: 4 additions & 3 deletions src/operations/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
};
Expand Down
8 changes: 8 additions & 0 deletions src/services/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export const createDiscordChannel = async (requestBody: DiscordCreateChannelRequ
return response.data;
};

export const deleteDiscordChannel = async (channelId: string) => {
const response = await bot.delete<DiscordChannel>(`channels/${channelId}`);

return response.data;
};

export const deleteDiscordRole = (roleId: string) => bot.delete(`guilds/${env.discord.server}/roles/${roleId}`);

/**
* Create a discord role
*/
Expand Down
19 changes: 17 additions & 2 deletions src/utils/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
addMemberRole,
fetchGuildMember,
removeMemberRole,
deleteDiscordChannel,
deleteDiscordRole,
} from '../services/discord';
import {
DiscordChannelPermission,
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
});
}
};

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 472fc2d

Please sign in to comment.