feat: Add application emoji search
This commit is contained in:
parent
da13cdf3e9
commit
7b707ec557
5 changed files with 51 additions and 17 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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<void>;
|
||||
autoComplete(interaction: AutocompleteInteraction, config: Config, option: AutocompleteFocusedOption) : Promise<void> {
|
||||
throw new Error("Autocompletion called on command that does not have #autoComplete implemented.");
|
||||
}
|
||||
abstract slashCommand: SharedSlashCommand
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<RESTGetAPIApplicationEmojisResult>);
|
||||
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<void> {
|
||||
if (option.name === 'emoji') {
|
||||
const search = option.value
|
||||
const data = await (interaction.client.rest.get(
|
||||
Routes.applicationEmojis(interaction.applicationId)
|
||||
) as Promise<RESTGetAPIApplicationEmojisResult>);
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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<typeof configT>;
|
||||
export const config: Config = configT.parse(rawconfig);
|
||||
|
|
|
|||
25
src/index.ts
25
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue