diff --git a/src/plugins/urbanDictionary.ts b/src/plugins/urbanDictionary.ts index 0adf8a5b..840fe5cb 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}\``, }); } }