From 79750ed5ccba547669053dbd158101d8f0518387 Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Tue, 8 Apr 2025 18:48:16 +0200 Subject: [PATCH] refactor: streamline API request handling and enhance message processing --- src/plugins/yandere/api/api.ts | 60 ++++++++++++++++++++-- src/plugins/yandere/api/post.ts | 90 ++++++++++++++++----------------- src/plugins/yandere/messages.ts | 75 +++++---------------------- src/plugins/yandere/plugin.ts | 42 +++++++++++++++ 4 files changed, 157 insertions(+), 110 deletions(-) diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts index bf9bf6f..951e592 100644 --- a/src/plugins/yandere/api/api.ts +++ b/src/plugins/yandere/api/api.ts @@ -1,5 +1,57 @@ -import { assert } from "@std/assert/assert"; -import { requestJSON, requestRaw } from "@root/structures/apiRequest.ts"; +import { requestJSON } 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`; \ No newline at end of file diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index c5dd3dd..88367db 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -1,46 +1,46 @@ type PostResponse = { - posts: { - id: number; - tags: string; - created_at: string; - creator_id: number; - approver_id: number; - author: string; - change: number; - source: string; - score: number; - md5: string; - file_size: number; - file_ext: string; - file_url: string; - is_shown_in_index: boolean; - preview_url: string; - preview_width: number; - preview_height: number; - actual_preview_width: number; - actual_preview_height: number; - sample_url: string; - sample_width: number; - sample_height: number; - sample_file_size: number; - jpeg_url: string; - jpeg_width: number; - jpeg_height: number; - rating: string; - is_rating_locked: boolean; - has_children: boolean; - parent_id: number; - status: string; - is_pending: boolean; - width: number; - height: number; - is_held: boolean; - frames_pending_string: string; - frames_pending: []; - frames_string: string; - frames: []; - is_note_locked: boolean; - last_noted_at: string; - last_commented_at: string; - }[]; - }; \ No newline at end of file + posts: { + id: number; + tags: string; + created_at: string; + creator_id: number; + approver_id: number; + author: string; + change: number; + source: string; + score: number; + md5: string; + file_size: number; + file_ext: string; + file_url: string; + is_shown_in_index: boolean; + preview_url: string; + preview_width: number; + preview_height: number; + actual_preview_width: number; + actual_preview_height: number; + sample_url: string; + sample_width: number; + sample_height: number; + sample_file_size: number; + jpeg_url: string; + jpeg_width: number; + jpeg_height: number; + rating: string; + is_rating_locked: boolean; + has_children: boolean; + parent_id: number; + status: string; + is_pending: boolean; + width: number; + height: number; + is_held: boolean; + frames_pending_string: string; + frames_pending: []; + frames_string: string; + frames: []; + is_note_locked: boolean; + last_noted_at: string; + last_commented_at: string; + }[]; +}; diff --git a/src/plugins/yandere/messages.ts b/src/plugins/yandere/messages.ts index 492c743..c901027 100644 --- a/src/plugins/yandere/messages.ts +++ b/src/plugins/yandere/messages.ts @@ -1,69 +1,22 @@ -import { Bot, Message } from "npm:discordeno@18.0.1"; -import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api/api.ts"; +import { Bot, DiscordEmbed, Embed, Message } from "npm:discordeno@18.0.1"; import { defaultString } from "@root/defaultString.ts"; import { logMessage } from "@root/logging.ts"; +import { drop, requestWorker } from "@root/plugins/yandere/plugin.ts"; export async function yandereMessageHandler(bot: Bot, message: Message) { const command = message.content.split(" ").slice(1).join(" "); - const args = message.content.split(" ").slice(1); - switch (command) { - case `drop`: - logMessage(message); - if ( - message.channelId === 754338073101205524n || - message.guildId === undefined - ) { - await refresh(); - bot.helpers.sendMessage(message.channelId, { - content: defaultString(await dropYandere()), - }); - } - 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; + if (command === "drop") { + bot.helpers.sendMessage(message.channelId, { + embeds: [await drop()] + }) + } else if (command.startsWith("drop [") && command.endsWith("]")) { + logMessage(message); + const generatedEmbeds: Embed[] = await requestWorker(command); + for (const embed of generatedEmbeds) { + bot.helpers.sendMessage(message.channelId, { + embeds: [embed], + }); + } } } diff --git a/src/plugins/yandere/plugin.ts b/src/plugins/yandere/plugin.ts index e69de29..6881746 100644 --- a/src/plugins/yandere/plugin.ts +++ b/src/plugins/yandere/plugin.ts @@ -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(keys: string[], key: string): key is T { + return keys.includes(key as any); +} + +export function requestParser(requestString: string) { + const res = requestString.match(/\[([\s*w+:\s*[\d+|\w+,]+)\]/g); + const map = new Map(); + 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; +} \ No newline at end of file