From c2a1c4cbf624486ebfc0e907bd0c9c43e4b6a579 Mon Sep 17 00:00:00 2001 From: Hunter <70922563+HunterAPI@users.noreply.github.com> Date: Tue, 16 May 2023 22:40:40 -0400 Subject: Urban Dictionary: better embed (#1127) Co-authored-by: V --- src/plugins/urbanDictionary.ts | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/plugins/urbanDictionary.ts') diff --git a/src/plugins/urbanDictionary.ts b/src/plugins/urbanDictionary.ts index 0adf8a5..840fe5c 100644 --- a/src/plugins/urbanDictionary.ts +++ b/src/plugins/urbanDictionary.ts @@ -41,37 +41,48 @@ export default definePlugin({ ], execute: async (args, ctx) => { try { - const { list: [definition] } = await (await fetch(`https://api.urbandictionary.com/v0/define?term=${args[0].value}`)).json(); + const query = encodeURIComponent(args[0].value); + const { list: [definition] } = await (await fetch(`https://api.urbandictionary.com/v0/define?term=${query}`)).json(); if (!definition) return void sendBotMessage(ctx.channel.id, { content: "No results found." }); - const linkify = text => text.replace(/\[(.+?)\]/g, (_, word) => `[${word}](https://www.urbandictionary.com/define.php?term=${encodeURIComponent(word)})`); + const linkify = (text: string) => text + .replaceAll("\r\n", "\n") + .replace(/([*>_`~\\])/gsi, "\\$1") + .replace(/\[(.+?)\]/g, (_, word) => `[${word}](https://www.urbandictionary.com/define.php?term=${encodeURIComponent(word)} "Define '${word}' on Urban Dictionary")`) + .trim(); return void sendBotMessage(ctx.channel.id, { embeds: [ { type: "rich", author: { - name: `Definition of ${definition.word}`, - url: definition.permalink + name: `Uploaded by "${definition.author}"`, + url: `https://www.urbandictionary.com/author.php?author=${encodeURIComponent(definition.author)}`, }, + title: definition.word, + url: `https://www.urbandictionary.com/define.php?term=${encodeURIComponent(definition.word)}`, description: linkify(definition.definition), fields: [ { name: "Example", - value: linkify(definition.example) - } + value: linkify(definition.example), + }, + { + name: "Want more definitions?", + value: `Check out [more definitions](https://www.urbandictionary.com/define.php?term=${query} "Define "${args[0].value}" on Urban Dictionary") on Urban Dictionary.`, + }, ], color: 0xFF9900, - footer: { text: `👍 ${definition.thumbs_up.toString()} | 👎 ${definition.thumbs_down.toString()} | Uploaded by ${definition.author}`, icon_url: "https://www.urbandictionary.com/favicon.ico" }, - timestamp: new Date(definition.written_on).toISOString() - } - ] as any + footer: { text: `👍 ${definition.thumbs_up.toString()} | 👎 ${definition.thumbs_down.toString()}`, icon_url: "https://www.urbandictionary.com/favicon.ico" }, + timestamp: new Date(definition.written_on).toISOString(), + }, + ] as any, }); } catch (error) { - return void sendBotMessage(ctx.channel.id, { - content: `Something went wrong: \`${error}\`` + sendBotMessage(ctx.channel.id, { + content: `Something went wrong: \`${error}\``, }); } } -- cgit