From 1257d1419d04678296441904a5576464ad86ac27 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 11 Sep 2025 18:13:31 -0700 Subject: feat: Add complaint system --- src/server.ts | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 2 deletions(-) (limited to 'src/server.ts') diff --git a/src/server.ts b/src/server.ts index bf3acf2..8925bdc 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5,6 +5,7 @@ import { ROLEPLAY_COMMAND, NSFW_COMMAND, TOP_COMMAND, + COMPLAIN_COMMAND, } from "./discord/commands.ts"; import { getCutePost, @@ -13,12 +14,39 @@ import { getTopPost, } from "./reddit.ts"; import type { TimePeriod } from "./discord/types.ts"; -import type { Environment } from "./discord/interfaces.ts"; -import { createPostEmbed } from "./discord/embeds.ts"; +import type { Environment, DiscordEmbed } from "./discord/interfaces.ts"; +import { createPostEmbed, createComplaintEmbed } from "./discord/embeds.ts"; import { JSONResponse } from "./discord/responses.ts"; import { verifyDiscordRequest } from "./discord/verification.ts"; const router = AutoRouter(); +const COMPLAINT_CHANNEL_ID = "1406422617724026901"; + +const sendComplaintToChannel = async ( + environment: Environment, + embed: DiscordEmbed, +): Promise => { + const url = `https://discord.com/api/v10/channels/${COMPLAINT_CHANNEL_ID}/messages`; + + try { + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${environment.DISCORD_TOKEN}`, + }, + body: JSON.stringify({ + embeds: [embed], + }), + }); + + return response.ok; + } catch (error) { + console.error("Error sending complaint to channel:", error); + + return false; + } +}; router.get("/", (_request: Request, environment: Environment) => { return new Response(`👋 ${environment.DISCORD_APPLICATION_ID}`); @@ -148,6 +176,73 @@ router.post("/", async (request: Request, environment: Environment) => { } } + case COMPLAIN_COMMAND.name.toLowerCase(): { + try { + const complaintMessage = interaction.data.options?.[0] + ?.value as string; + + if (!complaintMessage) + return new JSONResponse({ + type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, + data: { + content: "❌ Please provide a message for your complaint.", + flags: 64, + }, + }); + + const complainant = { + username: + interaction.member?.user?.username || + interaction.user?.username || + "Unknown", + id: + interaction.member?.user?.id || interaction.user?.id || "Unknown", + avatar: + interaction.member?.user?.avatar || interaction.user?.avatar, + }; + const isDM = !interaction.guild_id; + const complaintEmbed = createComplaintEmbed( + complaintMessage, + complainant, + Date.now(), + isDM, + ); + const success = await sendComplaintToChannel( + environment, + complaintEmbed, + ); + + if (success) { + return new JSONResponse({ + type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, + data: { + content: "✅ Your complaint has been submitted successfully!", + flags: 64, + }, + }); + } else { + return new JSONResponse({ + type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, + data: { + content: + "❌ Failed to submit your complaint. Please try again later.", + flags: 64, + }, + }); + } + } catch (error) { + console.error("Error in complain command:", error); + + return new JSONResponse({ + type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, + data: { + content: "❌ An error occurred while processing your complaint.", + flags: 64, + }, + }); + } + } + default: return new JSONResponse({ error: "Unknown Type" }, { status: 400 }); } -- cgit v1.2.3