xernerx 4.1.0 → 4.2.1

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 (112) hide show
  1. package/LICENSE +21 -21
  2. package/dist/cjs/build/ContextCommandBuilder.d.ts +68 -0
  3. package/dist/cjs/build/ContextCommandBuilder.js +97 -0
  4. package/dist/cjs/build/EventBuilder.d.ts +21 -0
  5. package/dist/cjs/build/EventBuilder.js +44 -0
  6. package/dist/cjs/build/InhibitorBuilder.d.ts +19 -0
  7. package/dist/cjs/build/InhibitorBuilder.js +37 -0
  8. package/dist/cjs/build/MessageCommandBuilder.d.ts +59 -0
  9. package/dist/cjs/build/MessageCommandBuilder.js +100 -0
  10. package/dist/cjs/build/SlashCommandBuilder.d.ts +64 -0
  11. package/dist/cjs/build/SlashCommandBuilder.js +168 -0
  12. package/dist/cjs/client/XernerxClient.d.ts +28 -0
  13. package/dist/cjs/client/XernerxClient.js +116 -0
  14. package/dist/cjs/handlers/CommandHandler.d.ts +27 -0
  15. package/dist/cjs/handlers/CommandHandler.js +45 -0
  16. package/dist/cjs/handlers/EventHandler.d.ts +13 -0
  17. package/dist/cjs/handlers/EventHandler.js +19 -0
  18. package/dist/cjs/handlers/Handler.d.ts +57 -0
  19. package/dist/cjs/handlers/Handler.js +308 -0
  20. package/dist/cjs/handlers/InhibitorHandler.d.ts +18 -0
  21. package/dist/cjs/handlers/InhibitorHandler.js +24 -0
  22. package/dist/cjs/main.d.ts +44 -0
  23. package/dist/cjs/main.js +112 -0
  24. package/dist/cjs/models/CommandArguments.d.ts +17 -0
  25. package/dist/cjs/models/CommandArguments.js +106 -0
  26. package/dist/cjs/models/Events.d.ts +24 -0
  27. package/dist/cjs/models/Events.js +307 -0
  28. package/dist/cjs/models/Extensions.d.ts +7 -0
  29. package/dist/cjs/models/Extensions.js +26 -0
  30. package/dist/cjs/tools/XernerxError.d.ts +5 -0
  31. package/dist/cjs/tools/XernerxError.js +11 -0
  32. package/dist/cjs/tools/package.d.ts +49 -0
  33. package/dist/cjs/tools/package.js +50 -0
  34. package/dist/cjs/types/enums.d.ts +44 -0
  35. package/dist/cjs/types/enums.js +38 -0
  36. package/dist/cjs/types/interfaces.d.ts +25 -0
  37. package/dist/cjs/types/interfaces.js +2 -0
  38. package/dist/cjs/types/options.d.ts +241 -0
  39. package/dist/cjs/types/options.js +2 -0
  40. package/dist/cjs/types/types.d.ts +61 -0
  41. package/dist/cjs/types/types.js +2 -0
  42. package/dist/cjs/utils/ClientUtil.d.ts +25 -0
  43. package/dist/cjs/utils/ClientUtil.js +211 -0
  44. package/dist/cjs/utils/CommandUtil.d.ts +58 -0
  45. package/dist/cjs/utils/CommandUtil.js +247 -0
  46. package/dist/cjs/validators/CommandValidations.d.ts +12 -0
  47. package/dist/cjs/validators/CommandValidations.js +169 -0
  48. package/dist/cjs/validators/InhibitorValidations.d.ts +11 -0
  49. package/dist/cjs/validators/InhibitorValidations.js +40 -0
  50. package/dist/client/XernerxClient.d.ts +5 -4
  51. package/dist/client/XernerxClient.js +2 -10
  52. package/dist/functions/deploy.js +20 -20
  53. package/dist/handlers/CommandHandler.js +28 -13
  54. package/dist/main.d.ts +1 -1
  55. package/dist/main.js +1 -1
  56. package/dist/mjs/build/ContextCommandBuilder.d.ts +68 -0
  57. package/dist/mjs/build/ContextCommandBuilder.js +88 -0
  58. package/dist/mjs/build/EventBuilder.d.ts +21 -0
  59. package/dist/mjs/build/EventBuilder.js +34 -0
  60. package/dist/mjs/build/InhibitorBuilder.d.ts +19 -0
  61. package/dist/mjs/build/InhibitorBuilder.js +27 -0
  62. package/dist/mjs/build/MessageCommandBuilder.d.ts +59 -0
  63. package/dist/mjs/build/MessageCommandBuilder.js +90 -0
  64. package/dist/mjs/build/SlashCommandBuilder.d.ts +64 -0
  65. package/dist/mjs/build/SlashCommandBuilder.js +121 -0
  66. package/dist/mjs/client/XernerxClient.d.ts +28 -0
  67. package/dist/mjs/client/XernerxClient.js +108 -0
  68. package/dist/mjs/handlers/CommandHandler.d.ts +27 -0
  69. package/dist/mjs/handlers/CommandHandler.js +42 -0
  70. package/dist/mjs/handlers/EventHandler.d.ts +13 -0
  71. package/dist/mjs/handlers/EventHandler.js +16 -0
  72. package/dist/mjs/handlers/Handler.d.ts +57 -0
  73. package/dist/mjs/handlers/Handler.js +260 -0
  74. package/dist/mjs/handlers/InhibitorHandler.d.ts +18 -0
  75. package/dist/mjs/handlers/InhibitorHandler.js +21 -0
  76. package/dist/mjs/main.d.ts +44 -0
  77. package/dist/mjs/main.js +52 -0
  78. package/dist/mjs/models/CommandArguments.d.ts +17 -0
  79. package/dist/mjs/models/CommandArguments.js +101 -0
  80. package/dist/mjs/models/Events.d.ts +24 -0
  81. package/dist/mjs/models/Events.js +295 -0
  82. package/dist/mjs/models/Extensions.d.ts +7 -0
  83. package/dist/mjs/models/Extensions.js +23 -0
  84. package/dist/mjs/tools/XernerxError.d.ts +5 -0
  85. package/dist/mjs/tools/XernerxError.js +8 -0
  86. package/dist/mjs/tools/package.d.ts +49 -0
  87. package/dist/mjs/tools/package.js +48 -0
  88. package/dist/mjs/types/enums.d.ts +44 -0
  89. package/dist/mjs/types/enums.js +35 -0
  90. package/dist/mjs/types/interfaces.d.ts +25 -0
  91. package/dist/mjs/types/interfaces.js +1 -0
  92. package/dist/mjs/types/options.d.ts +241 -0
  93. package/dist/mjs/types/options.js +1 -0
  94. package/dist/mjs/types/types.d.ts +61 -0
  95. package/dist/mjs/types/types.js +1 -0
  96. package/dist/mjs/utils/ClientUtil.d.ts +25 -0
  97. package/dist/mjs/utils/ClientUtil.js +202 -0
  98. package/dist/mjs/utils/CommandUtil.d.ts +58 -0
  99. package/dist/mjs/utils/CommandUtil.js +232 -0
  100. package/dist/mjs/validators/CommandValidations.d.ts +12 -0
  101. package/dist/mjs/validators/CommandValidations.js +166 -0
  102. package/dist/mjs/validators/InhibitorValidations.d.ts +11 -0
  103. package/dist/mjs/validators/InhibitorValidations.js +36 -0
  104. package/dist/tools/XernerxLog.d.ts +1 -0
  105. package/dist/tools/XernerxLog.js +26 -8
  106. package/dist/types/extenders.d.ts +11 -6
  107. package/dist/types/interfaces.d.ts +23 -19
  108. package/dist/utils/ClientUtil.d.ts +2 -1
  109. package/dist/utils/ClientUtil.js +10 -1
  110. package/dist/utils/MessageUtil.d.ts +4 -1
  111. package/dist/utils/MessageUtil.js +4 -2
  112. package/package.json +1 -1
