-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
116 lines (105 loc) · 2.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { CronJob } from 'cron';
import {
Client,
Collection,
type ContextMenuCommandBuilder,
REST,
Routes,
type SlashCommandBuilder,
} from 'discord.js';
import { getConfig, type Config } from './core/configuration';
import { gateLoader, type Gate } from './core/gates';
import { logException } from './core/helpers';
import { Logger } from './core/logger';
import { appConfig } from './.sparkbot.config';
import {
type CommandSpark,
type InteractionSpark,
sparkLoader,
} from './core/sparks';
/* Spark⚡️Bot is designed to run with Bun and will have errors if run in a
different environment. */
if (!Bun) {
throw new Error('Must be run with Bun');
}
// Initialize Logger
const logger = new Logger();
// Get parsed config
let config: Config;
try {
config = await getConfig(appConfig);
} catch (exception) {
throw logException(exception, logger);
}
// Initialize user-configured logger
try {
await logger.loadPlugin(config.loggingLibraryPlugin);
} catch (exception) {
throw logException(exception, logger);
}
// Initialize Discord.js client
declare module 'discord.js' {
interface Client {
config: Config;
interactions: Collection<string, InteractionSpark | CommandSpark>;
gates: Collection<string, Gate>;
logger: Logger;
scheduledEvents: Collection<string, CronJob>;
}
}
const discordClient = new Client({
intents: config.discordIntents,
partials: config.enabledPartials,
presence: config.defaultPresence,
});
discordClient.config = config;
discordClient.logger = logger;
logger.registerClientHandlers(discordClient);
discordClient.scheduledEvents = new Collection();
// Load gates
try {
discordClient.gates = await gateLoader();
} catch (exception) {
throw logException(exception, logger);
}
// Load sparks
discordClient.interactions = new Collection<
string,
InteractionSpark | CommandSpark
>();
try {
await sparkLoader(discordClient);
} catch (exception) {
throw logException(exception, logger);
}
const commands: Array<SlashCommandBuilder | ContextMenuCommandBuilder> = [];
discordClient.interactions.each((interaction) => {
if ('command' in interaction) {
commands.push(interaction.command);
}
});
if (commands.length > 0) {
const rest = new REST({ version: '10' }).setToken(config.discordAPIKey);
const route = Routes.applicationCommands(config.discordAppID);
try {
await rest
.put(route, {
body: commands.map((command) => command.toJSON()),
})
.then((data) => {
if (data && typeof data === 'object' && 'length' in data)
logger.info(`🔵 Registered ${String(data.length)} command(s)`);
})
.catch((exception: unknown) => {
logException(exception, logger);
});
} catch (exception) {
logException(exception, logger);
}
}
// Login to Discord
try {
await discordClient.login(config.discordAPIKey);
} catch (exception) {
throw logException(exception, logger);
}