refactor: streamline API request handling and enhance message processing

This commit is contained in:
fzzin
2025-04-08 18:48:16 +02:00
parent 1e8f195714
commit 79750ed5cc
4 changed files with 157 additions and 110 deletions

View File

@@ -1,5 +1,57 @@
import { assert } from "@std/assert/assert"; import { requestJSON } from "@root/structures/apiRequest.ts";
import { requestJSON, requestRaw } from "@root/structures/apiRequest.ts"; const baseURL = "https://yande.re";
const apikeys = [
"post",
"tags",
"artist",
"comments",
"wiki",
"notes",
"users",
"forum",
"pools",
] as const;
type APIKeys = typeof apikeys[number];
function prepareURLObjectForRequest(type: APIKeys) {
const tmp = new URL(baseURL);
tmp.searchParams.append("api_version", "3");
switch (type) {
case "post":
tmp.pathname = "post.json";
break;
case "tags":
tmp.pathname = "tag.json";
break;
case "artist":
tmp.pathname = "artist.json";
break;
case "comments":
tmp.pathname = "comment";
break;
case "wiki":
tmp.pathname = "wiki.json";
break;
case "notes":
tmp.pathname = "note.json";
break;
case "users":
tmp.pathname = "user.json";
break;
case "forum":
tmp.pathname = "forum.json";
break;
case "pools":
tmp.pathname = "pool.json";
break;
default:
throw Error("unknown ");
}
return tmp;
}
export function getPosts(search? : string){
const url = prepareURLObjectForRequest("post")
return requestJSON(url)
}
const baseURL = "https://yande.re"
const postURL = `${baseURL}/post.json?api_version=2`;

View File

@@ -1,46 +1,46 @@
type PostResponse = { type PostResponse = {
posts: { posts: {
id: number; id: number;
tags: string; tags: string;
created_at: string; created_at: string;
creator_id: number; creator_id: number;
approver_id: number; approver_id: number;
author: string; author: string;
change: number; change: number;
source: string; source: string;
score: number; score: number;
md5: string; md5: string;
file_size: number; file_size: number;
file_ext: string; file_ext: string;
file_url: string; file_url: string;
is_shown_in_index: boolean; is_shown_in_index: boolean;
preview_url: string; preview_url: string;
preview_width: number; preview_width: number;
preview_height: number; preview_height: number;
actual_preview_width: number; actual_preview_width: number;
actual_preview_height: number; actual_preview_height: number;
sample_url: string; sample_url: string;
sample_width: number; sample_width: number;
sample_height: number; sample_height: number;
sample_file_size: number; sample_file_size: number;
jpeg_url: string; jpeg_url: string;
jpeg_width: number; jpeg_width: number;
jpeg_height: number; jpeg_height: number;
rating: string; rating: string;
is_rating_locked: boolean; is_rating_locked: boolean;
has_children: boolean; has_children: boolean;
parent_id: number; parent_id: number;
status: string; status: string;
is_pending: boolean; is_pending: boolean;
width: number; width: number;
height: number; height: number;
is_held: boolean; is_held: boolean;
frames_pending_string: string; frames_pending_string: string;
frames_pending: []; frames_pending: [];
frames_string: string; frames_string: string;
frames: []; frames: [];
is_note_locked: boolean; is_note_locked: boolean;
last_noted_at: string; last_noted_at: string;
last_commented_at: string; last_commented_at: string;
}[]; }[];
}; };

View File

@@ -1,69 +1,22 @@
import { Bot, Message } from "npm:discordeno@18.0.1"; import { Bot, DiscordEmbed, Embed, Message } from "npm:discordeno@18.0.1";
import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api/api.ts";
import { defaultString } from "@root/defaultString.ts"; import { defaultString } from "@root/defaultString.ts";
import { logMessage } from "@root/logging.ts"; import { logMessage } from "@root/logging.ts";
import { drop, requestWorker } from "@root/plugins/yandere/plugin.ts";
export async function yandereMessageHandler(bot: Bot, message: Message) { export async function yandereMessageHandler(bot: Bot, message: Message) {
const command = message.content.split(" ").slice(1).join(" "); const command = message.content.split(" ").slice(1).join(" ");
const args = message.content.split(" ").slice(1);
switch (command) { if (command === "drop") {
case `drop`: bot.helpers.sendMessage(message.channelId, {
logMessage(message); embeds: [await drop()]
if ( })
message.channelId === 754338073101205524n || } else if (command.startsWith("drop [") && command.endsWith("]")) {
message.guildId === undefined logMessage(message);
) { const generatedEmbeds: Embed[] = await requestWorker(command);
await refresh(); for (const embed of generatedEmbeds) {
bot.helpers.sendMessage(message.channelId, { bot.helpers.sendMessage(message.channelId, {
content: defaultString(await dropYandere()), embeds: [embed],
}); });
} }
break;
case `drop 5`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
await refresh();
bot.helpers.sendMessage(message.channelId, {
content: defaultString(await dropYandere5()),
});
}
break;
case `page`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
if (args[0] === undefined) {
bot.helpers.sendMessage(message.channelId, {
content: "Please provide a page number",
});
} else if (isNaN(parseInt(args[0]))) {
bot.helpers.sendMessage(message.channelId, {
content: "Please provide a valid number",
});
} else {
await setPage(parseInt(args[0]));
bot.helpers.sendMessage(message.channelId, {
content: "Page set to " + args[0],
});
}
}
break;
case `getpage`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
bot.helpers.sendMessage(message.channelId, {
content: "Page is " + getPage(),
});
}
break;
} }
} }

View File

@@ -0,0 +1,42 @@
import {DiscordEmbedImage} from "npm:discordeno@18.0.1";
import {}
export function drop(): DiscordEmbedImage {
const tmp = await
return {url: }
}
export function requestWorker(request: string): DiscordEmbedImage[] {
}
function isKey<T>(keys: string[], key: string): key is T {
return keys.includes(key as any);
}
export function requestParser<T>(requestString: string) {
const res = requestString.match(/\[([\s*w+:\s*[\d+|\w+,]+)\]/g);
const map = new Map<T, string>();
if (res !== null) {
res[0]
.split(",")
.forEach((param) => {
const match = param.match(/\s*(\w+)\s*:\s*([\w+,+]+)/);
console.debug({ match });
if (match !== null) {
if (match[1] === undefined || match[2] === undefined) {
throw Error("Unreachable");
}
if (!isKey(match[1])) throw Error(`Key: ${match[1]} is not a Key!`);
map.set(match[1], match[2]);
} else {
throw Error(`match returned null in param = ${Deno.inspect(param)}`);
}
});
} else {
throw Error("Request String had some major issues chief");
}
console.debug();
return map;
}