Urban Dictionary: better embed (#1127)

Co-authored-by: V <vendicated@riseup.net>
This commit is contained in:
Hunter 2023-05-16 22:40:40 -04:00 committed by GitHub
parent 1d6b78f6c6
commit c2a1c4cbf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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}\``,
});
}
}