refactor: streamline API request handling and enhance message processing
This commit is contained in:
@@ -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`;
|
||||
@@ -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`:
|
||||
if (command === "drop") {
|
||||
bot.helpers.sendMessage(message.channelId, {
|
||||
embeds: [await drop()]
|
||||
})
|
||||
} else if (command.startsWith("drop [") && command.endsWith("]")) {
|
||||
logMessage(message);
|
||||
if (
|
||||
message.channelId === 754338073101205524n ||
|
||||
message.guildId === undefined
|
||||
) {
|
||||
await refresh();
|
||||
const generatedEmbeds: Embed[] = await requestWorker(command);
|
||||
for (const embed of generatedEmbeds) {
|
||||
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],
|
||||
embeds: [embed],
|
||||
});
|
||||
}
|
||||
}
|
||||
break;
|
||||
case `getpage`:
|
||||
logMessage(message);
|
||||
if (
|
||||
message.channelId === 754338073101205524n ||
|
||||
message.guildId === undefined
|
||||
) {
|
||||
bot.helpers.sendMessage(message.channelId, {
|
||||
content: "Page is " + getPage(),
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user