forked from Real-Dev-Squad/discord-slash-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
route and types for editing group role by super-users
- Loading branch information
1 parent
0aef740
commit 223907c
Showing
5 changed files
with
93 additions
and
0 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
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,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, | ||
}); | ||
} | ||
} |
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
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,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; | ||
} | ||
} |