feat: switched to vite node

This commit is contained in:
TresAbhi committed 2023-04-28 13:42:42 +00:00
1 parent d9deeb3630
commit 8ff34f0bbb
14 files changed
+370 -222

No files matched your search

+2 -1
View File
@@ -1,6 +1,7 @@
import { Client, EmbedBuilder, TextChannel } from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import { NEGATIVE_COLOR } from '../constants/colors.js';
import { client } from '../index.js';
import isDev from '../utilities/isDev.js';
const PROCESS_ERROR_EVENTS = ['uncaughtException'];
@@ -25,7 +26,7 @@ function handleError(error: Error, client: Client) {
});
}
export function registerErrorHandlers(client: Client) {
export function registerErrorHandlers() {
if (!isDev()) {
PROCESS_ERROR_EVENTS.forEach((name) =>
process.on(name, (error) => handleError(error, client)),
+73 -35
View File
@@ -3,15 +3,16 @@ import {
ChatInputCommandInteraction,
Collection,
Interaction,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
} from 'discord.js';
export interface CommandSet {
commandCollection: Collection<string, CommandRegistry>;
guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[];
publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[];
}
import { readdirSync } from 'fs';
import discord from '../../discord.json' assert { type: 'json' };
import { args } from '../utilities/args.js';
import getClientId from '../utilities/getClientId.js';
import isDev from '../utilities/isDev.js';
export interface CommandRegistry {
inDevelopment: boolean; // register with Skilled Bot (default: false)
@@ -19,39 +20,76 @@ export interface CommandRegistry {
inPublic: boolean; // registers on all servers (default: true)
command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
commandSed: CommandSet,
) => void;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
}
export default function interactionCreateCurry(commandSet: CommandSet) {
return async function interactionCreate(interaction: Interaction<CacheType>) {
if (!interaction.isChatInputCommand()) return;
const command = commandSet.commandCollection.get(interaction.commandName);
const rest = new REST().setToken(args['discord-token']);
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
const commandFolders = readdirSync('src/commands/');
const commandCollection = new Collection<string, CommandRegistry>();
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export 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());
}
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,
});
}
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,
});
}
};
}
}
+8 -7
View File
@@ -1,6 +1,10 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import { CommandRegistry } from '../behaviors/interactionCreate.js';
import {
CommandRegistry,
guildCommands,
publicCommands,
} from '../behaviors/interactionCreate.js';
import { SKILLED_COLOR } from '../constants/colors.js';
export default {
@@ -12,7 +16,7 @@ export default {
.setName('help')
.setDescription('All the help you need about the bot'),
async execute(interaction, commandCollection) {
async execute(interaction) {
await interaction.reply({
embeds: [
new EmbedBuilder()
@@ -21,11 +25,8 @@ 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
? [
...commandCollection.guildCommands,
...commandCollection.publicCommands,
]
: commandCollection.publicCommands
? [...guildCommands, ...publicCommands]
: publicCommands
)
.map((command) => `**/${command.name}**: ${command.description}`)
.join(
+3 -2
View File
@@ -4,6 +4,7 @@ import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { ClanInfo } from '../types/clanInfo.js';
import { PlayerPersonalData } from '../types/playerPersonalData.js';
import { args } from '../utilities/args.js';
import getClan from '../utilities/getClan.js';
import getWargamingResponse from '../utilities/getWargamingResponse.js';
@@ -53,14 +54,14 @@ export default {
const time = new Date().getTime() / 1000;
getWargamingResponse<ClanInfo>(
`https://api.wotblitz.com/wotb/clans/info/?application_id=${process.env.WARGAMING_APPLICATION_ID}&clan_id=${clan.clan_id}`,
`https://api.wotblitz.com/wotb/clans/info/?application_id=${args['wargaming-application-id']}&clan_id=${clan.clan_id}`,
interaction,
async (clanInfo) => {
const memberIds = clanInfo[clan.clan_id].members_ids;
getWargamingResponse<PlayerPersonalData>(
`https://api.wotblitz.com/wotb/account/info/?application_id=${
process.env.WARGAMING_APPLICATION_ID
args['wargaming-application-id']
}&account_id=${memberIds.join(',')}`,
interaction,
async (playerPersonalData) => {
+3 -2
View File
@@ -3,6 +3,7 @@ import { CommandRegistry } from '../behaviors/interactionCreate.js';
import { SKILLED_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { TanksStats } from '../types/tanksStats.js';
import { args } from '../utilities/args.js';
import getBlitzAccount from '../utilities/getBlitzAccount.js';
import getWargamingResponse from '../utilities/getWargamingResponse.js';
import { TANK_TYPE_EMOJIS, tankopedia } from '../utilities/tankopedia.js';
@@ -30,7 +31,7 @@ const COMP_TANKS = [
export default {
inProduction: true,
inDevelopment: true,
inDevelopment: false,
inPublic: true,
command: new SlashCommandBuilder()
@@ -69,7 +70,7 @@ export default {
getBlitzAccount(interaction, name, server, async (account) => {
getWargamingResponse<TanksStats>(
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${process.env.WARGAMING_APPLICATION_ID}&account_id=${account.account_id}`,
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${account.account_id}`,
interaction,
async (tankStats) => {
const tanks = tankStats[account.account_id]!.map(
+2 -1
View File
@@ -4,6 +4,7 @@ import { CommandRegistry } from '../behaviors/interactionCreate.js';
import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { PlayerClanData } from '../types/playerClanData.js';
import { args } from '../utilities/args.js';
import getBlitzAccount from '../utilities/getBlitzAccount.js';
import getWargamingResponse from '../utilities/getWargamingResponse.js';
@@ -39,7 +40,7 @@ export default {
getBlitzAccount(interaction, name, server, async (account) => {
getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${process.env.WARGAMING_APPLICATION_ID}&account_id=${account.account_id}&extra=clan`,
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${args['wargaming-application-id']}&account_id=${account.account_id}&extra=clan`,
interaction,
async (clanData) => {
const clan = clanData[account.account_id].clan;
+8 -67
View File
@@ -1,77 +1,18 @@
import {
Client,
Collection,
GatewayIntentBits,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
} from 'discord.js';
import { readdirSync } from 'fs';
import discord from '../discord.json' assert { type: 'json' };
import { Client, GatewayIntentBits } from 'discord.js';
import { registerErrorHandlers } from './behaviors/error.js';
import guildMemberAdd from './behaviors/guildMemberAdd.js';
import interactionCreateCurry, {
CommandRegistry,
} from './behaviors/interactionCreate.js';
import interactionCreate 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';
import { args } from './utilities/args.js';
tokenRequirements();
const client = new Client({
export 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[] = [];
registerErrorHandlers();
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)
client
.on('ready', ready)
.on('guildMemberAdd', guildMemberAdd)
.on(
'interactionCreate',
interactionCreateCurry({
commandCollection,
guildCommands,
publicCommands,
}),
)
.login(process.env.DISCORD_TOKEN);
.on('interactionCreate', interactionCreate)
.login(args['discord-token']);
+21
View File
@@ -0,0 +1,21 @@
import { argv } from 'process';
type ArgumentName = 'discord-token' | 'wargaming-application-id';
export const args: Record<ArgumentName, string> = {
'discord-token': '',
'wargaming-application-id': '',
};
argv.forEach((arg) => {
if (arg.includes('=')) {
const [name, value] = arg.split('=');
args[name as ArgumentName] = value;
}
});
Object.keys(args).forEach((arg) => {
if (args[arg as ArgumentName] === '') {
throw new Error(`Missing argument: ${arg}`);
}
});
+2 -1
View File
@@ -6,6 +6,7 @@ import {
import { NEGATIVE_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { Player, Players } from '../types/players.js';
import { args } from './args.js';
import getWargamingResponse from './getWargamingResponse.js';
export default async function getBlitzAccount(
@@ -17,7 +18,7 @@ export default async function getBlitzAccount(
const serverName = BLITZ_SERVERS[server];
getWargamingResponse<Players>(
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${name}`,
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${name}`,
interaction,
async (players) => {
if (players.length > 0 && players[0].nickname === name) {
+2 -1
View File
@@ -6,6 +6,7 @@ import {
import { NEGATIVE_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { Clan, ClanList } from '../types/clanList.js';
import { args } from './args.js';
import getWargamingResponse from './getWargamingResponse.js';
export default async function getClan(
@@ -17,7 +18,7 @@ export default async function getClan(
const serverName = BLITZ_SERVERS[server];
getWargamingResponse<ClanList>(
`https://api.wotblitz.${server}/wotb/clans/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${name}`,
`https://api.wotblitz.${server}/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${name}`,
interaction,
async (clans) => {
if (
-8
View File
@@ -1,8 +0,0 @@
export default function tokenRequirements() {
if (!process.env.DISCORD_TOKEN) {
throw new Error('DISCORD_TOKEN is not defined');
}
if (!process.env.WARGAMING_APPLICATION_ID) {
throw new Error('WARGAMING_APPLICATION_ID is not defined');
}
}