From 7b707ec557e14f799c9e392e56625fd7644b067a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Mon, 10 Mar 2025 18:36:32 +0100 Subject: [PATCH] feat: Add application emoji search --- package.json | 3 ++- src/command.ts | 5 ++++- src/commands/superfakenitro.ts | 33 ++++++++++++++++++++++++--------- src/config.ts | 2 +- src/index.ts | 25 ++++++++++++++++++++----- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 8239cfc..2097584 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "type": "module", "main": "dist/index.js", "scripts": { - "start": "node --no-warnings=ExperimentalWarning --loader ts-node/esm src/index.ts" + "start": "node --no-warnings=ExperimentalWarning --loader ts-node/esm src/index.ts", + "check": "tsc" }, "dependencies": { "discord.js": "^14.17.2", diff --git a/src/command.ts b/src/command.ts index 399f575..e1aca63 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,7 +1,10 @@ -import {ChatInputCommandInteraction, SharedSlashCommand} from "discord.js"; +import { AutocompleteFocusedOption, AutocompleteInteraction, ChatInputCommandInteraction, SharedSlashCommand } from "discord.js"; import { Config } from "./config"; export abstract class Command { abstract run(interaction: ChatInputCommandInteraction, config: Config): Promise; + autoComplete(interaction: AutocompleteInteraction, config: Config, option: AutocompleteFocusedOption) : Promise { + throw new Error("Autocompletion called on command that does not have #autoComplete implemented."); + } abstract slashCommand: SharedSlashCommand } diff --git a/src/commands/superfakenitro.ts b/src/commands/superfakenitro.ts index 40f19d0..6afc8d3 100644 --- a/src/commands/superfakenitro.ts +++ b/src/commands/superfakenitro.ts @@ -1,22 +1,22 @@ -import {Command} from "../command.ts"; +import { Command } from "../command.ts"; import { ApplicationIntegrationType, + AutocompleteFocusedOption, + AutocompleteInteraction, ChatInputCommandInteraction, - InteractionContextType, REST, Routes, + InteractionContextType, REST, RESTGetAPIApplicationEmojisResult, Routes, SlashCommandBuilder } from "discord.js"; -import {config, Config} from "../config.ts"; +import { config, Config } from "../config.ts"; export default class SuperFakeNitroCommand extends Command { async run(interaction: ChatInputCommandInteraction, config: Config) { const emojiname = interaction.options.getString("emoji"); - const rest = new REST().setToken(config.token); - const data = await rest.get( - Routes.applicationEmojis(config.applicationid) - ); - // @ts-ignore + const data = await (interaction.client.rest.get( + Routes.applicationEmojis(interaction.applicationId) + ) as Promise); const shit = data.items - const theemoji = shit.find((item: any) => item.name === emojiname); + const theemoji = shit.find((item) => item.name === emojiname)!; let string = "<"; if (theemoji.animated) { string += `a`; @@ -29,6 +29,20 @@ export default class SuperFakeNitroCommand extends Command { await interaction.reply(string); } + async autoComplete(interaction: AutocompleteInteraction, config: Config, option: AutocompleteFocusedOption): Promise { + if (option.name === 'emoji') { + const search = option.value + const data = await (interaction.client.rest.get( + Routes.applicationEmojis(interaction.applicationId) + ) as Promise); + const matches = data.items.filter(item => item.name && item.name.toLowerCase().includes(search.toLowerCase())) + interaction.respond(matches.map(emoji => ({ + name: 'emoji', + value: emoji.name! + }))) + } + } + slashCommand = new SlashCommandBuilder() .setName("superfakenitro") .setDescription("yeahh").setIntegrationTypes([ @@ -36,6 +50,7 @@ export default class SuperFakeNitroCommand extends Command { ]) .addStringOption(option => { return option.setName("emoji").setRequired(true).setDescription("the emojis name") + .setAutocomplete(true) }) .setContexts([ InteractionContextType.BotDM, diff --git a/src/config.ts b/src/config.ts index c6c9949..7c926c1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,7 +4,7 @@ const configT = z.object({ token: z.string(), listenbrainzAccount: z.string(), gitapi: z.string(), - applicationid: z.string(), + // applicationid: z.string(), }); export type Config = z.infer; export const config: Config = configT.parse(rawconfig); diff --git a/src/index.ts b/src/index.ts index 80e666c..fbd9de0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { Client, Events, GatewayIntentBits, + InteractionCallback, REST, Routes, } from "discord.js"; @@ -15,10 +16,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const client = new Client({ - intents: Object.keys(GatewayIntentBits).map((a) => { - //@ts-ignore - return GatewayIntentBits[a] - }), + intents: [], }); const commands: Command[] = [] @@ -56,10 +54,27 @@ client.on(Events.InteractionCreate, async (interaction) => { try { await command.run(interaction, config); } catch (e) { - console.error("error during command execution: " + commandName, e ) + console.error("error during command execution: " + commandName, e) interaction.reply("something sharted itself") } }) +client.on(Events.InteractionCreate, async (interaction) => { + if (!interaction.isAutocomplete()) return + const {commandName} = interaction; + + const command = commandLookup[commandName] + if (!command) { + console.error("unknown command: " + commandName) + return + } + + try { + await command.autoComplete(interaction, config, interaction.options.getFocused(true)); + } catch (e) { + console.error("error during command execution: " + commandName, e) + } +}) + await client.login(config.token);