|
| 1 | +import { |
| 2 | + type ChatInputCommandInteraction, |
| 3 | + SlashCommandBuilder, |
| 4 | +} from "discord.js"; |
| 5 | +import cowsay from "cowsay"; |
| 6 | +import textwrap from "@barudakrosul/textwrap"; |
| 7 | + |
| 8 | +const DEFAULT_WRAP_WIDTH = 40; |
| 9 | + |
| 10 | +export const data = new SlashCommandBuilder() |
| 11 | + .setName("cowsay") |
| 12 | + .setDescription("Make a cow say it") |
| 13 | + .addStringOption((option) => |
| 14 | + option |
| 15 | + .setName("text") |
| 16 | + .setDescription("The text to say") |
| 17 | + .setRequired(true), |
| 18 | + ) |
| 19 | + .addStringOption((option) => |
| 20 | + option |
| 21 | + .setName("cow") |
| 22 | + .setDescription("The cow design to use") |
| 23 | + .setRequired(false), |
| 24 | + ) |
| 25 | + .addIntegerOption((option) => |
| 26 | + option |
| 27 | + .setName("wrap_width") |
| 28 | + .setDescription( |
| 29 | + "Width at which to wrap the input text (0 to disable)", |
| 30 | + ) |
| 31 | + .setRequired(false), |
| 32 | + ); |
| 33 | + |
| 34 | +export async function command(interaction: ChatInputCommandInteraction) { |
| 35 | + const { options } = interaction; |
| 36 | + |
| 37 | + const text = options.getString("text", true); |
| 38 | + const cow = options.getString("cow"); |
| 39 | + const wrapWidth = options.getInteger("wrap_width") ?? DEFAULT_WRAP_WIDTH; |
| 40 | + |
| 41 | + const isChannel = !!interaction.channel; |
| 42 | + |
| 43 | + if (!isChannel) { |
| 44 | + await interaction.reply({ |
| 45 | + content: "This command can only be used in a channel", |
| 46 | + ephemeral: true, |
| 47 | + }); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (text === "!list") { |
| 52 | + for (const chunk of await getCowListResponseContent()) { |
| 53 | + await interaction.reply({ |
| 54 | + content: chunk, |
| 55 | + ephemeral: true, |
| 56 | + }); |
| 57 | + } |
| 58 | + } else { |
| 59 | + await interaction.reply({ |
| 60 | + content: getCowsayResponseContent(text, cow, wrapWidth), |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function getCowsayResponseContent( |
| 66 | + text: string, |
| 67 | + cow: string | null, |
| 68 | + wrapWidth: number, |
| 69 | +): string { |
| 70 | + const wrappedText = |
| 71 | + wrapWidth > 0 ? textwrap.wrap(text, wrapWidth).join("\n") : text; |
| 72 | + const cowsaid = cowsay.say({ |
| 73 | + text: wrappedText, |
| 74 | + f: cow ?? undefined, |
| 75 | + }); |
| 76 | + return "```\n" + cowsaid + "\n```"; |
| 77 | +} |
| 78 | + |
| 79 | +async function getCowListResponseContent(): Promise<string[]> { |
| 80 | + try { |
| 81 | + const filenames = await cowsay.list(() => {}); |
| 82 | + const cows = filenames.map((name) => name.replace(/\.cow$/, "")); |
| 83 | + const chunks = ["```\n"]; |
| 84 | + for (const cow of cows) { |
| 85 | + if (chunks[chunks.length - 1].length + cow.length + 1 + 3 > 2000) { |
| 86 | + chunks[chunks.length - 1] += "```"; |
| 87 | + chunks.push("```\n"); |
| 88 | + } |
| 89 | + chunks[chunks.length - 1] += cow + "\n"; |
| 90 | + } |
| 91 | + chunks[chunks.length - 1] += "```"; |
| 92 | + return chunks; |
| 93 | + } catch (e) { |
| 94 | + const msg = (e as Error).message; |
| 95 | + console.error(`Error listing cows: ${msg}`); |
| 96 | + return ["Error listing cows"]; |
| 97 | + } |
| 98 | +} |
0 commit comments