feat: enhance rule34 message handling with improved error handling and command processing
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Bot, Message} from "npm:discordeno@18.0.1";
|
import { Bot, Message } from "npm:discordeno@18.0.1";
|
||||||
import { drop, help, requestWorker } from "./plugin.ts";
|
import { drop, help, requestWorker } from "./plugin.ts";
|
||||||
import { logMessage } from "@root/logging.ts";
|
import { logMessage } from "@root/logging.ts";
|
||||||
import { defaultString } from "@root/defaultString.ts";
|
import { defaultString } from "@root/defaultString.ts";
|
||||||
@@ -6,13 +6,16 @@ import { Embed } from "@root/structures/embeds.ts";
|
|||||||
|
|
||||||
export async function rule34MessageHandler(bot: Bot, message: Message) {
|
export async function rule34MessageHandler(bot: Bot, message: Message) {
|
||||||
const command = message.content.trim().split(" ").slice(1).join(" ");
|
const command = message.content.trim().split(" ").slice(1).join(" ");
|
||||||
|
if (command === "drop") {
|
||||||
if (command.startsWith("[") && command.endsWith("]")) {
|
bot.helpers.sendMessage(message.channelId, {
|
||||||
|
embeds: [await drop()]
|
||||||
|
})
|
||||||
|
} else if (command.startsWith("drop [") && command.endsWith("]")) {
|
||||||
logMessage(message);
|
logMessage(message);
|
||||||
const generatedEmbeds: Embed[] = await requestWorker(command)
|
const generatedEmbeds: Embed[] = await requestWorker(command);
|
||||||
for (const embed of generatedEmbeds) {
|
for (const embed of generatedEmbeds) {
|
||||||
bot.helpers.sendMessage(message.channelId, {
|
bot.helpers.sendMessage(message.channelId, {
|
||||||
embeds: [embed]
|
embeds: [embed],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (command === "help") {
|
} else if (command === "help") {
|
||||||
|
|||||||
@@ -10,9 +10,30 @@ function isKey(key: string): key is PostKeys {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function drop() {
|
export async function drop() {
|
||||||
|
try {
|
||||||
const response = await requestJSON<ImageResponse[]>(`${postUrl}&limit=1`);
|
const response = await requestJSON<ImageResponse[]>(`${postUrl}&limit=1`);
|
||||||
if (response[0] === undefined) throw Error("Bro, get some sleep");
|
const img = response[0];
|
||||||
return response[0].file_url;
|
if (img === undefined) throw Error("An undefined Array was given!");
|
||||||
|
return new Embed(
|
||||||
|
`ID: ${img.id}`,
|
||||||
|
img.sample_url,
|
||||||
|
new EmbedAuthor(img.owner),
|
||||||
|
// [new EmbedField("Tags", img.tags, true)],
|
||||||
|
new EmbedImage(img.file_url, img.height, img.width),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.error((e as Error).message);
|
||||||
|
return new Embed(
|
||||||
|
"Error Message",
|
||||||
|
"https://gifdb.com/images/thumbnail/nuh-uh-demon-slayer-girl-hm7q3hqa4lnqyl6d.gif",
|
||||||
|
new EmbedAuthor("fzzinChemical"),
|
||||||
|
new EmbedImage(
|
||||||
|
"https://gifdb.com/images/thumbnail/nuh-uh-demon-slayer-girl-hm7q3hqa4lnqyl6d.gif",
|
||||||
|
250,
|
||||||
|
250,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requestWorker(requestString: string) {
|
export async function requestWorker(requestString: string) {
|
||||||
@@ -35,7 +56,7 @@ export async function requestWorker(requestString: string) {
|
|||||||
return stack;
|
return stack;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error((e as Error).message);
|
console.error((e as Error).message);
|
||||||
return []
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user