From 8df50c8c8fdb1c45f366fd125aca7a524ebf9944 Mon Sep 17 00:00:00 2001 From: fzzinchemical Date: Tue, 15 Apr 2025 17:11:37 +0200 Subject: [PATCH] Yande.re API fully functional --- src/plugins/yandere/api/api.ts | 46 ++++++++------------------- src/plugins/yandere/api/post.ts | 56 +++++++++++---------------------- src/plugins/yandere/commands.ts | 43 ++++++++++++++++--------- 3 files changed, 61 insertions(+), 84 deletions(-) diff --git a/src/plugins/yandere/api/api.ts b/src/plugins/yandere/api/api.ts index 15d705e..2debca6 100644 --- a/src/plugins/yandere/api/api.ts +++ b/src/plugins/yandere/api/api.ts @@ -1,4 +1,4 @@ -import { handlePostRequest } from "@root/plugins/yandere/api/post.ts"; +import { handlePostRequest, postKeys } from "@root/plugins/yandere/api/post.ts"; const baseURL = "https://yande.re"; const apikeys = [ @@ -17,40 +17,20 @@ 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 "); + 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 handlePostRequest(url, search) +export async function getPosts(args? : postKeys){ + return await handlePostRequest(prepareURLObjectForRequest("post"), args) } \ No newline at end of file diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index b37fa3f..d967825 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -46,29 +46,20 @@ type PostResponse = { last_commented_at: string; }[]; -const postSearchkeys = ["limit", "page", "tags"]; +export type postKeys = { + limit?: string | null | undefined; + page?: string | null | undefined; + tags?: string | null | undefined; +}; -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 (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}`, - ); +function parsePostListArgs(url: URL, args: postKeys | undefined) { + if (args === undefined) args = { limit: "1" }; //TODO that could be better + Object.entries(args).forEach(([key, value]) => { + if (value !== null) { + console.debug(`appending k:${key}, v:${value}`); + url.searchParams.append(key, value); } - 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 url; } @@ -87,29 +78,20 @@ async function returnDiscordEmbeds( .setAuthor( { name: post.author, - } + }, ) - .setImage(post.file_url) - ) + .setImage(post.preview_url), + ); } return embeds; } -// function copyObject(obj: T) { -// const newObject: T = Object. - - /** * 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. + * @param args Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss. */ -export async function handlePostRequest(url: URL, search?: string) { - // const urlCopy = copyObject(url); +export async function handlePostRequest(url: URL, args?: postKeys) { //parse - if (search) { - return await returnDiscordEmbeds(parsePostListArgs(url, search)); - } else { - return await returnDiscordEmbeds(url); - } -} \ No newline at end of file + return await returnDiscordEmbeds(parsePostListArgs(url, args)); +} diff --git a/src/plugins/yandere/commands.ts b/src/plugins/yandere/commands.ts index 56e47e7..e97aac0 100644 --- a/src/plugins/yandere/commands.ts +++ b/src/plugins/yandere/commands.ts @@ -1,7 +1,4 @@ -import { - ChatInputCommandInteraction, - SlashCommandBuilder, -} from "@discordjs"; +import { ChatInputCommandInteraction, SlashCommandBuilder } from "@discordjs"; import { getPosts } from "@root/plugins/yandere/api/api.ts"; // export async function yandereMessageHandler(message: Message) { @@ -34,25 +31,43 @@ export const commands = [ .addSubcommand((subcommand) => subcommand .setName("drop") - .setDescription("Drops one or multiple images using the yande.re API.") - .addStringOption(option => - option - .setName("limit") - .setDescription("Post limitation") + .setDescription( + "Drops one or multiple images using the yande.re API.", ) - .addStringOption(option => + .addStringOption((option) => option + .setName("limit") + .setDescription("Post limitation") + ) + .addStringOption((option) => option .setName("tags") - .setDescription("Tags to add to query") + .setDescription("Tags to add to query") ) - .addStringOption(option => + .addStringOption((option) => option .setName("page") - .setDescription("Which page to query") + .setDescription("Which page to query") ) ), async execute(interaction: ChatInputCommandInteraction) { - await interaction.reply({embeds: await getPosts("[limit: 5]")}); + const limit = interaction.options.getString("limit") + const tags = interaction.options.getString("tags") + const page = interaction.options.getString("page") + if (limit === null && tags === null && page === null) { + await interaction.reply({ + embeds: await getPosts({ + limit: "1" + }) + }) + } else { + await interaction.reply({ + embeds: await getPosts({ + limit: limit, + tags: tags, + page: page + }), + }) + } }, }, ];