Files
blitzkit/src/index.ts
T

77 lines
2.5 KiB
TypeScript
Raw Normal View History

import {
Client,
Collection,
GatewayIntentBits,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
} from 'discord.js';
import { readdirSync } from 'fs';
import discord from '../discord.json' assert { type: 'json' };
2023-04-26 23:42:15 +00:00
import { registerErrorHandlers } from './behaviors/error.js';
2023-04-22 01:59:41 +00:00
import guildMemberAdd from './behaviors/guildMemberAdd.js';
import interactionCreateCurry, {
CommandRegistry,
} from './behaviors/interactionCreate.js';
2023-04-20 13:37:10 +00:00
import ready from './behaviors/ready.js';
import getClientId from './utilities/getClientId.js';
import isDev from './utilities/isDev.js';
2023-04-20 14:09:22 +00:00
import tokenRequirements from './utilities/tokenRequirements.js';
tokenRequirements();
2023-04-18 01:16:43 +00:00
2023-04-18 22:53:48 +00:00
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});
const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
const commandFolders = readdirSync('src/commands/');
const commandCollection = new Collection<string, CommandRegistry>();
const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
for (const file of commandFolders) {
const command = (await import(`./commands/${file}`))
.default as CommandRegistry;
if (isDev() ? command.inDevelopment : command.inProduction) {
if (command.inPublic) {
publicCommands.push(command.command.toJSON());
} else {
guildCommands.push(command.command.toJSON());
}
commandCollection.set(command.command.name, command);
}
}
try {
console.log(`Refreshing ${publicCommands.length} public command(s).`);
const publicData = (await rest.put(
Routes.applicationCommands(getClientId()),
{ body: publicCommands },
)) as RESTPostAPIChatInputApplicationCommandsJSONBody[];
console.log(`Successfully refreshed ${publicData.length} public command(s).`);
console.log(`Refreshing ${guildCommands.length} guild command(s).`);
const guildData = (await rest.put(
Routes.applicationGuildCommands(getClientId(), discord.guild_id),
{ body: guildCommands },
)) as RESTPostAPIChatInputApplicationCommandsJSONBody[];
console.log(`Successfully refreshed ${guildData.length} guild command(s).`);
} catch (error) {
console.error(error);
}
2023-04-18 01:16:43 +00:00
2023-04-26 23:42:15 +00:00
registerErrorHandlers(client)
.on('ready', ready)
.on('guildMemberAdd', guildMemberAdd)
.on(
'interactionCreate',
interactionCreateCurry({
commandCollection,
guildCommands,
publicCommands,
}),
)
2023-04-26 23:42:15 +00:00
.login(process.env.DISCORD_TOKEN);