summaryrefslogtreecommitdiff
path: root/packages/interactions/server.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-10-08 23:00:03 -0700
committerFuwn <[email protected]>2025-10-08 23:00:03 -0700
commit2267348349241a86702eaf5a5b422bc3b9ed665f (patch)
treed02d78293a93e53b4bfa37d5db58af4a215f0dce /packages/interactions/server.ts
parentfeat(shared): Add staff roles constant (diff)
downloadumabotdiscord-2267348349241a86702eaf5a5b422bc3b9ed665f.tar.xz
umabotdiscord-2267348349241a86702eaf5a5b422bc3b9ed665f.zip
feat(interactions): Add toggle-privileged-access slash command
Diffstat (limited to 'packages/interactions/server.ts')
-rw-r--r--packages/interactions/server.ts123
1 files changed, 122 insertions, 1 deletions
diff --git a/packages/interactions/server.ts b/packages/interactions/server.ts
index f836286..baaecd3 100644
--- a/packages/interactions/server.ts
+++ b/packages/interactions/server.ts
@@ -11,6 +11,7 @@ import {
APPEAL_COMMAND,
COLOURS_COMMAND,
ROLEPLAY_VERIFY_COMMAND,
+ TOGGLE_PRIVILEGED_ACCESS_COMMAND,
} from "./discord/commands";
import {
getCutePost,
@@ -28,7 +29,12 @@ import {
} from "./discord/embeds.ts";
import { JSONResponse } from "./discord/responses.ts";
import { verifyDiscordRequest } from "./discord/verification.ts";
-import { CENTRAL_GUILD_ID, COLOR_ROLE_IDS } from "../shared";
+import {
+ CENTRAL_GUILD_ID,
+ COLOR_ROLE_IDS,
+ CENTRAL_STAFF_ROLES,
+ CENTRAL_PRIVILEGED_ACCESS_ROLE_ID,
+} from "../shared";
const router = AutoRouter();
const COMPLAINT_CHANNEL_ID = "1415868433714778204";
@@ -701,6 +707,121 @@ router.post("/", async (request: Request, environment: Environment) => {
}
}
+ case TOGGLE_PRIVILEGED_ACCESS_COMMAND.name.toLowerCase(): {
+ try {
+ const member = interaction.member;
+ const hasAdminPermission =
+ member?.permissions && (parseInt(member.permissions) & 0x8) === 0x8;
+ const hasStaffRole = member?.roles?.some((roleId) =>
+ CENTRAL_STAFF_ROLES.includes(roleId as any),
+ );
+
+ if (!hasAdminPermission && !hasStaffRole)
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content:
+ "❌ You don't have permission to use this command. Only staff members or server owner can use this command.",
+ flags: 64,
+ },
+ });
+
+ const targetUserID = interaction.data.options?.[0]?.value as string;
+
+ if (!targetUserID)
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: "❌ Please provide a target user.",
+ flags: 64,
+ },
+ });
+
+ const guild = await fetch(
+ `https://discord.com/api/v10/guilds/${CENTRAL_GUILD_ID}/members/${targetUserID}`,
+ {
+ headers: {
+ Authorization: `Bot ${environment.DISCORD_TOKEN}`,
+ },
+ },
+ );
+
+ if (!guild.ok)
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content:
+ "❌ Unable to fetch user information. The user may not be in this server.",
+ flags: 64,
+ },
+ });
+
+ const targetMember = await guild.json();
+ const currentRoles = targetMember.roles || [];
+ const hasRole = currentRoles.includes(
+ CENTRAL_PRIVILEGED_ACCESS_ROLE_ID,
+ );
+ let newRoles = [...currentRoles];
+
+ if (hasRole) {
+ newRoles = newRoles.filter(
+ (roleId) => roleId !== CENTRAL_PRIVILEGED_ACCESS_ROLE_ID,
+ );
+ } else {
+ newRoles.push(CENTRAL_PRIVILEGED_ACCESS_ROLE_ID);
+ }
+
+ const updateResponse = await fetch(
+ `https://discord.com/api/v10/guilds/${CENTRAL_GUILD_ID}/members/${targetUserID}`,
+ {
+ method: "PATCH",
+ headers: {
+ Authorization: `Bot ${environment.DISCORD_TOKEN}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ roles: newRoles,
+ }),
+ },
+ );
+
+ if (!updateResponse.ok) {
+ console.error(
+ "Failed to update user roles:",
+ await updateResponse.text(),
+ );
+
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content:
+ "❌ Failed to update user roles. The bot may not have sufficient permissions.",
+ flags: 64,
+ },
+ });
+ }
+
+ const action = hasRole ? "removed" : "added";
+
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: `✅ Successfully ${action} privileged access role for <@${targetUserID}>.`,
+ },
+ });
+ } catch (error) {
+ console.error("Error in toggle-privileged-access command:", error);
+
+ return new JSONResponse({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: "❌ An error occurred while managing the role.",
+ flags: 64,
+ },
+ });
+ }
+ }
+
default:
return new JSONResponse({ error: "Unknown Type" }, { status: 400 });
}