From ac458d0675613e82b1f9d9cb53f9e0452b9fb312 Mon Sep 17 00:00:00 2001 From: waki285 <67305123+waki285@users.noreply.github.com> Date: Thu, 3 Aug 2023 11:32:32 +0000 Subject: [PATCH] lint --- src/commands.ts | 13 +++++++ src/server.ts | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/commands.ts b/src/commands.ts index 23e0e84..feaaf25 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -27,6 +27,19 @@ const commands: RESTPostAPIApplicationCommandsJSONBody[] = [ }, ], }, + { + name: "masscheck", + description: "Mass-check OpenAI Keys", + options: [ + { + type: ApplicationCommandOptionType.String, + name: "key", + description: "Keys. Split with , or line feed or \\n", + required: true, + min_length: 80, + }, + ], + }, ]; export default commands; diff --git a/src/server.ts b/src/server.ts index 4522daa..00b2860 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,6 +1,8 @@ import { Hono } from "hono"; import { verifyDiscordRequest } from "@/verifyDiscordRequest"; import { + ButtonStyle, + ComponentType, InteractionResponseType, InteractionType, MessageFlags, @@ -199,6 +201,94 @@ app.post("/interactions", async (c) => { }, }); } + case "masscheck": { + logger.debug("handling masscheck interaction"); + const api = interaction.data.options?.filter(isString)[0]; + logger.debug(api); + const keys = api!.value.split(/,|\n|\\n/); + const responses = await Promise.all( + keys.map((x) => { + return fetch("https://api.openai.com/v1/chat/completions", { + method: "POST", + headers: { + Authorization: `Bearer ${x}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ model: "gpt-3.5-turbo" }), + }); + }), + ); + const responsesJson = await Promise.all( + responses.map((x) => x.json()), + ); + const results: any[] = []; + responsesJson.forEach((result, i) => { + if (result.error && result.error.code === "invalid_api_key") { + results.push({ + key: keys[i], + available: false, + gpt4: false, + reason: "Invalid API Key", + }); + } else if (result.error.code === "insufficient_quota") { + results.push({ + key: keys[i], + available: false, + gpt4: false, + reason: "Exceeded quota", + }); + } else if (responses[i].status === 429) { + results.push({ + key: keys[i], + available: false, + gpt4: false, + reason: "Server Error - 429", + }); + } else if (result.error) { + results.push({ + key: keys[i], + available: false, + gpt4: false, + reason: result.error.message, + }); + } else { + results.push({ + key: keys[i], + available: true, + gpt4: false, + reason: "", + }); + } + }); + return c.json({ + type: InteractionResponseType.ChannelMessageWithSource, + data: { + embeds: [ + { + description: results + .map( + (x) => + `*${x.key}* - ${x.available ? "🟢" : "🔴"} ${x.reason}`, + ) + .join("\n"), + }, + ], + components: [ + { + type: ComponentType.ActionRow, + components: [ + { + type: ComponentType.Button, + label: "Check GPT-4", + custom_id: "gpt4", + style: ButtonStyle.Secondary, + }, + ], + }, + ], + }, + }); + } } } }