diff --git a/src/commands/getShiggies.ts b/src/commands/getShiggies.ts new file mode 100644 index 0000000..a5998a5 --- /dev/null +++ b/src/commands/getShiggies.ts @@ -0,0 +1,18 @@ +import { SlashCommandBuilder } from "discord.js"; +import { PrefixCommand, SlashCommand } from "../commands"; +import changePfp from "../pfp"; +import authenticatedRequest from "../utils/authenticatedRequest"; + +export default new PrefixCommand({ + name: "getShiggies", + description: "refetches shiggies", + usage: "getShiggies", + callback: async (msg) => { + try { + await authenticatedRequest("/api/v0/getShiggies"); + msg.reply("Fetching shiggies..."); + } catch (e) { + msg.reply("Failed to request!"); + } + }, +}); diff --git a/src/utils/authenticatedRequest.ts b/src/utils/authenticatedRequest.ts new file mode 100644 index 0000000..a29ef7b --- /dev/null +++ b/src/utils/authenticatedRequest.ts @@ -0,0 +1,27 @@ +export default async function authenticatedRequest( + path: string, + method: "GET" | "POST" | "PUT" | "DELETE" = "GET", + body?: any +) { + const options: RequestInit = { + method, + }; + + if (body) { + options.body = JSON.stringify(body); + options.headers = { + "Content-Type": "application/json", + }; + } + + if (!process.env.SHARED_KEY) { + console.warn("No shared key set, skipping authentication"); + } else { + options.headers = { + ...options.headers, + Authorization: process.env.SHARED_KEY, + }; + } + + return await fetch(`http://app:4321/${path}`, options); +}