feat(Urban Dictionary): Chooses top rated definition & more results. (#2080)

This commit is contained in:
jd 2023-12-31 02:06:19 +02:00 committed by GitHub
parent 5dee2e8549
commit 30bc979c8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View file

@ -0,0 +1,13 @@
# Urban Dictionary
Use /urban slash command to search for a definition for a word on [Urban Dictionary](https://www.urbandictionary.com/).
## Preview
![preview](https://i.imgur.com/1zwzj38.png)
## Usage
- Enable this plugin
- Set plugin settings as desired
- Type /urban and start getting definitions right into your Discord client.

View file

@ -18,14 +18,24 @@
import { ApplicationCommandOptionType, sendBotMessage } from "@api/Commands";
import { ApplicationCommandInputType } from "@api/Commands/types";
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import definePlugin, { OptionType } from "@utils/types";
const settings = definePluginSettings({
resultsAmount: {
type: OptionType.NUMBER,
description: "The amount of results you want to get (more gives better results, but is slower)",
default: 10
}
});
export default definePlugin({
name: "UrbanDictionary",
description: "Search for a word on Urban Dictionary via /urban slash command",
authors: [Devs.jewdev],
dependencies: ["CommandsAPI"],
settings,
commands: [
{
name: "urban",
@ -41,12 +51,16 @@ export default definePlugin({
],
execute: async (args, ctx) => {
try {
const query = encodeURIComponent(args[0].value);
const { list: [definition] } = await (await fetch(`https://api.urbandictionary.com/v0/define?term=${query}`)).json();
const query: string = encodeURIComponent(args[0].value);
const { list } = await fetch(`https://api.urbandictionary.com/v0/define?term=${query}&per_page=${settings.store.resultsAmount}`).then(response => response.json());
if (!definition)
if (!list.length)
return void sendBotMessage(ctx.channel.id, { content: "No results found." });
const definition = list.reduce((prev, curr) => {
return prev.thumbs_up > curr.thumbs_up ? prev : curr;
});
const linkify = (text: string) => text
.replaceAll("\r\n", "\n")
.replace(/([*>_`~\\])/gsi, "\\$1")