Skip to content

Commit 223907c

Browse files
committed
route and types for editing group role by super-users
1 parent 0aef740 commit 223907c

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

src/constants/responses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const NAME_CHANGED = "User nickname changed successfully";
3434

3535
export const ROLE_REMOVED = "Role Removed successfully";
3636

37+
export const ROLE_UPDATED = "Role updated successfully";
38+
3739
export const VERIFICATION_STRING =
3840
"Please verify your discord account by clicking the link below 👇";
3941

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This function updates group-role in discord.
2+
import * as response from "../constants/responses";
3+
import { IRequest } from "itty-router";
4+
import { env } from "../typeDefinitions/default.types";
5+
import JSONResponse from "../utils/JsonResponse";
6+
import { verifyNodejsBackendAuthToken } from "../utils/verifyAuthToken";
7+
import { updateRole } from "../typeDefinitions/discordMessage.types";
8+
import { updateGuildRole } from "../utils/editGroupRole";
9+
10+
export async function editGuildRoleHandler(request: IRequest, env: env) {
11+
const authHeader = request.headers.get("Authorization");
12+
const reason = request.headers.get("X-Audit-Log-Reason");
13+
const roleId = decodeURI(request.params?.roleId ?? "");
14+
const { dev } = request.query;
15+
const devFlag = dev === "true";
16+
17+
if (!authHeader) {
18+
console.log("authheader did it");
19+
return new JSONResponse(response.BAD_SIGNATURE, { status: 401 });
20+
}
21+
22+
if (!devFlag) {
23+
return new JSONResponse(response.NOT_IMPLEMENTED, { status: 501 });
24+
}
25+
if (!roleId) {
26+
return new JSONResponse(response.BAD_REQUEST, { status: 400 });
27+
}
28+
try {
29+
await verifyNodejsBackendAuthToken(authHeader, env);
30+
const body: updateRole = await request.json();
31+
32+
const result = await updateGuildRole(body.rolename, roleId, env, reason);
33+
if (result === response.ROLE_UPDATED) {
34+
return new Response(null, { status: 204 });
35+
} else {
36+
return new JSONResponse(response.INTERNAL_SERVER_ERROR, {
37+
status: 500,
38+
});
39+
}
40+
} catch (err) {
41+
console.log("Error updating guild role: ", err);
42+
return new JSONResponse(response.INTERNAL_SERVER_ERROR, {
43+
status: 500,
44+
});
45+
}
46+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { sendTaskUpdatesHandler } from "./controllers/taskUpdatesHandler";
2424

2525
import config, { loadEnv } from "./../config/config";
2626
import { deleteGuildRoleHandler } from "./controllers/deleteGuildRoleHandler";
27+
import { editGuildRoleHandler } from "./controllers/editGuildRolesHandler";
2728

2829
const router = Router();
2930

@@ -65,6 +66,8 @@ router.post("/profile/blocked", sendProfileBlockedMessage);
6566

6667
router.post("/task/update", sendTaskUpdatesHandler);
6768

69+
router.patch("/roles/:roleId", editGuildRoleHandler);
70+
6871
router.get("/ankush", async (request, env, ctx: ExecutionContext) => {
6972
ctx.waitUntil(send(env));
7073

src/typeDefinitions/discordMessage.types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export interface createNewRole {
9090
rolename: string;
9191
mentionable: boolean;
9292
}
93+
export interface updateRole {
94+
roleid: string;
95+
rolename: string;
96+
mentionable: boolean;
97+
}
9398
export interface memberGroupRole {
9499
userid: string;
95100
roleid: string;

src/utils/editGroupRole.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { INTERNAL_SERVER_ERROR, ROLE_UPDATED } from "../constants/responses";
2+
import { DISCORD_BASE_URL } from "../constants/urls";
3+
import { env } from "../typeDefinitions/default.types";
4+
5+
import createDiscordHeaders from "./createDiscordHeaders";
6+
7+
export async function updateGuildRole(
8+
rolename: string,
9+
roleid: string,
10+
env: env,
11+
reason?: string
12+
) {
13+
const updateGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles/${roleid}`;
14+
15+
const headers: HeadersInit = createDiscordHeaders({
16+
reason,
17+
token: env.DISCORD_TOKEN,
18+
});
19+
const data = {
20+
name: rolename,
21+
mentionable: true,
22+
};
23+
try {
24+
const response = await fetch(updateGuildRoleUrl, {
25+
method: "PATCH",
26+
headers,
27+
body: JSON.stringify(data),
28+
});
29+
if (response.ok) {
30+
return ROLE_UPDATED;
31+
} else {
32+
return INTERNAL_SERVER_ERROR;
33+
}
34+
} catch (err) {
35+
return INTERNAL_SERVER_ERROR;
36+
}
37+
}

0 commit comments

Comments
 (0)