Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
waki285 committed Aug 3, 2023
1 parent e132562 commit ac458d0
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
90 changes: 90 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Hono } from "hono";
import { verifyDiscordRequest } from "@/verifyDiscordRequest";
import {
ButtonStyle,
ComponentType,
InteractionResponseType,
InteractionType,
MessageFlags,
Expand Down Expand Up @@ -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/);

Check warning on line 208 in src/server.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
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[] = [];

Check warning on line 224 in src/server.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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,
},
],
},
],
},
});
}
}
}
}
Expand Down

0 comments on commit ac458d0

Please sign in to comment.