diff --git a/components-jsx/ComponentMessage.tsx b/components-jsx/ComponentMessage.tsx index f9a7dd0..f722ba1 100644 --- a/components-jsx/ComponentMessage.tsx +++ b/components-jsx/ComponentMessage.tsx @@ -1,14 +1,26 @@ -import { CreateMessageOptions, EditMessageOptions, MessageComponent, MessageFlags } from "oceanic.js"; +import { + CreateMessageOptions, + EditMessageOptions, + InteractionOptions, + MessageComponent, + MessageFlags +} from "oceanic.js"; import { childrenToArray } from "./utils"; type MessageOptions = CreateMessageOptions | EditMessageOptions; -export type ComponentMessageProps = MessageOptions & { children: MessageComponent[]; }; +export type ComponentMessageProps = MessageOptions & { + children: MessageComponent[]; +}; -export function ComponentMessage({ children, flags, ...props }: ComponentMessageProps): MessageOptions { - return { - flags: MessageFlags.IS_COMPONENTS_V2 | (flags ?? 0), - components: childrenToArray(children), - ...props - }; +export function ComponentMessage({ + children, + flags, + ...props +}: ComponentMessageProps): MessageOptions { + return { + flags: MessageFlags.IS_COMPONENTS_V2 | (flags ?? 0), + components: childrenToArray(children), + ...props + }; } diff --git a/src/Constants.ts b/src/Constants.ts index b0d4873..1a07fff 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -1,6 +1,10 @@ export const Constants = { PENDING_CHANNEL_ID: "1370539719842070683", REJECTION_CHANNEL_ID: "1371073658403164261", + REVIEWER_ROLE_ID: "1375617676139040909", + MOD_ROLE_ID: "1370539692143153152", + MANAGER_ROLE_ID: "1370539689659863091", + OWNER_ID: "886685857560539176", COLORS: { NEW_REQ: 0xaaaaff, BAD: 0xffaaaa, diff --git a/src/joinRequestHandler.tsx b/src/joinRequestHandler.tsx index 597d88d..9279374 100644 --- a/src/joinRequestHandler.tsx +++ b/src/joinRequestHandler.tsx @@ -19,6 +19,8 @@ import { match } from "./utils"; import { PendingApplicationMessage } from "./components/PendingApplicationMessage"; import { ApplicationContent } from "./components/ApplicationContent"; +import("./lockUnlock"); + export async function setupJoinRequestHandler(shard: Shard) { shard.ws?.on("message", async (d) => { const data = JSON.parse(d.toString("utf8")); @@ -127,7 +129,7 @@ export async function setupJoinRequestHandler(shard: Shard) { } const response: Response = JSON.parse( - application.responses + application ? application.responses : {} // this will never be used if no applications ); const msg = await client.rest.channels.createMessage( diff --git a/src/lockUnlock.tsx b/src/lockUnlock.tsx new file mode 100644 index 0000000..a58f5bd --- /dev/null +++ b/src/lockUnlock.tsx @@ -0,0 +1,50 @@ +import { ComponentTypes, InteractionTypes, MessageFlags } from "oceanic.js"; +import { client } from "."; +import { db } from "./database"; +import { PendingApplicationMessage } from "./components/PendingApplicationMessage"; +import { canUser } from "./utils"; + +client.on("interactionCreate", async (interaction) => { + if (interaction.type === InteractionTypes.MESSAGE_COMPONENT) + if (interaction.data.componentType === ComponentTypes.BUTTON) { + if (!canUser(interaction.member!, "owner")) + return await interaction.createMessage({ + flags: MessageFlags.EPHEMERAL, + content: "💢 nop" + }); + + const application = await db.get( + "SELECT * FROM applications WHERE message_id=?", + interaction.message.id + ); + + switch (interaction.data.customID.split("-")[0]) { + case "lock": { + await interaction.editParent( + + ); + break; + } + case "unlock": { + await interaction.editParent( + + ); + break; + } + } + } +}); diff --git a/src/selfappReq.ts b/src/selfappReq.ts index a2d5e11..cbde29f 100644 --- a/src/selfappReq.ts +++ b/src/selfappReq.ts @@ -12,8 +12,6 @@ export async function selfappReq( return `?${new URLSearchParams(data).toString()}`; })(); - console.log(url); - return await fetch(url, { method, headers: { diff --git a/src/utils.ts b/src/utils.ts index 5b95965..e6baeed 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,6 @@ +import { Member } from "oceanic.js"; +import { Constants } from "./Constants"; + export function match( value: any, matches: { @@ -7,3 +10,39 @@ export function match( if (matches[value]) return matches[value]; else return null; } + +export function canUser( + member: Member, + can: "review" | "moderate" | "manage" | "owner" +) { + switch (can) { + case "review": + return ( + member.roles.some((role) => + [ + Constants.REVIEWER_ROLE_ID, + Constants.MOD_ROLE_ID, + Constants.MANAGER_ROLE_ID + ].includes(role) + ) || member.id === Constants.OWNER_ID + ); + case "moderate": + return ( + member.roles.some((role) => + [Constants.MOD_ROLE_ID, Constants.MANAGER_ROLE_ID].includes( + role + ) + ) || member.id === Constants.OWNER_ID + ); + case "manage": + return ( + member.roles.some((role) => + [Constants.MANAGER_ROLE_ID].includes(role) + ) || member.id === Constants.OWNER_ID + ); + case "owner": + return member.id === Constants.OWNER_ID; + default: + return false; + } +}