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",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"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": {
|
"dependencies": {
|
||||||
"discord.js": "^14.17.2",
|
"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";
|
import { Config } from "./config";
|
||||||
|
|
||||||
export abstract class Command {
|
export abstract class Command {
|
||||||
abstract run(interaction: ChatInputCommandInteraction, config: Config): Promise<void>;
|
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
|
abstract slashCommand: SharedSlashCommand
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
import {Command} from "../command.ts";
|
import { Command } from "../command.ts";
|
||||||
import {
|
import {
|
||||||
ApplicationIntegrationType,
|
ApplicationIntegrationType,
|
||||||
|
AutocompleteFocusedOption,
|
||||||
|
AutocompleteInteraction,
|
||||||
ChatInputCommandInteraction,
|
ChatInputCommandInteraction,
|
||||||
InteractionContextType, REST, Routes,
|
InteractionContextType, REST, RESTGetAPIApplicationEmojisResult, Routes,
|
||||||
SlashCommandBuilder
|
SlashCommandBuilder
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import {config, Config} from "../config.ts";
|
import { config, Config } from "../config.ts";
|
||||||
|
|
||||||
export default class SuperFakeNitroCommand extends Command {
|
export default class SuperFakeNitroCommand extends Command {
|
||||||
async run(interaction: ChatInputCommandInteraction, config: Config) {
|
async run(interaction: ChatInputCommandInteraction, config: Config) {
|
||||||
const emojiname = interaction.options.getString("emoji");
|
const emojiname = interaction.options.getString("emoji");
|
||||||
const rest = new REST().setToken(config.token);
|
const data = await (interaction.client.rest.get(
|
||||||
const data = await rest.get(
|
Routes.applicationEmojis(interaction.applicationId)
|
||||||
Routes.applicationEmojis(config.applicationid)
|
) as Promise<RESTGetAPIApplicationEmojisResult>);
|
||||||
);
|
|
||||||
// @ts-ignore
|
|
||||||
const shit = data.items
|
const shit = data.items
|
||||||
const theemoji = shit.find((item: any) => item.name === emojiname);
|
const theemoji = shit.find((item) => item.name === emojiname)!;
|
||||||
let string = "<";
|
let string = "<";
|
||||||
if (theemoji.animated) {
|
if (theemoji.animated) {
|
||||||
string += `a`;
|
string += `a`;
|
||||||
|
|
@ -29,6 +29,20 @@ export default class SuperFakeNitroCommand extends Command {
|
||||||
await interaction.reply(string);
|
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()
|
slashCommand = new SlashCommandBuilder()
|
||||||
.setName("superfakenitro")
|
.setName("superfakenitro")
|
||||||
.setDescription("yeahh").setIntegrationTypes([
|
.setDescription("yeahh").setIntegrationTypes([
|
||||||
|
|
@ -36,6 +50,7 @@ export default class SuperFakeNitroCommand extends Command {
|
||||||
])
|
])
|
||||||
.addStringOption(option => {
|
.addStringOption(option => {
|
||||||
return option.setName("emoji").setRequired(true).setDescription("the emojis name")
|
return option.setName("emoji").setRequired(true).setDescription("the emojis name")
|
||||||
|
.setAutocomplete(true)
|
||||||
})
|
})
|
||||||
.setContexts([
|
.setContexts([
|
||||||
InteractionContextType.BotDM,
|
InteractionContextType.BotDM,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ const configT = z.object({
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
listenbrainzAccount: z.string(),
|
listenbrainzAccount: z.string(),
|
||||||
gitapi: z.string(),
|
gitapi: z.string(),
|
||||||
applicationid: z.string(),
|
// applicationid: z.string(),
|
||||||
});
|
});
|
||||||
export type Config = z.infer<typeof configT>;
|
export type Config = z.infer<typeof configT>;
|
||||||
export const config: Config = configT.parse(rawconfig);
|
export const config: Config = configT.parse(rawconfig);
|
||||||
|
|
|
||||||
25
src/index.ts
25
src/index.ts
|
|
@ -2,6 +2,7 @@ import {
|
||||||
Client,
|
Client,
|
||||||
Events,
|
Events,
|
||||||
GatewayIntentBits,
|
GatewayIntentBits,
|
||||||
|
InteractionCallback,
|
||||||
REST,
|
REST,
|
||||||
Routes,
|
Routes,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
|
|
@ -15,10 +16,7 @@ const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: Object.keys(GatewayIntentBits).map((a) => {
|
intents: [],
|
||||||
//@ts-ignore
|
|
||||||
return GatewayIntentBits[a]
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const commands: Command[] = []
|
const commands: Command[] = []
|
||||||
|
|
@ -56,10 +54,27 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
try {
|
try {
|
||||||
await command.run(interaction, config);
|
await command.run(interaction, config);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error during command execution: " + commandName, e )
|
console.error("error during command execution: " + commandName, e)
|
||||||
interaction.reply("something sharted itself")
|
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);
|
await client.login(config.token);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue