xernerx 0.1.120-beta.2 → 0.1.120-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -0
  3. package/dist/main.cjs +1269 -266
  4. package/dist/main.cjs.map +1 -1
  5. package/dist/main.d.cts +651 -116
  6. package/dist/main.d.ts +651 -116
  7. package/dist/main.js +1257 -247
  8. package/dist/main.js.map +1 -1
  9. package/package.json +31 -18
  10. package/src/build/XernerxBaseBuilder.ts +21 -0
  11. package/src/build/XernerxEventBuilder.ts +39 -0
  12. package/src/build/XernerxMessageCommandBuilder.ts +60 -0
  13. package/src/build/XernerxSlashCommandBuilder.ts +290 -0
  14. package/src/client/XernerxClient.ts +173 -151
  15. package/src/client/XernerxShardClient.ts +89 -0
  16. package/src/events/clientReady.ts +96 -0
  17. package/src/events/entitlementCreate.ts +15 -0
  18. package/src/events/interactionCreate.ts +36 -0
  19. package/src/events/messageCreate.ts +60 -0
  20. package/src/events/messageDelete.ts +41 -0
  21. package/src/events/messageUpdate.ts +60 -0
  22. package/src/events/slashCommandInteraction.ts +55 -0
  23. package/src/handler/CommandHandler.ts +132 -0
  24. package/src/handler/EventHandler.ts +45 -0
  25. package/src/handler/Handler.ts +98 -0
  26. package/src/interfaces/XernerxClientOptions.ts +13 -0
  27. package/src/interfaces/XernerxClientStats.ts +15 -0
  28. package/src/interfaces/XernerxEventBuilderOptions.ts +9 -0
  29. package/src/interfaces/XernerxMessageCommandBuilderOptions.ts +8 -0
  30. package/src/interfaces/XernerxMessageCommandHandlerOptions.ts +15 -0
  31. package/src/interfaces/XernerxShardClientOptions.ts +5 -0
  32. package/src/interfaces/XernerxSlashCommand.ts +19 -0
  33. package/src/interfaces/XernerxSlashCommandBuilderOptions.ts +70 -0
  34. package/src/interfaces/XernerxSlashCommandHandlerOptions.ts +5 -0
  35. package/src/main.ts +21 -7
  36. package/src/model/XernerxGuild.ts +13 -0
  37. package/src/model/XernerxInteractionArguments.ts +92 -0
  38. package/src/model/XernerxMonitisation.ts +73 -0
  39. package/src/model/XernerxUser.ts +82 -0
  40. package/src/tools/XernerxBase.ts +16 -0
  41. package/src/tools/XernerxError.ts +16 -0
  42. package/src/tools/XernerxInfo.ts +13 -0
  43. package/src/tools/XernerxInitial.ts +28 -0
  44. package/src/tools/XernerxSuccess.ts +13 -0
  45. package/src/tools/XernerxWarn.ts +13 -0
  46. package/src/types/Events.d.ts +21 -0
  47. package/src/types/Interaction.d.ts +90 -0
  48. package/src/types/Message.d.ts +16 -0
  49. package/src/util/XernerxInteractionUtil.ts +82 -0
  50. package/src/util/XernerxMessageUtil.ts +105 -0
  51. package/src/util/XernerxUtil.ts +47 -0
  52. package/src/validators/XernerxBaseValidator.ts +82 -0
  53. package/src/validators/XernerxSlashCommandValidator.ts +11 -0
  54. package/dist/main.global.js +0 -88544
  55. package/dist/main.global.js.map +0 -1
  56. package/src/base/XernerxContextCommand.ts +0 -24
  57. package/src/base/XernerxEvent.ts +0 -21
  58. package/src/base/XernerxInhibitor.ts +0 -21
  59. package/src/base/XernerxMessageCommand.ts +0 -24
  60. package/src/base/XernerxSlashCommand.ts +0 -26
  61. package/src/handlers/CommandHandler.ts +0 -31
  62. package/src/handlers/Handler.ts +0 -57
  63. package/src/tools/XernerxLog.ts +0 -58
  64. package/src/types/interfaces.ts +0 -25
  65. package/src/types/types.ts +0 -29
  66. package/src/util/ClientUtil.ts +0 -29
  67. package/src/util/Util.ts +0 -11
  68. package/src/util/prompts.ts +0 -120
@@ -0,0 +1,89 @@
1
+ /** @format */
2
+
3
+ import { Cluster, ClusterManager, ClusterManagerOptions } from 'discord-hybrid-sharding';
4
+ import sharpyy from 'sharpyy';
5
+ import fs from 'fs';
6
+
7
+ import { XernerxInfo } from '../tools/XernerxInfo.js';
8
+ import { XernerxError } from '../tools/XernerxError.js';
9
+ import { XernerxSuccess } from '../tools/XernerxSuccess.js';
10
+ import { XernerxShardClientOptions } from '../interfaces/XernerxShardClientOptions.js';
11
+ import { XernerxInitial } from '../tools/XernerxInitial.js';
12
+
13
+ export class XernerxShardClient extends ClusterManager {
14
+ declare public readonly onlineShards: Set<Cluster>;
15
+ declare public readonly stats: {
16
+ onlineSince: number | null;
17
+ guildCount: number | null;
18
+ userCount: number | null;
19
+ shardCount: number | null;
20
+ voteCount: number | null;
21
+ shards: [] | null;
22
+ };
23
+ constructor(options: ClusterManagerOptions & XernerxShardClientOptions) {
24
+ new XernerxInitial('ShardClient');
25
+
26
+ try {
27
+ new XernerxInfo(`Loading ShardClient from ${sharpyy(options.file, 'bgGray')}`);
28
+ (fs.readFileSync(options.file), 'utf-8');
29
+ new XernerxSuccess(`ShardClient loaded successfully`);
30
+ } catch {
31
+ new XernerxError(`File ${sharpyy(options.file, 'bgGray')} not found.`, 1);
32
+ }
33
+
34
+ super(options.file, options);
35
+
36
+ this.onlineShards = new Set();
37
+
38
+ this.stats = {
39
+ onlineSince: Number(new Date()),
40
+ guildCount: null,
41
+ userCount: null,
42
+ shardCount: null,
43
+ voteCount: null,
44
+ shards: [],
45
+ };
46
+
47
+ this.on('clusterCreate', (cluster) => {
48
+ new XernerxInfo(`${sharpyy(`Shard ${cluster.id}`, 'txCyan')} | Creating...`);
49
+
50
+ cluster.on('ready', () => {
51
+ this.onlineShards.add(cluster);
52
+
53
+ if (this.onlineShards.size == (this.totalClusters || this.totalShards)) this.#update();
54
+ });
55
+
56
+ cluster.on('disconnect', () => {
57
+ this.onlineShards.delete(cluster);
58
+ });
59
+
60
+ cluster.on('death', (error) => {
61
+ this.onlineShards.delete(cluster);
62
+ });
63
+ });
64
+
65
+ this.spawn({ timeout: -1 });
66
+ }
67
+
68
+ async #update() {
69
+ await new Promise((resolve) => setTimeout(resolve, 1000));
70
+
71
+ setInterval(async () => {
72
+ const stats = await this.fetchClientValues('stats');
73
+
74
+ const shards = await this.fetchClientValues('stats.shard');
75
+
76
+ this.stats.guildCount = stats[0].guildCount;
77
+ this.stats.userCount = stats[0].userCount;
78
+ this.stats.shardCount = stats[0].shardCount;
79
+ this.stats.voteCount = stats[0].voteCount;
80
+ this.stats.shards = shards;
81
+ }, 1000);
82
+
83
+ await new Promise((resolve) => setTimeout(resolve, 4000));
84
+
85
+ new XernerxSuccess(
86
+ `All Shards are online! Watching ${sharpyy(String(this.stats.guildCount?.toLocaleString()), 'txCyan')} guilds and users: ${sharpyy(String(this.stats.userCount?.toLocaleString()), 'txCyan')}, and ${sharpyy(String(this.stats.shardCount?.toLocaleString()), 'txCyan')} shards.`
87
+ );
88
+ }
89
+ }
@@ -0,0 +1,96 @@
1
+ /** @format */
2
+
3
+ import { Client, GatewayVersion, REST, Routes } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxClient } from '../client/XernerxClient.js';
7
+ import { XernerxSlashCommandBuilder } from '../build/XernerxSlashCommandBuilder.js';
8
+ import { XernerxError } from '../tools/XernerxError.js';
9
+ import { XernerxSuccess } from '../tools/XernerxSuccess.js';
10
+ import { XernerxInfo } from '../tools/XernerxInfo.js';
11
+ import { XernerxUser } from '../model/XernerxUser.js';
12
+
13
+ export class XernerxClientReadyEvent extends XernerxEventBuilder {
14
+ declare private readonly commands: {
15
+ global: Array<XernerxSlashCommandBuilder>;
16
+ local: Array<XernerxSlashCommandBuilder>;
17
+ };
18
+
19
+ constructor() {
20
+ super('XernerxClientReadyEvent', {
21
+ name: 'clientReady',
22
+ emitter: 'client',
23
+ once: true,
24
+ });
25
+
26
+ this.commands = {
27
+ global: [],
28
+ local: [],
29
+ };
30
+ }
31
+
32
+ override async run(client: XernerxClient) {
33
+ // premium for owners
34
+
35
+ if (client.settings.owners?.length) {
36
+ const owners = [];
37
+
38
+ if (client.premium?.synchronize) new XernerxInfo(`Synchronizing entitlements for premium owners...`);
39
+
40
+ for (const owner of client.settings.owners) {
41
+ const user = await client.users.fetch(owner);
42
+
43
+ new XernerxUser(client as Client<true>, user).synchronize();
44
+
45
+ owners.push(user);
46
+ }
47
+
48
+ if (client.premium?.synchronize) new XernerxSuccess(`Successfully synchronized entitlements for ${owners.map((owner) => owner.username).join(',')}`);
49
+ }
50
+
51
+ // interaction commands
52
+ try {
53
+ const commands = [...client.commands.slash].map(([, command]) => command);
54
+
55
+ if (!client.settings.global) {
56
+ this.commands.global = [];
57
+ this.commands.local = commands;
58
+ } else {
59
+ this.commands.global = commands.filter((command) => command.deploy?.global);
60
+ this.commands.local = commands.filter((command) => !command.deploy?.global);
61
+ }
62
+
63
+ this.put(client);
64
+
65
+ new XernerxSuccess(`Successfully deployed slash commands ${client.settings.global ? 'globally' : 'locally'}`);
66
+ } catch (error) {
67
+ new XernerxError((error as Error).message);
68
+ }
69
+ }
70
+
71
+ async put(client: XernerxClient) {
72
+ const rest = new REST({ version: GatewayVersion }).setToken(client.token);
73
+
74
+ if (this.commands.global.length) {
75
+ if (!client.settings.global) return new XernerxInfo('Loading commands locally');
76
+
77
+ await rest.put(Routes.applicationCommands(client.user?.id as string), { body: this.commands.global.map((command) => command.data.toJSON()) });
78
+ }
79
+
80
+ if (this.commands.local.length) {
81
+ const guilds: { [index: string]: Array<XernerxSlashCommandBuilder> } = {};
82
+
83
+ for (const command of this.commands.local) {
84
+ (command.deploy?.guilds as Array<string>)?.map((id) => (guilds[id] ? guilds[id].push(command) : (guilds[id] = [command])));
85
+ }
86
+
87
+ if (!client.settings.guildId?.length && !Object.keys(guilds).length) return new XernerxError('Cannot locally deploy slash commands without a guild ID');
88
+
89
+ (client.settings.guildId as Array<string>).map((id: string) => (guilds[id] ? guilds[id] : (guilds[id] = this.commands.local)));
90
+
91
+ for (const [guildId, commands] of Object.entries(guilds)) {
92
+ await rest.put(Routes.applicationGuildCommands(client.user?.id as string, guildId), { body: commands.map((command) => command.data.toJSON()) });
93
+ }
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,15 @@
1
+ /** @format */
2
+
3
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
4
+
5
+ export class XernerxEntitlementCreateEvent extends XernerxEventBuilder {
6
+ constructor() {
7
+ super('XernerxEntitlementCreateEvent', {
8
+ name: 'entitlementCreate',
9
+ emitter: 'client',
10
+ once: false,
11
+ });
12
+ }
13
+
14
+ override async run(a: any, b: any, c: any) {}
15
+ }
@@ -0,0 +1,36 @@
1
+ /** @format */
2
+
3
+ import { Interaction } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxUser } from '../model/XernerxUser.js';
7
+ import { XernerxClient } from '../client/XernerxClient.js';
8
+ import { XernerxInteractionUtil } from '../util/XernerxInteractionUtil.js';
9
+
10
+ export class XernerxInteractionCreateEvent extends XernerxEventBuilder {
11
+ constructor() {
12
+ super('XernerxInteractionCreateEvent', {
13
+ name: 'interactionCreate',
14
+ emitter: 'client',
15
+ once: false,
16
+ });
17
+ }
18
+
19
+ override async run(interaction: Interaction) {
20
+ this.client = interaction.client as XernerxClient;
21
+
22
+ interaction.user = new XernerxUser(interaction.client, interaction.user);
23
+
24
+ interaction.util = new XernerxInteractionUtil(this.client as Interaction['client'], interaction);
25
+
26
+ if (interaction.isChatInputCommand()) return await interaction.client.emit('slashCommandInteraction', interaction);
27
+
28
+ if (interaction.isContextMenuCommand()) return await interaction.client.emit('contextCommandInteraction', interaction);
29
+
30
+ if (interaction.isAnySelectMenu()) return await interaction.client.emit('selectMenuInteraction', interaction);
31
+
32
+ if (interaction.isButton()) return await interaction.client.emit('buttonInteraction', interaction);
33
+
34
+ if (interaction.isModalSubmit()) return await interaction.client.emit('modalInteraction', interaction);
35
+ }
36
+ }
@@ -0,0 +1,60 @@
1
+ /** @format */
2
+
3
+ import { Message } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxUser } from '../model/XernerxUser.js';
7
+ import { XernerxMessageUtil } from '../util/XernerxMessageUtil.js';
8
+
9
+ export class XernerxMessageCreateEvent extends XernerxEventBuilder {
10
+ constructor() {
11
+ super('XernerxMessageCreateEvent', {
12
+ name: 'messageCreate',
13
+ emitter: 'client',
14
+ once: false,
15
+ });
16
+ }
17
+
18
+ override async run(message: Message) {
19
+ this.client = message.client;
20
+
21
+ message.user = new XernerxUser(message.client, message.author);
22
+
23
+ message.util = new XernerxMessageUtil(this.client as Message['client'], message);
24
+
25
+ if (!message.content) return;
26
+ if (this.client.handler.message.ignore?.bots && message.author.bot) return;
27
+ if (this.client.handler.message.ignore?.self && message.author.id === this.client.user?.id) return;
28
+ if (this.client.handler.message.ignore?.system && message.system) return;
29
+
30
+ if (!message.util.parsed.command) return;
31
+
32
+ const command = this.client.commands.message.get(message.util.parsed.command);
33
+
34
+ if (!command) return;
35
+
36
+ if (
37
+ this.client.handler.message.mention &&
38
+ !message.util.parsed.mention &&
39
+ !command?.prefix.includes(message.util.parsed.prefix as string) &&
40
+ !this.client.handler.message.prefix?.includes(message.util.parsed.prefix as string)
41
+ )
42
+ return;
43
+
44
+ // validation
45
+
46
+ // args
47
+
48
+ // inhibitors
49
+
50
+ try {
51
+ await message.client.emit('commandStart', message, {}, command);
52
+
53
+ await command?.exec(message, message.util.args);
54
+
55
+ await message.client.emit('commandFinish', message, {}, command);
56
+ } catch (error) {
57
+ await message.client.emit('commandError', message, {}, command, error as Error);
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,41 @@
1
+ /** @format */
2
+
3
+ import { Message } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxUser } from '../model/XernerxUser.js';
7
+ import { XernerxMessageUtil } from '../util/XernerxMessageUtil.js';
8
+
9
+ export class XernerxMessageDeleteEvent extends XernerxEventBuilder {
10
+ constructor() {
11
+ super('XernerxMessageDeleteEvent', {
12
+ name: 'messageDelete',
13
+ emitter: 'client',
14
+ once: false,
15
+ });
16
+ }
17
+
18
+ override async run(message: Message) {
19
+ this.client = message.client;
20
+
21
+ message.user = new XernerxUser(message.client, message.author);
22
+
23
+ message.util = new XernerxMessageUtil(this.client as Message['client'], message);
24
+
25
+ if (!this.client.handler.message.handleDeletions) return;
26
+
27
+ if (!this.client.cache.messages.has(message.id)) return;
28
+
29
+ const id = this.client.cache.messages.get(message.id);
30
+
31
+ if (!id) return;
32
+
33
+ try {
34
+ const msg = await message.channel.messages.fetch(id);
35
+
36
+ await msg.delete();
37
+ } catch (error) {
38
+ await this.client.emit('commandDelete', message, error as Error);
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,60 @@
1
+ /** @format */
2
+
3
+ import { Message } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxUser } from '../model/XernerxUser.js';
7
+ import { XernerxMessageUtil } from '../util/XernerxMessageUtil.js';
8
+
9
+ export class XernerxMessageUpdateEvent extends XernerxEventBuilder {
10
+ constructor() {
11
+ super('XernerxMessageUpdateEvent', {
12
+ name: 'messageUpdate',
13
+ emitter: 'client',
14
+ once: false,
15
+ });
16
+ }
17
+
18
+ override async run(oldMessage: Message, message: Message) {
19
+ this.client = message.client;
20
+
21
+ message.user = new XernerxUser(message.client, message.author);
22
+
23
+ message.util = new XernerxMessageUtil(this.client as Message['client'], message);
24
+
25
+ if (!message.content) return;
26
+ if (this.client.handler.message.ignore?.bots && message.author.bot) return;
27
+ if (this.client.handler.message.ignore?.self && message.author.id === this.client.user?.id) return;
28
+ if (this.client.handler.message.ignore?.system && message.system) return;
29
+
30
+ if (!message.util.parsed.command) return;
31
+
32
+ const command = this.client.commands.message.get(message.util.parsed.command);
33
+
34
+ if (!command) return;
35
+
36
+ if (
37
+ this.client.handler.message.mention &&
38
+ !message.util.parsed.mention &&
39
+ !command?.prefix.includes(message.util.parsed.prefix as string) &&
40
+ !this.client.handler.message.prefix?.includes(message.util.parsed.prefix as string)
41
+ )
42
+ return;
43
+
44
+ // validation
45
+
46
+ // args
47
+
48
+ // inhibitors
49
+
50
+ try {
51
+ await message.client.emit('commandStart', message, {}, command);
52
+
53
+ await command?.exec(message, message.util.args);
54
+
55
+ await message.client.emit('commandFinish', message, {}, command);
56
+ } catch (error) {
57
+ await message.client.emit('commandError', message, {}, command, error as Error);
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,55 @@
1
+ /** @format */
2
+
3
+ import { AutocompleteInteraction, ChatInputCommandInteraction } from 'discord.js';
4
+
5
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
6
+ import { XernerxInteractionArguments } from '../model/XernerxInteractionArguments.js';
7
+ import { XernerxSlashCommandValidator } from '../validators/XernerxSlashCommandValidator.js';
8
+
9
+ export class XernerxSlashCommandInteractionEvent extends XernerxEventBuilder {
10
+ constructor() {
11
+ super('XernerxSlashCommandInteractionEvent', {
12
+ name: 'slashCommandInteraction',
13
+ emitter: 'client',
14
+ once: false,
15
+ });
16
+ }
17
+
18
+ override async run(interaction: ChatInputCommandInteraction) {
19
+ const command = interaction.client.commands.slash.get(interaction.commandName);
20
+
21
+ if (!command) return;
22
+
23
+ // inhibitors
24
+
25
+ new Promise(async (resolve) => {
26
+ if (interaction.isAutocomplete()) {
27
+ const focused = (interaction as AutocompleteInteraction).options.getFocused(true);
28
+ const options = (interaction as AutocompleteInteraction).options;
29
+
30
+ await command.autocomplete({ interaction, focused, options });
31
+ } else resolve(true);
32
+ }).then(async () => {
33
+ const args = new XernerxInteractionArguments(interaction, command);
34
+ const options = { options: args.options(), subcommand: args.subcommand(), group: args.subcommand() };
35
+
36
+ try {
37
+ await interaction.client.emit('commandStart', interaction, options, command);
38
+
39
+ const validation = new XernerxSlashCommandValidator(interaction, command);
40
+
41
+ if (!(await validation.validate())) return;
42
+
43
+ if (command.defer) await interaction.deferReply();
44
+
45
+ if (await command.conditions({ interaction, ...options, command })) return;
46
+
47
+ await command.exec({ interaction, ...options, command });
48
+
49
+ await interaction.client.emit('commandFinish', interaction, options, command);
50
+ } catch (error) {
51
+ await interaction.client.emit('commandError', interaction, options, command, error as Error);
52
+ }
53
+ });
54
+ }
55
+ }
@@ -0,0 +1,132 @@
1
+ /** @format */
2
+
3
+ import sharpyy from 'sharpyy';
4
+ import { z } from 'zod';
5
+
6
+ import { XernerxClient } from '../client/XernerxClient.js';
7
+ import { Handler } from './Handler.js';
8
+ import { XernerxMessageCreateEvent } from '../events/messageCreate.js';
9
+ import { XernerxMessageUpdateEvent } from '../events/messageUpdate.js';
10
+ import { XernerxMessageDeleteEvent } from '../events/messageDelete.js';
11
+ import { XernerxInteractionCreateEvent } from '../events/interactionCreate.js';
12
+ import { XernerxSuccess } from '../tools/XernerxSuccess.js';
13
+ import { XernerxWarn } from '../tools/XernerxWarn.js';
14
+ import { XernerxError } from '../tools/XernerxError.js';
15
+ import { XernerxMessageCommandHandlerOptions } from '../interfaces/XernerxMessageCommandHandlerOptions.js';
16
+ import { XernerxSlashCommandHandlerOptions } from '../interfaces/XernerxSlashCommandHandlerOptions.js';
17
+ import { XernerxEventBuilder } from '../build/XernerxEventBuilder.js';
18
+ import { XernerxClientReadyEvent } from '../events/clientReady.js';
19
+ import { XernerxSlashCommandInteractionEvent } from '../events/slashCommandInteraction.js';
20
+ import { XernerxEntitlementCreateEvent } from '../events/entitlementCreate.js';
21
+
22
+ export class CommandHandler extends Handler {
23
+ constructor(client: XernerxClient) {
24
+ super(client);
25
+
26
+ this.loadEvents(XernerxEntitlementCreateEvent, XernerxClientReadyEvent);
27
+ }
28
+
29
+ /**
30
+ * Loads a message command from a specified file.
31
+ *
32
+ * @param file - The path to the file containing the message command to be loaded.
33
+ * @returns A promise that resolves when the file has been successfully loaded.
34
+ */
35
+ public async loadMessageCommand(file: string) {
36
+ return await this.loadFile(file);
37
+ }
38
+
39
+ /**
40
+ * Loads message commands based on the provided options.
41
+ *
42
+ * @param options - Configuration options for loading message commands, including directory, prefix, mention, separator, handleEdits, handleDeletions, and ignore settings.
43
+ * @returns A promise that resolves when all message commands have been successfully loaded.
44
+ */
45
+ public async loadMessageCommands(options: XernerxMessageCommandHandlerOptions) {
46
+ const config = z
47
+ .object({
48
+ directory: z.string(),
49
+ prefix: z.union([z.string(), z.array(z.string())]).default([]),
50
+ mention: z.boolean().default(false),
51
+ seperator: z.string().default(' '),
52
+ handleEdits: z.boolean().default(false),
53
+ handleDeletions: z.boolean().default(false),
54
+ ignore: z
55
+ .object({
56
+ system: z.boolean().default(true),
57
+ bots: z.boolean().default(true),
58
+ self: z.boolean().default(true),
59
+ })
60
+ .default({ system: true, bots: true, self: true }),
61
+ })
62
+ .parse(options);
63
+
64
+ typeof config.prefix == 'string' ? (config.prefix = [config.prefix]) : config.prefix;
65
+
66
+ this.client.handler.message = config;
67
+
68
+ const files = this.loadFiles(config.directory);
69
+
70
+ this.loadEvents(XernerxMessageCreateEvent, XernerxMessageUpdateEvent, XernerxMessageDeleteEvent);
71
+
72
+ if (!this.client.options.intents.has('GuildMessages')) new XernerxError(`Missing ${sharpyy('GuildMessages', 'txYellow')} intent, message commands will not work.`);
73
+ if (!this.client.options.intents.has('MessageContent') && !config.mention)
74
+ new XernerxError(`Missing ${sharpyy('MessageContent', 'txYellow')} intent and bot mention is turned off, message commands will not work.`);
75
+ if (!this.client.options.intents.has('MessageContent') && config.mention)
76
+ new XernerxWarn(`Missing ${sharpyy('MessageContent', 'txYellow')} intent, message commands can only be triggered by @mention.`);
77
+
78
+ for (const file of files) {
79
+ await this.loadMessageCommand(file);
80
+ }
81
+
82
+ new XernerxSuccess(`Loaded message commands: ${this.client.commands.message.map((command) => sharpyy(command.id, 'txYellow')).join(', ')}`);
83
+ }
84
+
85
+ /**
86
+ * Loads a slash command from a specified file.
87
+ *
88
+ * @param file - The path to the file containing the slash command to be loaded.
89
+ * @returns A promise that resolves when the file has been successfully loaded.
90
+ */
91
+ public async loadSlashCommand(file: string) {
92
+ return await this.loadFile(file);
93
+ }
94
+
95
+ /**
96
+ * Loads slash commands based on the provided options.
97
+ *
98
+ * @param options - Configuration options for loading slash commands, including the directory where the command files are located.
99
+ * @returns A promise that resolves when all slash commands have been successfully loaded.
100
+ */
101
+ public async loadSlashCommands(options: XernerxSlashCommandHandlerOptions) {
102
+ const config = z
103
+ .object({
104
+ directory: z.string(),
105
+ })
106
+ .parse(options);
107
+
108
+ this.client.handler.slash = config;
109
+
110
+ const files = this.loadFiles(config.directory);
111
+
112
+ for (const file of files) {
113
+ await this.loadSlashCommand(file);
114
+ }
115
+
116
+ this.loadEvents(XernerxInteractionCreateEvent, XernerxSlashCommandInteractionEvent);
117
+
118
+ new XernerxSuccess(`Loaded slash commands: ${this.client.commands.slash.map((command) => sharpyy(command.id, 'txYellow')).join(', ')}`);
119
+ }
120
+
121
+ /**
122
+ * Loads and imports event files based on the provided event classes.
123
+ *
124
+ * @param args - An array of event classes extending XernerxEventBuilder. Each class represents an event to be loaded and imported.
125
+ * @returns A promise that resolves when all specified events have been successfully imported.
126
+ */
127
+ private async loadEvents(...args: Array<typeof XernerxEventBuilder>) {
128
+ for (const event of args) {
129
+ this.importFile(new event('', { name: '' }));
130
+ }
131
+ }
132
+ }
@@ -0,0 +1,45 @@
1
+ /** @format */
2
+
3
+ import sharpyy from 'sharpyy';
4
+
5
+ import { XernerxClient } from '../client/XernerxClient.js';
6
+ import { Handler } from './Handler.js';
7
+ import { XernerxSuccess } from '../tools/XernerxSuccess.js';
8
+
9
+ export class EventHandler extends Handler {
10
+ constructor(client: XernerxClient) {
11
+ super(client);
12
+ }
13
+
14
+ /**
15
+ * Loads a single event file.
16
+ *
17
+ * @param file - The path to the event file to be loaded.
18
+ * @returns A promise that resolves when the event file has been successfully loaded.
19
+ */
20
+ public async loadEvent(file: string) {
21
+ return this.loadFile(file);
22
+ }
23
+
24
+ /**
25
+ * Loads all event files from the specified directory and initializes them.
26
+ *
27
+ * @param options - An object containing options for loading events.
28
+ * @param options.directory - The directory path where event files are located.
29
+ * @returns A promise that resolves when all events have been loaded.
30
+ */
31
+ public async loadEvents(options: { directory: string }) {
32
+ const files = this.loadFiles(options.directory);
33
+
34
+ for (const file of files) {
35
+ await this.loadEvent(file);
36
+ }
37
+
38
+ new XernerxSuccess(
39
+ `Loaded events: ${this.client.events
40
+ .filter((event) => !event.id.startsWith('Xernerx'))
41
+ .map((event) => sharpyy(event.id, 'txYellow'))
42
+ .join(', ')}`
43
+ );
44
+ }
45
+ }