50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { requestJSON } from "@root/structures/apiRequest.ts";
|
|
import { ImageResponse, postUrl } from "./api.ts";
|
|
import { EmbedBuilder } from "@discordjs";
|
|
|
|
export type PostRequestParameters = {
|
|
limit?: string| null| undefined,
|
|
id?: string| null| undefined,
|
|
pid?: string| null| undefined,
|
|
tags?: string| null| undefined,
|
|
page?: string| null| undefined,
|
|
}
|
|
|
|
export async function requestWorker(postRequestParams: PostRequestParameters) {
|
|
const embeds = [];
|
|
try {
|
|
const response = await requestJSON<ImageResponse[]>(
|
|
generateRequestURL(postRequestParams),
|
|
);
|
|
for (const post of response) {
|
|
embeds.push(
|
|
new EmbedBuilder()
|
|
.setTitle(post.id.toString())
|
|
.setURL(post.file_url)
|
|
.setAuthor(
|
|
{
|
|
name: post.owner,
|
|
},
|
|
)
|
|
.setImage(post.preview_url),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
console.error((e as Error).message);
|
|
return [];
|
|
}
|
|
return embeds;
|
|
}
|
|
|
|
export function generateRequestURL(postRequestParams: PostRequestParameters) {
|
|
const postCpy = new URL(postUrl);
|
|
const parameterStrings: string[] = []
|
|
|
|
Object.entries(postRequestParams).forEach(([k, v]) => {
|
|
if (v !== null) {
|
|
parameterStrings.push(`&${k}=${v}`)
|
|
}
|
|
});
|
|
parameterStrings.push('&api_key=&user_id=' + (Deno.env.get("RULE34_API_KEY") ?? ''));
|
|
return postCpy.href + parameterStrings.join('');
|
|
} |