@@ -0,0 +1,121 @@
1
+ import Discord, { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from 'discord.js';
2
+ import { z } from 'zod';
3
+ import XernerxError from '../tools/XernerxError.js';
4
+ import XernerxClient from '../client/XernerxClient.js';
5
+ /**
6
+ * @description - The command builder for slash commands.
7
+ * @param {String} id - The unique ID of the command.
8
+ * @param {SlashCommandOptions} options - The command options.
9
+ */
10
+ export default class SlashCommandBuilder {
11
+ id;
12
+ data;
13
+ name;
14
+ description;
15
+ info;
16
+ category;
17
+ channelType;
18
+ cooldown;
19
+ ignore;
20
+ strict;
21
+ permissions;
22
+ args;
23
+ subcommands;
24
+ groups;
25
+ defer;
26
+ client;
27
+ constructor(id, options) {
28
+ this.id = id;
29
+ this.data = new Discord.SlashCommandBuilder().setName(options.name).setDescription(options.description);
30
+ if (options.args && options?.args?.length > 0) {
31
+ this.addArgs(this.data, options.args);
32
+ }
33
+ if (options.subcommands && options?.subcommands?.length > 0) {
34
+ this.addSubcommands(this.data, options.subcommands);
35
+ }
36
+ if (options.groups && options?.groups?.length > 0) {
37
+ this.addSubcommandGroups(options.groups);
38
+ }
39
+ z.object({
40
+ description: z.string(),
41
+ info: z.string(),
42
+ category: z.string(),
43
+ channelType: z.array(z.number()).or(z.number()),
44
+ cooldown: z.number(),
45
+ ignore: z.object({
46
+ owner: z.boolean(),
47
+ users: z.array(z.string()),
48
+ channels: z.array(z.string()),
49
+ guilds: z.array(z.string()),
50
+ }),
51
+ strict: z.object({
52
+ owner: z.boolean(),
53
+ users: z.array(z.string()),
54
+ channels: z.array(z.string()),
55
+ guilds: z.array(z.string()),
56
+ }),
57
+ permissions: z.object({
58
+ client: z.array(z.string()),
59
+ users: z.array(z.string()),
60
+ dm: z.boolean(),
61
+ }),
62
+ defer: z.object({
63
+ reply: z.boolean(),
64
+ ephemeral: z.boolean(),
65
+ fetch: z.boolean(),
66
+ }),
67
+ })
68
+ .deepPartial()
69
+ .parse(options);
70
+ this.name = options.name;
71
+ this.description = options.description;
72
+ this.info = options.info;
73
+ this.category = options.category;
74
+ this.channelType = options.channelType;
75
+ this.cooldown = options.cooldown;
76
+ this.ignore = options.ignore;
77
+ this.strict = options.strict;
78
+ this.permissions = options.permissions;
79
+ this.defer = options.defer;
80
+ this.client = XernerxClient;
81
+ }
82
+ /**
83
+ * TODO - update description
84
+ * @param interaction
85
+ */
86
+ async conditions(interaction) {}
87
+ /**
88
+ * TODO - update description
89
+ * @param interaction
90
+ */
91
+ async exec(interaction) {}
92
+ addArgs(command, args) {
93
+ const types = ['boolean', 'integer', 'number', 'string', 'user', 'role', 'channel', 'mentionable'];
94
+ for (const argument of args) {
95
+ if (!types.includes(argument.type.toLowerCase())) throw new XernerxError(`Expected one of ${types.join(', ')}, received ${argument.type} instead.`);
96
+ let slashArgumentType = `${argument.type.slice(0, 1).toUpperCase()}${argument.type.slice(1).toLowerCase()}`;
97
+ command[`add${slashArgumentType}Option`]((option) => {
98
+ option
99
+ .setName(argument.name)
100
+ .setDescription(argument.description)
101
+ .setRequired(argument.required || false);
102
+ if (argument.choices) option.setChoices(...argument?.choices);
103
+ return option;
104
+ });
105
+ }
106
+ }
107
+ addSubcommands(method, subcommands) {
108
+ for (const subcommand of subcommands) {
109
+ let sub = new SlashCommandSubcommandBuilder().setName(subcommand.name).setDescription(subcommand.description);
110
+ if (subcommand.args?.length > 0) this.addArgs(sub, subcommand.args);
111
+ method.addSubcommand(sub);
112
+ }
113
+ }
114
+ addSubcommandGroups(groups) {
115
+ for (const group of groups) {
116
+ let subcommandGroup = new SlashCommandSubcommandGroupBuilder().setName(group.name).setDescription(group.description);
117
+ if (group.subcommands?.length > 0) this.addSubcommands(subcommandGroup, group.subcommands);
118
+ this.data.addSubcommandGroup(subcommandGroup);
119
+ }
120
+ }
121
+ }
@@ -0,0 +1,28 @@
1
+ import { Client, Collection } from 'discord.js';
2
+ import ExtensionBuilder from 'xernerx-extension-builder';
3
+ import { ClientUtil } from '../utils/ClientUtil.js';
4
+ import { ClientOptions, DiscordOptions, ExtensionOptions, HandlerOptions } from '../types/options.js';
5
+ import { ClientCache, ClientCommands, ClientModules } from '../types/interfaces.js';
6
+ import EventBuilder from '../build/EventBuilder.js';
7
+ import InhibitorBuilder from '../build/InhibitorBuilder.js';
8
+ /**
9
+ * @description - The Client.
10
+ * @param {DiscordOptions} discordOptions - The options for discord.js.
11
+ * @param {ClientOptions} clientOptions - The options for the Xernerx Client.
12
+ * @extends {Client}
13
+ */
14
+ export default class XernerxClient extends Client {
15
+ settings: ClientOptions;
16
+ commands: ClientCommands;
17
+ cache: ClientCache;
18
+ modules: ClientModules;
19
+ util: ClientUtil;
20
+ handlerOptions: HandlerOptions;
21
+ events: Collection<string, EventBuilder>;
22
+ extensions: Record<string, ExtensionBuilder>;
23
+ inhibitors: Collection<string, InhibitorBuilder>;
24
+ config: object;
25
+ constructor(discordOptions: DiscordOptions, clientOptions: ClientOptions, config?: object);
26
+ register(token: string): void;
27
+ loadAllExtensions(options: ExtensionOptions): Promise<void>;
28
+ }
@@ -0,0 +1,108 @@
1
+ import { Client, Collection } from 'discord.js';
2
+ import { z } from 'zod';
3
+ import { Style } from 'dumfunctions';
4
+ import { ClientUtil } from '../utils/ClientUtil.js';
5
+ import CommandHandler from '../handlers/CommandHandler.js';
6
+ import EventHandler from '../handlers/EventHandler.js';
7
+ import InhibitorHandler from '../handlers/InhibitorHandler.js';
8
+ import Extensions from '../models/Extensions.js';
9
+ // TODO - Add commandNotFound as an event to the client.
10
+ /**
11
+ * @description - The Client.
12
+ * @param {DiscordOptions} discordOptions - The options for discord.js.
13
+ * @param {ClientOptions} clientOptions - The options for the Xernerx Client.
14
+ * @extends {Client}
15
+ */
16
+ export default class XernerxClient extends Client {
17
+ settings;
18
+ commands;
19
+ cache;
20
+ modules;
21
+ util;
22
+ handlerOptions;
23
+ events;
24
+ extensions;
25
+ inhibitors;
26
+ config;
27
+ constructor(discordOptions, clientOptions, config) {
28
+ super(discordOptions);
29
+ this.settings = z
30
+ .object({
31
+ ownerId: z.string().or(z.array(z.string())).default([]),
32
+ permissions: z
33
+ .object({
34
+ client: z.array(z.string()).default([]),
35
+ user: z.array(z.string()).default([]),
36
+ dm: z.boolean().default(false),
37
+ })
38
+ .optional(),
39
+ ignore: z
40
+ .object({
41
+ owner: z.boolean().default(false),
42
+ users: z.array(z.string()),
43
+ channels: z.array(z.string()),
44
+ guilds: z.array(z.string()),
45
+ })
46
+ .optional(),
47
+ log: z
48
+ .object({
49
+ ready: z.boolean().default(false),
50
+ error: z.boolean().default(false),
51
+ })
52
+ .optional(),
53
+ cooldown: z
54
+ .object({
55
+ command: z.number().default(0),
56
+ cache: z.number().default(0),
57
+ })
58
+ .optional(),
59
+ })
60
+ .parse(clientOptions);
61
+ this.config = config || {};
62
+ this.commands = {
63
+ message: new Collection(),
64
+ slash: new Collection(),
65
+ context: new Collection(),
66
+ };
67
+ this.events = new Collection();
68
+ this.inhibitors = new Collection();
69
+ this.cache = {
70
+ messages: new Collection(),
71
+ cooldowns: new Collection(),
72
+ messageCommands: new Collection(),
73
+ slashCommands: new Collection(),
74
+ contextCommands: new Collection(),
75
+ commands: new Collection(),
76
+ };
77
+ this.modules = {
78
+ commandHandler: new CommandHandler(this),
79
+ eventHandler: new EventHandler(this),
80
+ inhibitorHandler: new InhibitorHandler(this),
81
+ };
82
+ this.extensions = {};
83
+ this.util = new ClientUtil(this);
84
+ this.handlerOptions = {};
85
+ if (this.settings?.log?.ready) {
86
+ this.once('ready', async (client) => {
87
+ const size = (await client.guilds.fetch()).size;
88
+ console.info(
89
+ Style.log(
90
+ `Xernerx | ${client.user.tag} signed in watching ${size} server${size > 1 ? 's' : ''}. ${
91
+ (this.handlerOptions.slash || this.handlerOptions.context)?.guildId
92
+ ? `Using ${(await this.guilds.fetch((this.handlerOptions.slash || this.handlerOptions.context)?.guildId || '0')).name} as local guild.`
93
+ : ''
94
+ }`,
95
+ { color: Style.TextColor.Purple }
96
+ )
97
+ );
98
+ });
99
+ }
100
+ }
101
+ register(token) {
102
+ this.login(token);
103
+ }
104
+ loadAllExtensions(options) {
105
+ const extensions = new Extensions(this);
106
+ return extensions.load(options.extensions, options.logging || false);
107
+ }
108
+ }
@@ -0,0 +1,27 @@
1
+ import XernerxClient from '../client/XernerxClient.js';
2
+ import { Handler } from './Handler.js';
3
+ import { ContextCommandHandlerOptions, MessageCommandHandlerOptions, SlashCommandHandlerOptions } from '../types/options.js';
4
+ /**
5
+ * @description - The command handler.
6
+ * @param {XernerxClient} client - The XernerxClient.
7
+ */
8
+ export default class CommandHandler {
9
+ client: XernerxClient;
10
+ handler: Handler;
11
+ constructor(client: XernerxClient);
12
+ /**
13
+ * @description - The message command loader.
14
+ * @param {MessageCommandOptions} options - message command loader options.
15
+ */
16
+ loadAllMessageCommands(options: MessageCommandHandlerOptions): void;
17
+ /**
18
+ * @description - The slash command loader.
19
+ * @param {SlashCommandOptions} options - slash command loader options.
20
+ */
21
+ loadAllSlashCommands(options: SlashCommandHandlerOptions): void;
22
+ /**
23
+ * @description - The context command loader.
24
+ * @param {ContextCommandOptions} options - context command loader options.
25
+ */
26
+ loadAllContextCommands(options: ContextCommandHandlerOptions): void;
27
+ }
@@ -0,0 +1,42 @@
1
+ import { Handler } from './Handler.js';
2
+ import { CommandsDeploy, ContextCommandEvents, MessageCommandEvents, SlashCommandEvents } from '../models/Events.js';
3
+ /**
4
+ * @description - The command handler.
5
+ * @param {XernerxClient} client - The XernerxClient.
6
+ */
7
+ export default class CommandHandler {
8
+ client;
9
+ handler;
10
+ constructor(client) {
11
+ this.client = client;
12
+ this.handler = new Handler(client);
13
+ new CommandsDeploy(this.client).deploy();
14
+ }
15
+ /**
16
+ * @description - The message command loader.
17
+ * @param {MessageCommandOptions} options - message command loader options.
18
+ */
19
+ loadAllMessageCommands(options) {
20
+ this.handler.loadAllMessageCommands(options);
21
+ const events = new MessageCommandEvents(this.client);
22
+ events.messageCreate();
23
+ events.messageUpdate();
24
+ events.messageDelete();
25
+ }
26
+ /**
27
+ * @description - The slash command loader.
28
+ * @param {SlashCommandOptions} options - slash command loader options.
29
+ */
30
+ loadAllSlashCommands(options) {
31
+ this.handler.loadAllSlashCommands(options);
32
+ new SlashCommandEvents(this.client).slashCreate();
33
+ }
34
+ /**
35
+ * @description - The context command loader.
36
+ * @param {ContextCommandOptions} options - context command loader options.
37
+ */
38
+ loadAllContextCommands(options) {
39
+ this.handler.loadAllContextCommands(options);
40
+ new ContextCommandEvents(this.client).contextCreate();
41
+ }
42
+ }
@@ -0,0 +1,13 @@
1
+ import XernerxClient from '../client/XernerxClient.js';
2
+ import { EventHandlerOptions } from '../types/options.js';
3
+ import { Handler } from './Handler.js';
4
+ export default class EventHandler {
5
+ client: XernerxClient;
6
+ handler: Handler;
7
+ constructor(client: XernerxClient);
8
+ /**
9
+ * @description - The event loader
10
+ * @param {EventLoadOptions} options - message command options
11
+ */
12
+ loadAllEvents(options: EventHandlerOptions): void;
13
+ }
@@ -0,0 +1,16 @@
1
+ import { Handler } from './Handler.js';
2
+ export default class EventHandler {
3
+ client;
4
+ handler;
5
+ constructor(client) {
6
+ this.client = client;
7
+ this.handler = new Handler(client);
8
+ }
9
+ /**
10
+ * @description - The event loader
11
+ * @param {EventLoadOptions} options - message command options
12
+ */
13
+ loadAllEvents(options) {
14
+ return this.handler.loadAllEvents(options);
15
+ }
16
+ }
@@ -0,0 +1,57 @@
1
+ import XernerxClient from '../client/XernerxClient.js';
2
+ import { ContextCommandHandlerOptions, EventHandlerOptions, InhibitorHandlerOptions, MessageCommandHandlerOptions, SlashCommandHandlerOptions } from '../types/options.js';
3
+ /**
4
+ * @description - The Handler class.
5
+ * @param {XernerxClient} client - The XernerxClient.
6
+ */
7
+ export declare class Handler {
8
+ private client;
9
+ commands: object[];
10
+ readyTimestamp: number;
11
+ constructor(client: XernerxClient);
12
+ /**
13
+ * @description - The loader for the message commands.
14
+ * @param {MessageCommandOptions} options - The options for the message command loader.
15
+ */
16
+ loadAllMessageCommands(options: MessageCommandHandlerOptions): void;
17
+ /**
18
+ * @description - The loader for the slash commands.
19
+ * @param {SlashCommandOptions} options - The options for the slash command loader.
20
+ */
21
+ loadAllSlashCommands(options: SlashCommandHandlerOptions): void;
22
+ /**
23
+ * @description - The loader for the context commands.
24
+ * @param {ContextCommandOptions} options - The options for the context command loader.
25
+ */
26
+ loadAllContextCommands(options: ContextCommandHandlerOptions): void;
27
+ /**
28
+ * @description - The loader for the events.
29
+ * @param {EventLoadOptions} options - The options for the event loader.
30
+ */
31
+ loadAllEvents(options: EventHandlerOptions): void;
32
+ /**
33
+ * @description - The loader for the inhibitors.
34
+ * @param {InhibitorLoadOptions} options - The options for the inhibitor loader.
35
+ */
36
+ loadAllInhibitors(options: InhibitorHandlerOptions): void;
37
+ /**
38
+ * @description - a reader for directories
39
+ * @param {string} dir - the directory to look for.
40
+ * @returns array of files.
41
+ */
42
+ private readdir;
43
+ /**
44
+ * @description file loader.
45
+ * @param {string} dir - the directory.
46
+ * @param {string} file - the file in the directory.
47
+ * @param {string} type - the type of file.
48
+ * @returns undefined
49
+ */
50
+ private load;
51
+ /**
52
+ * @description the event emitter to emit all events.
53
+ * @param {object} event - the event to be emitted.
54
+ * @returns void
55
+ */
56
+ private emitter;
57
+ }
@@ -0,0 +1,260 @@
1
+ import fs from 'fs';
2
+ import * as path from 'path';
3
+ import { CommandType } from '../types/enums.js';
4
+ import { s } from '@sapphire/shapeshift';
5
+ import { Style } from 'dumfunctions';
6
+ // TODO - Add reload<handlerFunction>
7
+ // TODO - Add unload<handlerFunction>
8
+ // TODO - Rename load<handlerFunction>
9
+ // TODO - Update validator to zod
10
+ /**
11
+ * @description - The Handler class.
12
+ * @param {XernerxClient} client - The XernerxClient.
13
+ */
14
+ export class Handler {
15
+ client;
16
+ commands;
17
+ readyTimestamp;
18
+ constructor(client) {
19
+ this.client = client;
20
+ this.commands = [];
21
+ this.readyTimestamp = Number(Date.now());
22
+ }
23
+ /**
24
+ * @description - The loader for the message commands.
25
+ * @param {MessageCommandOptions} options - The options for the message command loader.
26
+ */
27
+ loadAllMessageCommands(options) {
28
+ this.client.handlerOptions.message = s
29
+ .object({
30
+ directory: s.string,
31
+ prefix: s.union(s.string, s.array(s.string)),
32
+ allowMention: s.boolean.default(false),
33
+ cooldown: s.number.default(0),
34
+ userPermissions: s.array(s.bigint).default([]),
35
+ clientPermissions: s.array(s.bigint).default([]),
36
+ handleEdits: s.boolean.default(false),
37
+ handleDeletes: s.boolean.default(false),
38
+ handleTyping: s.boolean.default(false),
39
+ logging: s.boolean.default(false),
40
+ })
41
+ .parse(options);
42
+ const files = this.readdir(options.directory),
43
+ loaded = [];
44
+ for (const file of files) {
45
+ this.load(options.directory, file, CommandType.MessageCommand);
46
+ loaded.push(file.replace('.js', ''));
47
+ }
48
+ if (this.client.handlerOptions.message?.logging)
49
+ console.info(
50
+ Style.log(`Xernerx | Loaded ${loaded.length} message commands: ${loaded.join(', ')}.`, {
51
+ color: Style.TextColor.Cyan,
52
+ })
53
+ );
54
+ }
55
+ /**
56
+ * @description - The loader for the slash commands.
57
+ * @param {SlashCommandOptions} options - The options for the slash command loader.
58
+ */
59
+ loadAllSlashCommands(options) {
60
+ this.client.handlerOptions.slash = s
61
+ .object({
62
+ directory: s.string,
63
+ guildId: s.string,
64
+ global: s.boolean,
65
+ cooldown: s.number.default(0),
66
+ userPermissions: s.array(s.bigint).default([]),
67
+ clientPermissions: s.array(s.bigint).default([]),
68
+ logging: s.boolean.default(false),
69
+ defer: s.object({
70
+ reply: s.boolean.optional,
71
+ ephemeral: s.boolean.optional,
72
+ fetchReply: s.boolean.optional,
73
+ }).optional,
74
+ })
75
+ .parse(options);
76
+ const files = this.readdir(options.directory),
77
+ loaded = [];
78
+ for (const file of files) {
79
+ this.load(options.directory, file, CommandType.SlashCommand);
80
+ loaded.push(file.replace('.js', ''));
81
+ }
82
+ if (this.client.handlerOptions.slash?.logging)
83
+ console.info(
84
+ Style.log(`Xernerx | Loaded ${loaded.length} slash commands: ${loaded.join(', ')}.`, {
85
+ color: Style.TextColor.Cyan,
86
+ })
87
+ );
88
+ }
89
+ /**
90
+ * @description - The loader for the context commands.
91
+ * @param {ContextCommandOptions} options - The options for the context command loader.
92
+ */
93
+ loadAllContextCommands(options) {
94
+ this.client.handlerOptions.context = s
95
+ .object({
96
+ directory: s.string,
97
+ guildId: s.string,
98
+ global: s.boolean,
99
+ cooldown: s.number.default(0),
100
+ userPermissions: s.array(s.bigint).default([]),
101
+ clientPermissions: s.array(s.bigint).default([]),
102
+ logging: s.boolean.default(false),
103
+ defer: s.object({
104
+ reply: s.boolean.optional,
105
+ ephemeral: s.boolean.optional,
106
+ fetchReply: s.boolean.optional,
107
+ }).optional,
108
+ })
109
+ .parse(options);
110
+ const files = this.readdir(options.directory),
111
+ loaded = [];
112
+ for (const file of files) {
113
+ this.load(options.directory, file, CommandType.ContextCommand);
114
+ loaded.push(file.replace('.js', ''));
115
+ }
116
+ if (this.client.handlerOptions.context?.logging)
117
+ console.info(
118
+ Style.log(`Xernerx | Loaded ${loaded.length} context commands: ${loaded.join(', ')}.`, {
119
+ color: Style.TextColor.Cyan,
120
+ })
121
+ );
122
+ }
123
+ /**
124
+ * @description - The loader for the events.
125
+ * @param {EventLoadOptions} options - The options for the event loader.
126
+ */
127
+ loadAllEvents(options) {
128
+ this.client.handlerOptions.events = s
129
+ .object({
130
+ directory: s.string,
131
+ logging: s.boolean.default(false),
132
+ })
133
+ .parse(options);
134
+ const files = this.readdir(options.directory),
135
+ loaded = [];
136
+ (async () => {
137
+ for (const file of files) {
138
+ try {
139
+ options.directory.split(/\//).shift();
140
+ let event = (await import('file://' + `${path.resolve(options.directory)}/${file}`)).default;
141
+ event = new event();
142
+ event.client = this.client;
143
+ event.commandType = CommandType.Event;
144
+ this.client.events.set(event.name, event);
145
+ this.emitter(event);
146
+ } catch (error) {
147
+ console.error(Style.log(`Xernerx | Couldn't load ${file} because <${error}>`, { color: Style.BackgroundColor.Red }));
148
+ }
149
+ loaded.push(file.replace('.js', ''));
150
+ }
151
+ if (this.client.handlerOptions.events?.logging) console.info(Style.log(`Xernerx | Loaded ${loaded.length} events: ${loaded.join(', ')}.`, { color: Style.TextColor.Cyan }));
152
+ })();
153
+ }
154
+ /**
155
+ * @description - The loader for the inhibitors.
156
+ * @param {InhibitorLoadOptions} options - The options for the inhibitor loader.
157
+ */
158
+ loadAllInhibitors(options) {
159
+ this.client.handlerOptions.inhibitors = s
160
+ .object({
161
+ directory: s.string,
162
+ logging: s.boolean.default(false),
163
+ })
164
+ .parse(options);
165
+ const files = this.readdir(options.directory),
166
+ loaded = [];
167
+ for (const file of files) {
168
+ this.load(options.directory, file, CommandType.Inhibitor);
169
+ loaded.push(file.replace('.js', ''));
170
+ }
171
+ if (this.client.handlerOptions.inhibitors?.logging)
172
+ console.info(
173
+ Style.log(`Xernerx | Loaded ${loaded.length} inhibitors: ${loaded.join(', ')}.`, {
174
+ color: Style.TextColor.Cyan,
175
+ })
176
+ );
177
+ }
178
+ /**
179
+ * @description - a reader for directories
180
+ * @param {string} dir - the directory to look for.
181
+ * @returns array of files.
182
+ */
183
+ readdir(dir) {
184
+ try {
185
+ return fs.readdirSync(path.resolve(dir)).filter((file) => file.endsWith('.js'));
186
+ } catch (error) {
187
+ console.error(Style.log(`Xernerx | Couldn't read ${dir} because <${error}>.`, { color: Style.BackgroundColor.Red }));
188
+ }
189
+ return [];
190
+ }
191
+ /**
192
+ * @description file loader.
193
+ * @param {string} dir - the directory.
194
+ * @param {string} file - the file in the directory.
195
+ * @param {string} type - the type of file.
196
+ * @returns undefined
197
+ */
198
+ async load(dir, file, type) {
199
+ try {
200
+ dir.split(/\//).shift();
201
+ let command = (await import('file://' + `${path.resolve(dir)}/${file}`)).default;
202
+ command = new command();
203
+ command.client = this.client;
204
+ command.commandType = type;
205
+ command.filepath = `${path.resolve(dir)}\\${file}`;
206
+ command.file = file;
207
+ if (type === CommandType.MessageCommand) this.client.commands.message.set(command?.name || command?.name, command);
208
+ if (type === CommandType.SlashCommand) {
209
+ this.client.commands.slash.set(command?.data?.name || command?.name, command);
210
+ }
211
+ if (type === CommandType.ContextCommand) {
212
+ this.client.commands.context.set(command?.data?.name || command?.name, command);
213
+ }
214
+ if (type === CommandType.Event) {
215
+ this.client.events.set(command.name, command);
216
+ }
217
+ if (type === CommandType.Inhibitor) {
218
+ this.client.inhibitors.set(command.name, command);
219
+ }
220
+ } catch (error) {
221
+ console.error(Style.log(`Xernerx | Couldn't load ${file} because <${error}>`, { color: Style.BackgroundColor.Red }));
222
+ }
223
+ }
224
+ /**
225
+ * @description the event emitter to emit all events.
226
+ * @param {object} event - the event to be emitted.
227
+ * @returns void
228
+ */
229
+ emitter(event) {
230
+ const client = this.client;
231
+ if (event.emitter === 'client') {
232
+ if (event.once)
233
+ client.once(event.name, (...args) => {
234
+ event.run(...args);
235
+ });
236
+ else
237
+ client.on(event.name, (...args) => {
238
+ event.run(...args);
239
+ });
240
+ } else if (event.emitter === 'rest') {
241
+ if (event.once)
242
+ client.once(event.name, (...args) => {
243
+ event.run(...args);
244
+ });
245
+ else
246
+ client.on(event.name, (...args) => {
247
+ event.run(...args);
248
+ });
249
+ } else if (event.emitter === 'process') {
250
+ if (event.once)
251
+ process.once(event.name, (...args) => {
252
+ event.run(...args);
253
+ });
254
+ else
255
+ process.on(event.name, (...args) => {
256
+ event.run(...args);
257
+ });
258
+ }
259
+ }
260
+ }