From 2fcc67dbd7ae2d50ff7fd2ff714049f54e7545ec Mon Sep 17 00:00:00 2001 From: fzzinchemical Date: Tue, 1 Apr 2025 22:54:49 +0200 Subject: [PATCH] feat: implement embed message handling and create Embed structure for improved message formatting --- src/plugins/rule34/messages.ts | 12 ++++--- src/plugins/rule34/plugin.ts | 15 ++++++-- src/structures/embeds.ts | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/structures/embeds.ts diff --git a/src/plugins/rule34/messages.ts b/src/plugins/rule34/messages.ts index 6d0e724..6d5e7b7 100644 --- a/src/plugins/rule34/messages.ts +++ b/src/plugins/rule34/messages.ts @@ -1,16 +1,20 @@ -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 { logMessage } from "@root/logging.ts"; import { defaultString } from "@root/defaultString.ts"; +import { Embed } from "@root/structures/embeds.ts"; export async function rule34MessageHandler(bot: Bot, message: Message) { const command = message.content.trim().split(" ").slice(1).join(" "); if (command.startsWith("[") && command.endsWith("]")) { logMessage(message); - bot.helpers.sendMessage(message.channelId, { - content: defaultString(await requestWorker(command)), - }); + const generatedEmbeds: Embed[] = await requestWorker(command) + for (const embed of generatedEmbeds) { + bot.helpers.sendMessage(message.channelId, { + embeds: [embed] + }); + } } else if (command === "help") { logMessage(message); bot.helpers.sendMessage(message.channelId, { diff --git a/src/plugins/rule34/plugin.ts b/src/plugins/rule34/plugin.ts index 03d97a7..8800475 100644 --- a/src/plugins/rule34/plugin.ts +++ b/src/plugins/rule34/plugin.ts @@ -1,5 +1,7 @@ import { assert } from "@std/assert/assert"; import {requestJSON, postUrl, ImageResponse} from "./api.ts" +import { Embed, EmbedAuthor, EmbedField, EmbedImage, Field } from "@root/structures/embeds.ts"; +import { DiscordEmbedAuthor, DiscordEmbedImage } from "npm:discordeno@18.0.1"; const keys = ["limit" , "id" , "pid" , "tags"] as const type PostKeys = typeof keys[number] @@ -16,11 +18,17 @@ export async function drop() { export async function requestWorker(requestString: string){ const response = await requestJSON(generateRequestURL(requestString)) - const stack: string[] = [] + const stack: Embed[] = [] for (const img of response) { - stack.push(img.file_url) + stack.push(new Embed( + `ID: ${img.id}`, + img.file_url, + new EmbedAuthor(img.owner), + [new EmbedField("Tags", img.tags, true)], + new EmbedImage(img.preview_url, img.height, img.width) + )) } - return stack.join("\n") + return stack } //TODO: get help in form of a nice beautiful format @@ -65,6 +73,7 @@ export function generateRequestURL(requestString: string) { return postCpy.href.toString().replaceAll("%2B", '+') } + Deno.test("Test Request Parser", () => { assert(requestParser("[limit: 12,tags:bro+likes+bread]"), '{ "limit" => "12", "tags" => "bro+likes+bread" }') }) diff --git a/src/structures/embeds.ts b/src/structures/embeds.ts new file mode 100644 index 0000000..8198a98 --- /dev/null +++ b/src/structures/embeds.ts @@ -0,0 +1,66 @@ +import { DiscordEmbed, DiscordEmbedAuthor, DiscordEmbedField, DiscordEmbedImage } from "npm:discordeno@18.0.1"; + +export type Field = {name: string, value: string} + +export class Embed implements DiscordEmbed{ + title: string + url: string + author: DiscordEmbedAuthor + fields: Field[] + image: DiscordEmbedImage + // timestamp: Time + + constructor( + title: string, + url: string, + author:DiscordEmbedAuthor, + fields: EmbedField[], + image: DiscordEmbedImage, + ){ + this.title = title + this.url = url + this.author = author + this.fields= fields + this.image = image + + } + toJSON() { + return { + title: this.title, + url : this.url, + author: this.author, + fields: this.fields, + image: this.image + } + } +} + +export class EmbedImage implements DiscordEmbedImage { + url: string + height: number + width: number + constructor(url: string, height: number, width : number){ + this.url = url + this.height = height + this.width = width + } +} + +export class EmbedAuthor implements DiscordEmbedAuthor { + name: string + constructor(name: string){ + this.name = name + } +} + +export class EmbedField implements DiscordEmbedField { + name: string + value: string + inline: boolean + + constructor(name: string, value: string, inline: boolean) { + this.name = name + this.value = value + this.inline = inline + } +} \ No newline at end of file