|
| 1 | +import { ApplyOptions } from '@sapphire/decorators' |
| 2 | +import type { Command } from '@sapphire/framework' |
| 3 | +import { applyLocalizedBuilder, resolveKey } from '@sapphire/plugin-i18next' |
| 4 | +import type { Subcommand } from '@sapphire/plugin-subcommands' |
| 5 | +import { MessageFlags, type SlashCommandStringOption } from 'discord.js' |
| 6 | +import { TracedSubcommand } from '../../structures/subcommand.mjs' |
| 7 | +import { |
| 8 | + BOORU_QUERY_MAX_LENGTH, |
| 9 | + type BooruSiteName, |
| 10 | + booruSites, |
| 11 | + buildBooruPage, |
| 12 | + channelAllowsNsfw, |
| 13 | + isBooruSiteName, |
| 14 | +} from '../../utils/booru.mjs' |
| 15 | + |
| 16 | +@ApplyOptions<Subcommand.Options>({ |
| 17 | + cooldownDelay: 3_000, |
| 18 | + subcommands: [ |
| 19 | + { name: 'e621', chatInputRun: 'chatInputE621' }, |
| 20 | + { name: 'rule34', chatInputRun: 'chatInputRule34' }, |
| 21 | + { name: 'derpibooru', chatInputRun: 'chatInputDerpibooru' }, |
| 22 | + ], |
| 23 | +}) |
| 24 | +export class BooruCommand extends TracedSubcommand { |
| 25 | + public override registerApplicationCommands(registry: Subcommand.Registry) { |
| 26 | + const queryOption = (option: SlashCommandStringOption) => |
| 27 | + applyLocalizedBuilder( |
| 28 | + option, |
| 29 | + 'commands/names:booruOptionQuery', |
| 30 | + 'commands/descriptions:booruOptionQuery', |
| 31 | + ) |
| 32 | + .setRequired(true) |
| 33 | + .setMaxLength(BOORU_QUERY_MAX_LENGTH) |
| 34 | + .setAutocomplete(true) |
| 35 | + |
| 36 | + registry.registerChatInputCommand( |
| 37 | + (builder) => { |
| 38 | + applyLocalizedBuilder( |
| 39 | + builder, |
| 40 | + 'commands/names:booru', |
| 41 | + 'commands/descriptions:booru', |
| 42 | + ) |
| 43 | + .addSubcommand((sub) => |
| 44 | + applyLocalizedBuilder( |
| 45 | + sub, |
| 46 | + 'commands/names:booruE621', |
| 47 | + 'commands/descriptions:booruE621', |
| 48 | + ).addStringOption(queryOption), |
| 49 | + ) |
| 50 | + .addSubcommand((sub) => |
| 51 | + applyLocalizedBuilder( |
| 52 | + sub, |
| 53 | + 'commands/names:booruRule34', |
| 54 | + 'commands/descriptions:booruRule34', |
| 55 | + ).addStringOption(queryOption), |
| 56 | + ) |
| 57 | + .addSubcommand((sub) => |
| 58 | + applyLocalizedBuilder( |
| 59 | + sub, |
| 60 | + 'commands/names:booruDerpibooru', |
| 61 | + 'commands/descriptions:booruDerpibooru', |
| 62 | + ).addStringOption(queryOption), |
| 63 | + ) |
| 64 | + }, |
| 65 | + { |
| 66 | + guildIds: ['1034462346908794910'], |
| 67 | + }, |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + public override async autocompleteRun( |
| 72 | + interaction: Command.AutocompleteInteraction, |
| 73 | + ) { |
| 74 | + const site = interaction.options.getSubcommand(false) |
| 75 | + if (!site || !isBooruSiteName(site)) { |
| 76 | + return interaction.respond([]) |
| 77 | + } |
| 78 | + |
| 79 | + // Complete the word being typed and keep the already-entered tags in |
| 80 | + // front, so multi-tag queries stay autocompletable. |
| 81 | + const chunks = interaction.options.getFocused().split(' ') |
| 82 | + const term = chunks.pop() ?? '' |
| 83 | + if (term.length === 0) { |
| 84 | + return interaction.respond([]) |
| 85 | + } |
| 86 | + |
| 87 | + const hits = await booruSites[site] |
| 88 | + .autocomplete(term, channelAllowsNsfw(interaction)) |
| 89 | + .catch(() => []) |
| 90 | + return interaction.respond( |
| 91 | + hits |
| 92 | + .map((hit) => [...chunks, hit].join(' ').trim()) |
| 93 | + .filter( |
| 94 | + (value) => value.length > 0 && value.length <= BOORU_QUERY_MAX_LENGTH, |
| 95 | + ) |
| 96 | + .slice(0, 25) |
| 97 | + .map((value) => ({ name: value, value })), |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + public chatInputE621(interaction: Subcommand.ChatInputCommandInteraction) { |
| 102 | + return this.runSite(interaction, 'e621') |
| 103 | + } |
| 104 | + |
| 105 | + public chatInputRule34(interaction: Subcommand.ChatInputCommandInteraction) { |
| 106 | + return this.runSite(interaction, 'rule34') |
| 107 | + } |
| 108 | + |
| 109 | + public chatInputDerpibooru( |
| 110 | + interaction: Subcommand.ChatInputCommandInteraction, |
| 111 | + ) { |
| 112 | + return this.runSite(interaction, 'derpibooru') |
| 113 | + } |
| 114 | + |
| 115 | + private async runSite( |
| 116 | + interaction: Subcommand.ChatInputCommandInteraction, |
| 117 | + site: BooruSiteName, |
| 118 | + ) { |
| 119 | + const nsfwAllowed = channelAllowsNsfw(interaction) |
| 120 | + if (booruSites[site].gated && !nsfwAllowed) { |
| 121 | + return interaction.reply({ |
| 122 | + content: (await resolveKey( |
| 123 | + interaction, |
| 124 | + 'commands/common:nsfwDisabled', |
| 125 | + )) as string, |
| 126 | + flags: MessageFlags.Ephemeral, |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + const query = interaction.options.getString('query', true) |
| 131 | + await interaction.deferReply() |
| 132 | + |
| 133 | + const posts = await booruSites[site].search(query, nsfwAllowed) |
| 134 | + if (posts.length === 0) { |
| 135 | + return interaction.editReply( |
| 136 | + (await resolveKey(interaction, 'commands/common:noResultsFor', { |
| 137 | + query, |
| 138 | + })) as string, |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + return interaction.editReply( |
| 143 | + await buildBooruPage(interaction, site, query, posts, 0), |
| 144 | + ) |
| 145 | + } |
| 146 | +} |
0 commit comments