From 1e8f195714b23a59a61232d034d5c8998c971b0e Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Tue, 8 Apr 2025 16:27:58 +0200 Subject: [PATCH 1/6] Export requestJSON and requestRAW to core, formatting and adapting code and removing yandere code for update. --- src/plugins/rule34/api.ts | 17 +---- src/plugins/rule34/plugin.ts | 3 +- src/plugins/yandere/api.ts | 128 -------------------------------- src/plugins/yandere/api/api.ts | 5 ++ src/plugins/yandere/api/post.ts | 46 ++++++++++++ src/plugins/yandere/messages.ts | 2 +- src/plugins/yandere/plugin.ts | 0 src/plugins/yandere/test.ts | 2 +- src/structures/apiRequest.ts | 14 ++++ 9 files changed, 70 insertions(+), 147 deletions(-) delete mode 100755 src/plugins/yandere/api.ts create mode 100644 src/plugins/yandere/api/api.ts create mode 100644 src/plugins/yandere/api/post.ts create mode 100644 src/plugins/yandere/plugin.ts create mode 100644 src/structures/apiRequest.ts diff --git a/src/plugins/rule34/api.ts b/src/plugins/rule34/api.ts index 60ff63a..0538801 100755 --- a/src/plugins/rule34/api.ts +++ b/src/plugins/rule34/api.ts @@ -1,5 +1,6 @@ //TODO Add optional extensions like limit at Posts with tags etc. import * as xml_parser from "jsr:@melvdouc/xml-parser"; +import { requestRaw, requestJSON } from "@root/structures/apiRequest.ts"; export type ImageResponse = { preview_url: string; @@ -49,22 +50,6 @@ export const postUrl = new URL(`${baseUrl}/index.php?page=dapi&s=post&q=index&js const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`; const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`; -export async function requestJSON(url: string) { - const response = await requestRaw(url); - return await response.json(); -} - -async function requestRaw(url: string) { - const response = await fetch(url, { - headers: { "Accept": "application/json" }, - }); - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return response; -} -//List - // Comments export async function getPostComments(postID: number): Promise { const response = await requestRaw(`${commentsUrl}&post_id=${postID}`); diff --git a/src/plugins/rule34/plugin.ts b/src/plugins/rule34/plugin.ts index ce1b614..2b74eba 100644 --- a/src/plugins/rule34/plugin.ts +++ b/src/plugins/rule34/plugin.ts @@ -1,6 +1,7 @@ import { assert } from "@std/assert/assert"; -import { ImageResponse, postUrl, requestJSON } from "./api.ts"; +import { requestJSON } from "@root/structures/apiRequest.ts"; import { Embed, EmbedAuthor, EmbedImage } from "@root/structures/embeds.ts"; +import { ImageResponse, postUrl } from "./api.ts"; const keys = ["limit", "id", "pid", "tags"] as const; type PostKeys = typeof keys[number]; diff --git a/src/plugins/yandere/api.ts b/src/plugins/yandere/api.ts deleted file mode 100755 index 12f3215..0000000 --- a/src/plugins/yandere/api.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { assert } from "@std/assert/assert"; - -const apiUrl = "https://yande.re/post.json?api_version=2"; - -type APIResponse = { - 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; - }[]; -}; - -let page = 1; - -let baseRequest = await fetch(apiUrl, { - headers: { "Accept": "application/json" }, -}) - .then(async (response) => { - if (!response.ok) { - throw new Error("Network response was not ok"); - } - return await response.json(); - }); - -const hyperlinkarray: string[] = []; -for (const k of baseRequest.posts) { - hyperlinkarray.push(k.file_url); -} - -export async function setPage(newpage: number) { - page = newpage; - hyperlinkarray.length = 0; - await refresh(); -} - -export function getPage() { - return page; -} - -//duplicate code, also found in rule34/api.ts -export async function refresh() { - await fetch(`${apiUrl}&page=${page}`, { - headers: { "Accept": "application/json" }, - }) - .then(async (response) => { - if (!response.ok) { - throw new Error("Network response was not ok"); - } - baseRequest = await response.json(); - for (const k of baseRequest.posts) { - if (!hyperlinkarray.includes(k.file_url)) { - hyperlinkarray.push(k.file_url); - } - } - }); -} - -export async function fetchNextPage() { - page += 1; - await refresh(); -} - -export async function dropYandere() { - if (hyperlinkarray.length === 0) { - await fetchNextPage(); - return await dropYandere(); - } - return hyperlinkarray.pop()!; -} - -export async function dropYandere5() { - let tmp = ""; - for (let i = 0; i < 5; i++) { - tmp += await dropYandere() + "\n"; - } - return tmp; -} - -//Test Stack resize when fetching new data -Deno.test("Refresh Stack", async () => { - await refresh(); - const previousStack = [...hyperlinkarray]; - await new Promise((r) => setTimeout(r, 10000)); - await refresh(); - assert( - previousStack.length <= hyperlinkarray.length, - "Stack-size did not increase as expected!", - ); -}); diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts new file mode 100644 index 0000000..bf9bf6f --- /dev/null +++ b/src/plugins/yandere/api/api.ts @@ -0,0 +1,5 @@ +import { assert } from "@std/assert/assert"; +import { requestJSON, requestRaw } from "@root/structures/apiRequest.ts"; + +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 new file mode 100644 index 0000000..c5dd3dd --- /dev/null +++ b/src/plugins/yandere/api/post.ts @@ -0,0 +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 diff --git a/src/plugins/yandere/messages.ts b/src/plugins/yandere/messages.ts index 95407ca..492c743 100644 --- a/src/plugins/yandere/messages.ts +++ b/src/plugins/yandere/messages.ts @@ -1,5 +1,5 @@ import { Bot, Message } from "npm:discordeno@18.0.1"; -import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api.ts"; +import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api/api.ts"; import { defaultString } from "@root/defaultString.ts"; import { logMessage } from "@root/logging.ts"; diff --git a/src/plugins/yandere/plugin.ts b/src/plugins/yandere/plugin.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/plugins/yandere/test.ts b/src/plugins/yandere/test.ts index 9564495..2545218 100644 --- a/src/plugins/yandere/test.ts +++ b/src/plugins/yandere/test.ts @@ -4,7 +4,7 @@ import { getPage, refresh, setPage, -} from "@root/plugins/yandere/api.ts"; +} from "./api/api.ts"; import { assertEquals } from "@std/assert/equals"; //tests /* diff --git a/src/structures/apiRequest.ts b/src/structures/apiRequest.ts new file mode 100644 index 0000000..3b9a51e --- /dev/null +++ b/src/structures/apiRequest.ts @@ -0,0 +1,14 @@ +export async function requestJSON(url: string) { + const response = await requestRaw(url); + return await response.json(); +} + +export async function requestRaw(url: string) { + const response = await fetch(url, { + headers: { "Accept": "application/json" }, + }); + if (!response.ok) { + throw new Error("Network response was not ok"); + } + return response; +} \ No newline at end of file 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 2/6] 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 From aa834bda66ac77fc3cf5a7815fc3bbc3b66c864d Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Thu, 10 Apr 2025 10:57:13 +0200 Subject: [PATCH 3/6] Post-API implemented, testing required. --- src/plugins/yandere/api/post.ts | 159 +++++++++++++++++++++++--------- 1 file changed, 114 insertions(+), 45 deletions(-) diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index 88367db..84023a9 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -1,46 +1,115 @@ +import { requestJSON } from "@root/structures/apiRequest.ts"; +import { DiscordEmbed } from "npm:discordeno@18.0.1"; + 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; - }[]; -}; + 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; +}[]; + +const postSearchkeys = ["limit", "page", "tags"]; + +function parsePostListArgs(url: URL, args: string) { + const urlCopy: URL = copyObject(url); + const argarr = args.replaceAll(/(\[|\|\ ])/g, "").split(","); + for (const arg of argarr) { + const [k, v] = arg.split(":"); + if (k === undefined || v === undefined) { + throw Error( + `undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`, + ); + } + if (k in postSearchkeys) { + urlCopy.searchParams.append(k, v); + } else { + throw Error( + `unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`, + ); + } + } + return urlCopy; +} + +async function returnDiscordEmbeds( + url: URL, +): Promise { + const response: PostResponse = await requestJSON( + url.toString(), + ); + const embeds: DiscordEmbed[] = []; + for (const post of response) { + embeds.push( + { + url: post.source, + title: post.id.toString(), + author: { + name: post.author, + }, + image: { + url: post.file_url, + width: post.width, + height: post.height, + }, + }, + ); + } + return embeds; +} + +function copyObject(obj: T) { + return JSON.parse(JSON.stringify(obj)); +} + +/** + * Post-Request Handler Function, that returns DiscordEmbeds. + * @param url URL Object + * @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss. + */ +export async function handlePostRequest(url: URL, search?: string): Promise { + const urlCopy = copyObject(url); + //parse + if (search) { + return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search)); + } else { + return await returnDiscordEmbeds(urlCopy); + } +} From ab6ec7fc3d5c919d147ab3ca6a72fa3cede51366 Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Thu, 10 Apr 2025 11:37:18 +0200 Subject: [PATCH 4/6] api requests work, added simple tests... TODO: URL Object needs to be cloned for memory safety --- src/plugins/yandere/api/api.ts | 15 ++++++++++-- src/plugins/yandere/api/post.ts | 25 +++++++++++--------- src/plugins/yandere/plugin.ts | 42 --------------------------------- 3 files changed, 27 insertions(+), 55 deletions(-) diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts index 951e592..08faec0 100644 --- a/src/plugins/yandere/api/api.ts +++ b/src/plugins/yandere/api/api.ts @@ -1,4 +1,4 @@ -import { requestJSON } from "@root/structures/apiRequest.ts"; +import { handlePostRequest } from "@root/plugins/yandere/api/post.ts"; const baseURL = "https://yande.re"; const apikeys = [ @@ -52,6 +52,17 @@ function prepareURLObjectForRequest(type: APIKeys) { export function getPosts(search? : string){ const url = prepareURLObjectForRequest("post") - return requestJSON(url) + return handlePostRequest(url, search) } +Deno.test("Empty Post Request", async() => { + console.debug(await getPosts()) +}) + +Deno.test("Post Request with tag", async() => { + console.debug(await getPosts("[tags:love_live!_(series)]")) +}) + +Deno.test("Post Request with limit", async() => { + console.debug(await getPosts("[limit: 1)]")) +}) \ No newline at end of file diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index 84023a9..a671daa 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -49,24 +49,27 @@ type PostResponse = { const postSearchkeys = ["limit", "page", "tags"]; function parsePostListArgs(url: URL, args: string) { - const urlCopy: URL = copyObject(url); - const argarr = args.replaceAll(/(\[|\|\ ])/g, "").split(","); + // const urlCopy: URL = copyObject(url); + const argarr = args.replaceAll(/(\[|\ |\])/g, "").split(","); for (const arg of argarr) { const [k, v] = arg.split(":"); + if (url === undefined) throw Error("undefined Object: url!") if (k === undefined || v === undefined) { throw Error( `undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`, ); } - if (k in postSearchkeys) { - urlCopy.searchParams.append(k, v); + if (postSearchkeys.includes(k)) { + console.debug(k, v) + console.debug(JSON.stringify(url)) + url.searchParams.append(k, v); } else { throw Error( `unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`, ); } } - return urlCopy; + return url; } async function returnDiscordEmbeds( @@ -95,9 +98,9 @@ async function returnDiscordEmbeds( return embeds; } -function copyObject(obj: T) { - return JSON.parse(JSON.stringify(obj)); -} +// function copyObject(obj: T) { +// const newObject: T = Object. + /** * Post-Request Handler Function, that returns DiscordEmbeds. @@ -105,11 +108,11 @@ function copyObject(obj: T) { * @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss. */ export async function handlePostRequest(url: URL, search?: string): Promise { - const urlCopy = copyObject(url); + // const urlCopy = copyObject(url); //parse if (search) { - return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search)); + return await returnDiscordEmbeds(parsePostListArgs(url, search)); } else { - return await returnDiscordEmbeds(urlCopy); + return await returnDiscordEmbeds(url); } } diff --git a/src/plugins/yandere/plugin.ts b/src/plugins/yandere/plugin.ts index 6881746..e69de29 100644 --- a/src/plugins/yandere/plugin.ts +++ b/src/plugins/yandere/plugin.ts @@ -1,42 +0,0 @@ -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 From 0013af7c3bf43bd3b34e3a67b44ac1407cfa52fa Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Thu, 10 Apr 2025 13:26:57 +0200 Subject: [PATCH 5/6] refactor: update yandere message handling to use getPosts for embed generation and add tests --- src/plugins/yandere/api/api.ts | 4 ++++ src/plugins/yandere/messages.ts | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts index 08faec0..69400df 100644 --- a/src/plugins/yandere/api/api.ts +++ b/src/plugins/yandere/api/api.ts @@ -65,4 +65,8 @@ Deno.test("Post Request with tag", async() => { Deno.test("Post Request with limit", async() => { console.debug(await getPosts("[limit: 1)]")) +}) + +Deno.test("Post Request with page", async() => { + console.debug(await getPosts("[page: 30)]")) }) \ No newline at end of file diff --git a/src/plugins/yandere/messages.ts b/src/plugins/yandere/messages.ts index c901027..45dfecb 100644 --- a/src/plugins/yandere/messages.ts +++ b/src/plugins/yandere/messages.ts @@ -1,22 +1,28 @@ -import { Bot, DiscordEmbed, Embed, Message } from "npm:discordeno@18.0.1"; +import { Bot, 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"; +import { getPosts } from "@root/plugins/yandere/api/api.ts"; export async function yandereMessageHandler(bot: Bot, message: Message) { const command = message.content.split(" ").slice(1).join(" "); - + const args = command.match(/(\[\.+\])/g) if (command === "drop") { + const embed = await getPosts("[limit: 1]") bot.helpers.sendMessage(message.channelId, { - embeds: [await drop()] + embeds: [embed as Embed] }) } else if (command.startsWith("drop [") && command.endsWith("]")) { logMessage(message); - const generatedEmbeds: Embed[] = await requestWorker(command); + if (args === null) throw Error("args is null") + const generatedEmbeds = await getPosts(args[0]); for (const embed of generatedEmbeds) { bot.helpers.sendMessage(message.channelId, { - embeds: [embed], + embeds: [embed as Embed], }); } } } + +Deno.test("Test Bot Drop", async() => { + +}) \ No newline at end of file From 098ffd8f141531709321d6263a11ca91c2cfa33c Mon Sep 17 00:00:00 2001 From: fzzin <43480857+fzzinchemical@users.noreply.github.com> Date: Thu, 10 Apr 2025 16:35:58 +0200 Subject: [PATCH 6/6] remove unnecessary test --- src/plugins/yandere/messages.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/plugins/yandere/messages.ts b/src/plugins/yandere/messages.ts index 45dfecb..dbd35d2 100644 --- a/src/plugins/yandere/messages.ts +++ b/src/plugins/yandere/messages.ts @@ -2,6 +2,7 @@ import { Bot, Embed, Message } from "npm:discordeno@18.0.1"; import { defaultString } from "@root/defaultString.ts"; import { logMessage } from "@root/logging.ts"; import { getPosts } from "@root/plugins/yandere/api/api.ts"; +import { debug } from "node:console"; export async function yandereMessageHandler(bot: Bot, message: Message) { const command = message.content.split(" ").slice(1).join(" "); @@ -21,8 +22,4 @@ export async function yandereMessageHandler(bot: Bot, message: Message) { }); } } -} - -Deno.test("Test Bot Drop", async() => { - -}) \ No newline at end of file +} \ No newline at end of file