Skip to content

Commit

Permalink
route and types for editing group role by super-users
Browse files Browse the repository at this point in the history
  • Loading branch information
mdansarijaved committed Jan 8, 2025
1 parent 0aef740 commit 223907c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/constants/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const NAME_CHANGED = "User nickname changed successfully";

export const ROLE_REMOVED = "Role Removed successfully";

export const ROLE_UPDATED = "Role updated successfully";

export const VERIFICATION_STRING =
"Please verify your discord account by clicking the link below 👇";

Expand Down
46 changes: 46 additions & 0 deletions src/controllers/editGuildRolesHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This function updates group-role in discord.
import * as response from "../constants/responses";
import { IRequest } from "itty-router";
import { env } from "../typeDefinitions/default.types";
import JSONResponse from "../utils/JsonResponse";
import { verifyNodejsBackendAuthToken } from "../utils/verifyAuthToken";
import { updateRole } from "../typeDefinitions/discordMessage.types";
import { updateGuildRole } from "../utils/editGroupRole";

export async function editGuildRoleHandler(request: IRequest, env: env) {
const authHeader = request.headers.get("Authorization");
const reason = request.headers.get("X-Audit-Log-Reason");
const roleId = decodeURI(request.params?.roleId ?? "");
const { dev } = request.query;
const devFlag = dev === "true";

if (!authHeader) {
console.log("authheader did it");
return new JSONResponse(response.BAD_SIGNATURE, { status: 401 });
}

if (!devFlag) {
return new JSONResponse(response.NOT_IMPLEMENTED, { status: 501 });
}
if (!roleId) {
return new JSONResponse(response.BAD_REQUEST, { status: 400 });
}
try {
await verifyNodejsBackendAuthToken(authHeader, env);
const body: updateRole = await request.json();

const result = await updateGuildRole(body.rolename, roleId, env, reason);
if (result === response.ROLE_UPDATED) {
return new Response(null, { status: 204 });
} else {
return new JSONResponse(response.INTERNAL_SERVER_ERROR, {
status: 500,
});
}
} catch (err) {
console.log("Error updating guild role: ", err);
return new JSONResponse(response.INTERNAL_SERVER_ERROR, {
status: 500,
});
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { sendTaskUpdatesHandler } from "./controllers/taskUpdatesHandler";

import config, { loadEnv } from "./../config/config";
import { deleteGuildRoleHandler } from "./controllers/deleteGuildRoleHandler";
import { editGuildRoleHandler } from "./controllers/editGuildRolesHandler";

const router = Router();

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

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

router.patch("/roles/:roleId", editGuildRoleHandler);

router.get("/ankush", async (request, env, ctx: ExecutionContext) => {
ctx.waitUntil(send(env));

Expand Down
5 changes: 5 additions & 0 deletions src/typeDefinitions/discordMessage.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export interface createNewRole {
rolename: string;
mentionable: boolean;
}
export interface updateRole {
roleid: string;
rolename: string;
mentionable: boolean;
}
export interface memberGroupRole {
userid: string;
roleid: string;
Expand Down
37 changes: 37 additions & 0 deletions src/utils/editGroupRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { INTERNAL_SERVER_ERROR, ROLE_UPDATED } from "../constants/responses";
import { DISCORD_BASE_URL } from "../constants/urls";
import { env } from "../typeDefinitions/default.types";

import createDiscordHeaders from "./createDiscordHeaders";

export async function updateGuildRole(
rolename: string,
roleid: string,
env: env,
reason?: string
) {
const updateGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles/${roleid}`;

const headers: HeadersInit = createDiscordHeaders({
reason,
token: env.DISCORD_TOKEN,
});
const data = {
name: rolename,
mentionable: true,
};
try {
const response = await fetch(updateGuildRoleUrl, {
method: "PATCH",
headers,
body: JSON.stringify(data),
});
if (response.ok) {
return ROLE_UPDATED;
} else {
return INTERNAL_SERVER_ERROR;
}
} catch (err) {
return INTERNAL_SERVER_ERROR;
}
}

0 comments on commit 223907c

Please sign in to comment.