-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 feat: Discord roles integration (#373)
- Loading branch information
Showing
18 changed files
with
635 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/api/prisma/migrations/20220207174037_/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- AlterTable | ||
ALTER TABLE "cad" ADD COLUMN "discordRolesId" TEXT; | ||
|
||
-- CreateTable | ||
CREATE TABLE "DiscordRoles" ( | ||
"id" TEXT NOT NULL, | ||
"guildId" TEXT NOT NULL, | ||
"leoRoleId" TEXT, | ||
"leoSupervisorRoleId" TEXT, | ||
"emsFdRoleId" TEXT, | ||
"dispatchRoleId" TEXT, | ||
"towRoleId" TEXT, | ||
|
||
CONSTRAINT "DiscordRoles_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "DiscordRole" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"discordRolesId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "DiscordRole_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "DiscordRole_id_key" ON "DiscordRole"("id"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "cad" ADD CONSTRAINT "cad_discordRolesId_fkey" FOREIGN KEY ("discordRolesId") REFERENCES "DiscordRoles"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRoles" ADD CONSTRAINT "DiscordRoles_leoRoleId_fkey" FOREIGN KEY ("leoRoleId") REFERENCES "DiscordRole"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRoles" ADD CONSTRAINT "DiscordRoles_leoSupervisorRoleId_fkey" FOREIGN KEY ("leoSupervisorRoleId") REFERENCES "DiscordRole"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRoles" ADD CONSTRAINT "DiscordRoles_emsFdRoleId_fkey" FOREIGN KEY ("emsFdRoleId") REFERENCES "DiscordRole"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRoles" ADD CONSTRAINT "DiscordRoles_dispatchRoleId_fkey" FOREIGN KEY ("dispatchRoleId") REFERENCES "DiscordRole"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRoles" ADD CONSTRAINT "DiscordRoles_towRoleId_fkey" FOREIGN KEY ("towRoleId") REFERENCES "DiscordRole"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DiscordRole" ADD CONSTRAINT "DiscordRole_discordRolesId_fkey" FOREIGN KEY ("discordRolesId") REFERENCES "DiscordRoles"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import process from "node:process"; | ||
import { BodyParams, Context, Controller, UseBeforeEach } from "@tsed/common"; | ||
import { Get, Post } from "@tsed/schema"; | ||
import { RESTGetAPIGuildRolesResult, Routes } from "discord-api-types/v9"; | ||
import { IsAuth } from "middlewares/IsAuth"; | ||
import { prisma } from "lib/prisma"; | ||
import type { cad } from "@prisma/client"; | ||
import { BadRequest } from "@tsed/exceptions"; | ||
import { DISCORD_SETTINGS_SCHEMA } from "@snailycad/schemas"; | ||
import { validateSchema } from "lib/validateSchema"; | ||
import { getRest } from "lib/discord"; | ||
|
||
const guildId = process.env.DISCORD_SERVER_ID; | ||
|
||
@Controller("/admin/manage/cad-settings/discord") | ||
@UseBeforeEach(IsAuth) | ||
export class DiscordSettingsController { | ||
@Get("/") | ||
async getGuildRoles(@Context("cad") cad: cad) { | ||
if (!guildId) { | ||
throw new BadRequest("mustSetBotTokenGuildId"); | ||
} | ||
|
||
const rest = getRest(); | ||
const roles = (await rest.get( | ||
`${Routes.guildRoles(guildId)}`, | ||
)) as RESTGetAPIGuildRolesResult | null; | ||
|
||
const discordRoles = await prisma.discordRoles.upsert({ | ||
where: { id: String(cad.discordRolesId) }, | ||
update: { guildId }, | ||
create: { | ||
guildId, | ||
}, | ||
}); | ||
|
||
await prisma.cad.update({ | ||
where: { id: cad.id }, | ||
data: { discordRolesId: discordRoles.id }, | ||
}); | ||
|
||
const rolesBody = Array.isArray(roles) ? roles : []; | ||
const data = []; | ||
|
||
for (const role of rolesBody) { | ||
if (role.name === "@everyone") continue; | ||
|
||
const discordRole = await prisma.discordRole.upsert({ | ||
where: { id: role.id }, | ||
create: { | ||
name: role.name, | ||
id: role.id, | ||
discordRolesId: discordRoles.id, | ||
}, | ||
update: { | ||
name: role.name, | ||
discordRolesId: discordRoles.id, | ||
}, | ||
}); | ||
|
||
data.push(discordRole); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
@Post("/") | ||
async setRoleTypes(@Context("cad") cad: cad, @BodyParams() body: unknown) { | ||
if (!guildId) { | ||
throw new BadRequest("mustSetBotTokenGuildId"); | ||
} | ||
|
||
const data = validateSchema(DISCORD_SETTINGS_SCHEMA, body); | ||
|
||
const rest = getRest(); | ||
const roles = (await rest.get( | ||
`${Routes.guildRoles(guildId)}`, | ||
)) as RESTGetAPIGuildRolesResult | null; | ||
|
||
const rolesBody = Array.isArray(roles) ? roles : []; | ||
|
||
Object.values(data).map((roleId) => { | ||
if (roleId && !this.doesRoleExist(rolesBody, roleId)) { | ||
throw new BadRequest("invalidRoleId"); | ||
} | ||
}); | ||
|
||
const createUpdateData = { | ||
guildId, | ||
dispatchRoleId: data.dispatchRoleId ?? null, | ||
leoRoleId: data.leoRoleId ?? null, | ||
leoSupervisorRoleId: data.leoSupervisorRoleId ?? null, | ||
emsFdRoleId: data.emsFdRoleId ?? null, | ||
towRoleId: data.towRoleId ?? null, | ||
}; | ||
|
||
const discordRoles = await prisma.discordRoles.upsert({ | ||
where: { id: String(cad.discordRolesId) }, | ||
update: createUpdateData, | ||
create: createUpdateData, | ||
}); | ||
|
||
await prisma.cad.update({ | ||
where: { id: cad.id }, | ||
data: { discordRolesId: discordRoles.id }, | ||
}); | ||
|
||
return discordRoles; | ||
} | ||
|
||
protected doesRoleExist(roles: { id: string }[], roleId: string) { | ||
return roles.some((v) => v.id === roleId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.