xernerx 3.3.7 → 3.4.0

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 (50) hide show
  1. package/dist/build/ContextCommandBuilder.d.ts +3 -1
  2. package/dist/build/ContextCommandBuilder.js +5 -1
  3. package/dist/build/EventBuilder.d.ts +1 -1
  4. package/dist/build/EventBuilder.js +1 -1
  5. package/dist/build/InhibitorBuilder.d.ts +2 -1
  6. package/dist/build/InhibitorBuilder.js +1 -1
  7. package/dist/build/MessageCommandBuilder.d.ts +1 -1
  8. package/dist/build/MessageCommandBuilder.js +1 -1
  9. package/dist/build/SlashCommandBuilder.d.ts +6 -2
  10. package/dist/build/SlashCommandBuilder.js +20 -12
  11. package/dist/build/XContextCommandBuilder.d.ts +40 -0
  12. package/dist/build/XContextCommandBuilder.js +84 -0
  13. package/dist/build/XEventBuilder.d.ts +17 -0
  14. package/dist/build/XEventBuilder.js +35 -0
  15. package/dist/build/XInhibitorBuilder.d.ts +20 -0
  16. package/dist/build/XInhibitorBuilder.js +28 -0
  17. package/dist/build/XMessageCommandBuilder.d.ts +42 -0
  18. package/dist/build/XMessageCommandBuilder.js +80 -0
  19. package/dist/build/XSlashCommandBuilder.d.ts +36 -0
  20. package/dist/build/XSlashCommandBuilder.js +114 -0
  21. package/dist/client/XernerxClient.d.ts +1 -1
  22. package/dist/client/XernerxClient.js +1 -2
  23. package/dist/handlers/CommandHandler.d.ts +4 -5
  24. package/dist/handlers/CommandHandler.js +2 -2
  25. package/dist/handlers/EventHandler.d.ts +4 -5
  26. package/dist/handlers/EventHandler.js +1 -1
  27. package/dist/handlers/Handler.d.ts +25 -6
  28. package/dist/handlers/Handler.js +41 -36
  29. package/dist/handlers/InhibitorHandler.d.ts +2 -3
  30. package/dist/main.d.ts +5 -71
  31. package/dist/main.js +6 -11
  32. package/dist/models/CommandArguments.d.ts +2 -2
  33. package/dist/models/CommandArguments.js +4 -2
  34. package/dist/models/Events.d.ts +5 -5
  35. package/dist/models/Events.js +69 -24
  36. package/dist/models/Extensions.d.ts +4 -3
  37. package/dist/models/Extensions.js +5 -3
  38. package/dist/tools/package.d.ts +3 -3
  39. package/dist/tools/package.js +31 -35
  40. package/dist/types/enums.js +4 -4
  41. package/dist/types/options.d.ts +3 -2
  42. package/dist/utils/ClientUtil.d.ts +1 -1
  43. package/dist/utils/ClientUtil.js +0 -11
  44. package/dist/utils/CommandUtil.d.ts +12 -6
  45. package/dist/utils/CommandUtil.js +26 -14
  46. package/dist/validators/CommandValidations.d.ts +7 -0
  47. package/dist/validators/CommandValidations.js +185 -0
  48. package/dist/validators/InhibitorValidations.d.ts +11 -0
  49. package/dist/validators/InhibitorValidations.js +42 -0
  50. package/package.json +1 -1
