48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { registerCommand } from "@vendetta/commands";
|
|
import { findByProps } from "@vendetta/metro";
|
|
import { showToast } from "@vendetta/ui/toasts";
|
|
import { fetchDefinition } from "./lib/urbanDictionary";
|
|
|
|
const STRING = 3;
|
|
|
|
// Same lesson as Message Snippets: returning a CommandResult and relying on the framework to
|
|
// auto-send it is unreliable in practice, so this sends the message directly instead.
|
|
const MessageActions = findByProps("sendMessage", "sendBotMessage");
|
|
|
|
export default function loadCommands(): (() => void)[] {
|
|
const unregister = registerCommand({
|
|
name: "urban",
|
|
description: "Look up a term on Urban Dictionary",
|
|
options: [{ name: "term", description: "What to look up", type: STRING, required: true }],
|
|
execute: async (args, ctx) => {
|
|
const term = args.find((a) => a.name === "term")?.value;
|
|
if (!term) return;
|
|
|
|
let definition;
|
|
try {
|
|
definition = await fetchDefinition(term);
|
|
} catch {
|
|
showToast(`Couldn't reach Urban Dictionary for "${term}"`, undefined);
|
|
return;
|
|
}
|
|
|
|
if (!definition) {
|
|
showToast(`No definition found for "${term}"`, undefined);
|
|
return;
|
|
}
|
|
|
|
if (!MessageActions?.sendMessage) {
|
|
showToast("Couldn't send the definition - message action not found", undefined);
|
|
return;
|
|
}
|
|
|
|
const content =
|
|
`**${definition.word}** (👍 ${definition.thumbsUp} 👎 ${definition.thumbsDown})\n${definition.definition}` +
|
|
(definition.example ? `\n\n*${definition.example}*` : "");
|
|
|
|
MessageActions.sendMessage(ctx.channel.id, { content });
|
|
}
|
|
});
|
|
|
|
return [unregister];
|
|
}
|