feat: switched back to node-ts because discord.js is a moron

This commit is contained in:
TresAbhi committed 2023-04-27 01:00:56 +00:00
1 parent 560f033a78
commit 6ddf030ce3
8 files changed
+205 -334

No files matched your search

+2 -2
View File
@@ -3,8 +3,8 @@ import discord from '../../discord.json' assert { type: 'json' };
import { NEGATIVE_COLOR } from '../constants/colors.js';
import isDev from '../utilities/isDev.js';
export const PROCESS_ERROR_EVENTS = ['uncaughtException'];
export const CLIENT_ERROR_EVENTS = ['error'];
const PROCESS_ERROR_EVENTS = ['uncaughtException'];
const CLIENT_ERROR_EVENTS = ['error'];
function handleError(error: Error, client: Client) {
console.error(error);
+35 -71
View File
@@ -3,15 +3,15 @@ import {
ChatInputCommandInteraction,
Collection,
Interaction,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
} from 'discord.js';
import { readdirSync } from 'fs';
import discord from '../../discord.json' assert { type: 'json' };
import getClientId from '../utilities/getClientId.js';
import isDev from '../utilities/isDev.js';
export interface CommandSet {
commandCollection: Collection<string, CommandRegistry>;
guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[];
publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[];
}
export interface CommandRegistry {
inDevelopment: boolean; // register with Skilled Bot (default: false)
@@ -19,75 +19,39 @@ export interface CommandRegistry {
inPublic: boolean; // registers on all servers (default: true)
command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
commandSed: CommandSet,
) => void;
}
export const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
const commandFolders = readdirSync('src/commands/');
const commandCollection = new Collection<string, CommandRegistry>();
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export default function interactionCreateCurry(commandSet: CommandSet) {
return async function interactionCreate(interaction: Interaction<CacheType>) {
if (!interaction.isChatInputCommand()) return;
const command = commandSet.commandCollection.get(interaction.commandName);
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());
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
}
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);
}
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
if (!interaction.isChatInputCommand()) return;
const command = commandCollection.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true,
});
try {
command.execute(interaction, commandSet);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true,
});
}
}
}
};
}
+2 -1
View File
@@ -2,10 +2,11 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import packageJSON from '../../package.json' assert { type: 'json' };
import { CommandRegistry } from '../behaviors/interactionCreate.js';
import { SKILLED_COLOR } from '../constants/colors.js';
import { executionStart } from '../index.js';
import getClientId from '../utilities/getClientId.js';
import { tankopedia } from '../utilities/tankopedia.js';
const executionStart = new Date().getTime();
export default {
inProduction: true,
inDevelopment: false,
+1 -1
View File
@@ -7,7 +7,7 @@ import getBlitzAccount from '../utilities/getBlitzAccount.js';
import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js';
import poweredByBlitzStars from '../utilities/poweredByBlitzStars.js';
export const CLANS = {
const CLANS = {
sklld: {
name: 'Skilled',
id: 71559,
+7 -8
View File
@@ -1,10 +1,6 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import {
CommandRegistry,
guildCommands,
publicCommands,
} from '../behaviors/interactionCreate.js';
import { CommandRegistry } from '../behaviors/interactionCreate.js';
import { SKILLED_COLOR } from '../constants/colors.js';
export default {
@@ -16,7 +12,7 @@ export default {
.setName('help')
.setDescription('All the help you need about the bot'),
async execute(interaction) {
async execute(interaction, commandCollection) {
await interaction.reply({
embeds: [
new EmbedBuilder()
@@ -25,8 +21,11 @@ export default {
.setDescription(
`**About**\nSkilled Bot automates many mundane tasks and provide statistics in numerous flexible ways.\n\nMade by TresAbhi from the Skilled [SKLLD] clan.\nGitHub: https://github.com/sklld/bot\nSkilled: https://discord.gg/ZPvcEG7DS8\n\n**Commands**\n${(interaction.guildId ===
discord.guild_id
? [...guildCommands, ...publicCommands]
: publicCommands
? [
...commandCollection.guildCommands,
...commandCollection.publicCommands,
]
: commandCollection.publicCommands
)
.map((command) => `**/${command.name}**: ${command.description}`)
.join(
+62 -5
View File
@@ -1,20 +1,77 @@
import { Client, GatewayIntentBits } from 'discord.js';
import {
Client,
Collection,
GatewayIntentBits,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
} from 'discord.js';
import { readdirSync } from 'fs';
import discord from '../discord.json' assert { type: 'json' };
import { registerErrorHandlers } from './behaviors/error.js';
import guildMemberAdd from './behaviors/guildMemberAdd.js';
import interactionCreate from './behaviors/interactionCreate.js';
import interactionCreateCurry, {
CommandRegistry,
} from './behaviors/interactionCreate.js';
import ready from './behaviors/ready.js';
import getClientId from './utilities/getClientId.js';
import isDev from './utilities/isDev.js';
import tokenRequirements from './utilities/tokenRequirements.js';
export const executionStart = new Date().getTime();
tokenRequirements();
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);
}
registerErrorHandlers(client)
.on('ready', ready)
.on('guildMemberAdd', guildMemberAdd)
.on('interactionCreate', interactionCreate)
.on(
'interactionCreate',
interactionCreateCurry({
commandCollection,
guildCommands,
publicCommands,
}),
)
.login(process.env.DISCORD_TOKEN);