@@ -0,0 +1,185 @@
1
+ import { Message } from 'discord.js';
2
+ import { CommandType } from '../types/enums.js';
3
+ function getUser(from) {
4
+ if (from instanceof Message)
5
+ return from.author;
6
+ else
7
+ return from.user;
8
+ }
9
+ export default function commandValidation(action, command, client, res = false) {
10
+ let user = getUser(action);
11
+ if ((command.ignoreOwner || client.settings.ignoreOwner) && client.util.isOwner(user.id))
12
+ return res;
13
+ else if (inCooldown(user, client, command, action))
14
+ return true;
15
+ else if (command.owner && !client.util.isOwner(user.id)) {
16
+ res = true;
17
+ emit(client, action, command, 'Not an owner', client.settings.ownerId);
18
+ }
19
+ else if (command.channelType?.length > 0 || command.channelType) {
20
+ if (!Array.isArray(command.channelType))
21
+ command.channelType = [command.channelType];
22
+ if (!command?.channelType?.includes(action.channel?.type)) {
23
+ res = true;
24
+ emit(client, action, command, 'Not the correct channel', command.channelType);
25
+ }
26
+ }
27
+ else if (command.channels?.length > 0 && !command.channels.includes(action.channel?.id)) {
28
+ res = true;
29
+ emit(client, action, command, 'Not a whitelisted channel', command.channels);
30
+ }
31
+ else if (command.guilds?.length > 0 && !command.guilds.includes(action.guild?.id)) {
32
+ res = true;
33
+ emit(client, action, command, 'Not a whitelisted guild', command.guilds);
34
+ }
35
+ else if (command.channelType && command.channelType !== action.channel?.type) {
36
+ res = true;
37
+ emit(client, action, command, 'Not the correct type of channel', command.channelType);
38
+ }
39
+ else if (action?.guild && (command.userPermissions || client.settings.userPermissions)) {
40
+ const permissions = command.userPermissions ||
41
+ client?.settings?.userPermissions?.push(...handlerPermissions(client, command.commandType, 'user')), missing = [];
42
+ if (Array.isArray(permissions))
43
+ permissions.map((permission) => {
44
+ if (!(action.member?.permissions).has(permission)) {
45
+ missing.push(permission);
46
+ }
47
+ });
48
+ if (missing.length > 0) {
49
+ res = true;
50
+ emit(client, action, command, 'Missing User Permissions', missing);
51
+ }
52
+ }
53
+ else if (action?.guild && (command.clientPermissions || client.settings.clientPermissions)) {
54
+ const permissions = command.clientPermissions ||
55
+ client?.settings?.clientPermissions?.push(...handlerPermissions(client, command.commandType, 'client')), missing = [];
56
+ if (Array.isArray(permissions))
57
+ permissions.map((permission) => {
58
+ if (!(action.member?.permissions).has(permission)) {
59
+ missing.push(permission);
60
+ }
61
+ });
62
+ if (missing.length > 0) {
63
+ res = true;
64
+ emit(client, action, command, 'Missing Client Permissions', missing);
65
+ }
66
+ }
67
+ return res;
68
+ }
69
+ function emit(client, action, command, reason, extra) {
70
+ return client.emit('commandBlock', action, command, reason, extra);
71
+ }
72
+ function inCooldown(user, client, command, action) {
73
+ let res = false, downs = {};
74
+ const { cooldowns, messageCommands, slashCommands, contextCommands, commands } = client.cache;
75
+ if (command.commandType === CommandType.MessageCommand && client.handlerOptions.message.cooldown) {
76
+ if (messageCommands.has(user.id)) {
77
+ const usr = messageCommands.get(user.id);
78
+ usr.timeRemaining = usr.endsAt - Date.now();
79
+ downs.message = usr;
80
+ res = true;
81
+ }
82
+ if (!messageCommands.has(user.id)) {
83
+ messageCommands.set(user.id, toCooldown(action, command, client.handlerOptions.message.cooldown, user.id));
84
+ setInterval(() => {
85
+ messageCommands.delete(user.id);
86
+ }, client.handlerOptions.message.cooldown);
87
+ }
88
+ }
89
+ if (command.commandType === CommandType.SlashCommand && client.handlerOptions.slash.cooldown) {
90
+ if (slashCommands.has(user.id)) {
91
+ const usr = slashCommands.get(user.id);
92
+ usr.timeRemaining = usr.endsAt - Date.now();
93
+ downs.slash = usr;
94
+ res = true;
95
+ }
96
+ if (!slashCommands.has(user.id)) {
97
+ slashCommands.set(user.id, toCooldown(action, command, client.handlerOptions.slash.cooldown, user.id));
98
+ setInterval(() => {
99
+ slashCommands.delete(user.id);
100
+ }, client.handlerOptions.slash.cooldown);
101
+ }
102
+ }
103
+ if (command.commandType === CommandType.ContextCommand && client.handlerOptions.context.cooldown) {
104
+ if (contextCommands.has(user.id)) {
105
+ const usr = contextCommands.get(user.id);
106
+ usr.timeRemaining = usr.endsAt - Date.now();
107
+ downs.context = usr;
108
+ res = true;
109
+ }
110
+ if (!contextCommands.has(user.id)) {
111
+ contextCommands.set(user.id, toCooldown(action, command, client.handlerOptions.context.cooldown, user.id));
112
+ setInterval(() => {
113
+ contextCommands.delete(user.id);
114
+ }, client.handlerOptions.context.cooldown);
115
+ }
116
+ }
117
+ if (client.settings?.cooldown?.default) {
118
+ if (cooldowns.has(user.id)) {
119
+ const usr = cooldowns.get(user.id);
120
+ usr.timeRemaining = usr.endsAt - Date.now();
121
+ downs.default = usr;
122
+ res = true;
123
+ }
124
+ if (!cooldowns.has(user.id)) {
125
+ cooldowns.set(user.id, toCooldown(action, command, client.settings.cooldown.default, user.id));
126
+ setInterval(() => {
127
+ cooldowns.delete(user.id);
128
+ }, client.settings.cooldown.default);
129
+ }
130
+ }
131
+ if (command.cooldown) {
132
+ const name = user.id + '-' + (command.data?.name || command?.name) + '-' + command.commandType;
133
+ if (commands.has(name)) {
134
+ const usr = commands.get(name);
135
+ usr.timeRemaining = usr.endsAt - Date.now();
136
+ downs.command = usr;
137
+ res = true;
138
+ }
139
+ if (!commands.has(name)) {
140
+ commands.set(name, toCooldown(action, command, command.cooldown, name));
141
+ setInterval(() => {
142
+ commands.delete(name);
143
+ }, command.cooldown);
144
+ }
145
+ }
146
+ if (Object.keys(downs).length > 0)
147
+ client.emit('commandCooldown', action, command, downs);
148
+ return res;
149
+ }
150
+ function toCooldown(action, command, cooldown, name) {
151
+ let user = getUser(action);
152
+ return {
153
+ name,
154
+ id: user.id,
155
+ tag: user.tag,
156
+ commandType: command.commandType,
157
+ commandName: command?.data?.name || command?.name,
158
+ guildId: action?.guild?.id,
159
+ channelId: action?.channel?.id,
160
+ startsAt: Number(Date.now()),
161
+ endsAt: Number(Date.now()) + cooldown,
162
+ };
163
+ }
164
+ function handlerPermissions(client, commandType, type) {
165
+ let permissions = [];
166
+ if (commandType === CommandType.MessageCommand) {
167
+ if (type === 'user')
168
+ permissions = client.handlerOptions.message?.userPermissions || [];
169
+ else if (type === 'client')
170
+ permissions = client.handlerOptions.message?.clientPermissions || [];
171
+ }
172
+ else if (commandType === CommandType.SlashCommand) {
173
+ if (type === 'user')
174
+ permissions = client.handlerOptions.slash?.userPermissions || [];
175
+ else if (type === 'client')
176
+ permissions = client.handlerOptions.slash?.clientPermissions || [];
177
+ }
178
+ else if (commandType === CommandType.ContextCommand) {
179
+ if (type === 'user')
180
+ permissions = client.handlerOptions.context?.userPermissions || [];
181
+ else if (type === 'client')
182
+ permissions = client.handlerOptions.context?.clientPermissions || [];
183
+ }
184
+ return permissions;
185
+ }
@@ -0,0 +1,11 @@
1
+ import XernerxClient from '../client/XernerxClient.js';
2
+ import { XernerxInteraction, XernerxMessage } from '../types/types.js';
3
+ export declare class InhibitorValidation {
4
+ private client;
5
+ private action;
6
+ private command;
7
+ private inhibited;
8
+ constructor(client: XernerxClient, action: XernerxMessage | XernerxInteraction, command: string);
9
+ inhibit(): Promise<Record<string, string | void> | undefined>;
10
+ private args;
11
+ }
@@ -0,0 +1,42 @@
1
+ import { InhibitorType } from '../types/enums.js';
2
+ export class InhibitorValidation {
3
+ client;
4
+ action;
5
+ command;
6
+ inhibited;
7
+ constructor(client, action, command) {
8
+ this.client = client;
9
+ this.action = action;
10
+ this.command = command;
11
+ this.inhibited = {};
12
+ }
13
+ async inhibit() {
14
+ for (const inhibitor of this.client.inhibitors) {
15
+ const inhibit = inhibitor[1];
16
+ if (inhibit)
17
+ this.inhibited[inhibit.name] = await inhibit.check(this.action, this.args(this.action, inhibit.type));
18
+ }
19
+ for (const inhibit of Object.values(this.inhibited)) {
20
+ if (inhibit)
21
+ return this.inhibited;
22
+ }
23
+ }
24
+ args(action, type) {
25
+ if (type === InhibitorType.Channel)
26
+ return action.channel;
27
+ if (type === InhibitorType.User)
28
+ return action.author || action.user;
29
+ if (type === InhibitorType.Guild)
30
+ return action.guild;
31
+ if (type === InhibitorType.Member)
32
+ return action.member;
33
+ // if (type === InhibitorType.Command)
34
+ // return this.client.util.getAllCommands().filter(this.command.name);
35
+ // if (type === InhibitorType.MessageCommand)
36
+ // return this.client.util.getAllMessageCommands().find(this.command.name);
37
+ // if (type === InhibitorType.SlashCommand)
38
+ // return this.client.util.getAllSlashCommands().find(this.command.name);
39
+ // if (type === InhibitorType.ContextCommand)
40
+ // return this.client.util.getAllContextCommands().find(this.command.name);
41
+ }
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xernerx",
3
- "version": "3.3.7",
3
+ "version": "3.4.0",
4
4
  "type": "module",
5
5
  "description": "A bot framework for discord.js.",
6
6
  "main": "dist/main.js",