meocord 2.1.1 → 3.1.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.
- package/AUTHOR.md +2 -3
- package/README.md +289 -41
- package/dist/cjs/_shared/controller.decorator-MUHA_A3z.cjs +529 -0
- package/dist/cjs/core/index.cjs +288 -85
- package/dist/cjs/decorator/index.cjs +5 -1
- package/dist/cjs/enum/index.cjs +17 -4
- package/dist/cjs/testing/index.cjs +88 -4
- package/dist/esm/bin/builder-template/builder/primary-entry-point.builder.template +26 -0
- package/dist/esm/bin/builder-template/controller/autocomplete.controller.template +16 -0
- package/dist/esm/bin/builder-template/controller/channel-select-menu.controller.template +12 -0
- package/dist/esm/bin/builder-template/controller/context-menu.controller.template +2 -2
- package/dist/esm/bin/builder-template/controller/mentionable-select-menu.controller.template +12 -0
- package/dist/esm/bin/builder-template/controller/modal-submit.controller.template +1 -1
- package/dist/esm/bin/builder-template/controller/primary-entry-point.controller.template +12 -0
- package/dist/esm/bin/builder-template/controller/role-select-menu.controller.template +12 -0
- package/dist/esm/bin/builder-template/controller/slash.controller.template +1 -1
- package/dist/esm/bin/builder-template/controller/user-select-menu.controller.template +12 -0
- package/dist/esm/bin/generator.js +4 -9
- package/dist/esm/bin/helper/controller-generator.helper.js +24 -7
- package/dist/esm/core/meocord.app.js +289 -85
- package/dist/esm/decorator/controller.decorator.js +157 -24
- package/dist/esm/decorator/index.js +1 -1
- package/dist/esm/enum/controller.enum.js +23 -4
- package/dist/esm/testing/mock-interaction.js +89 -5
- package/dist/esm/util/interaction.util.js +174 -0
- package/dist/types/controller.enum-DYfhYaat.d.ts +36 -0
- package/dist/types/core/index.d.ts +94 -0
- package/dist/types/decorator/index.d.ts +74 -41
- package/dist/types/enum/index.d.ts +1 -1
- package/dist/types/interface/index.d.ts +87 -3
- package/dist/types/testing/index.d.ts +3 -21
- package/package.json +2 -2
- package/dist/cjs/_shared/controller.decorator-DX5lFlPZ.cjs +0 -216
- package/dist/types/controller.enum-QA-IuReF.d.ts +0 -18
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
require('reflect-metadata');
|
|
4
|
-
var inversify = require('inversify');
|
|
5
|
-
var discord_js = require('discord.js');
|
|
6
|
-
var enum_index = require('../enum/index.cjs');
|
|
7
|
-
var metadataKey_enum = require('./metadata-key.enum-BzzvGUId.cjs');
|
|
8
|
-
|
|
9
|
-
const COMMAND_METADATA_KEY = Symbol('commands');
|
|
10
|
-
const MESSAGE_HANDLER_METADATA_KEY = Symbol('message_handlers');
|
|
11
|
-
const REACTION_HANDLER_METADATA_KEY = Symbol('reaction_handlers');
|
|
12
|
-
/**
|
|
13
|
-
* Decorator to register message handlers in the controller.
|
|
14
|
-
*
|
|
15
|
-
* @param keyword - An optional keyword to filter messages this handler should respond to.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* @MessageHandler('hello')
|
|
20
|
-
* async handleHelloMessage(message: Message) {
|
|
21
|
-
* await message.reply('Hello! How can I help you?');
|
|
22
|
-
* }
|
|
23
|
-
*
|
|
24
|
-
* @MessageHandler()
|
|
25
|
-
* async handleAnyMessage(message: Message) {
|
|
26
|
-
* console.log(`Received a message: ${message.content}`);
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/ function MessageHandler(keyword) {
|
|
30
|
-
return function(target, propertyKey, _descriptor) {
|
|
31
|
-
const handlers = Reflect.getMetadata(MESSAGE_HANDLER_METADATA_KEY, target) || [];
|
|
32
|
-
handlers.push({
|
|
33
|
-
keyword,
|
|
34
|
-
method: propertyKey.toString()
|
|
35
|
-
});
|
|
36
|
-
Reflect.defineMetadata(MESSAGE_HANDLER_METADATA_KEY, handlers, target);
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Decorator to register reaction handlers in the controller.
|
|
41
|
-
*
|
|
42
|
-
* @param emoji - Optional emoji name to filter reactions this handler should respond to.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```typescript
|
|
46
|
-
* @ReactionHandler('👍')
|
|
47
|
-
* async handleThumbsUpReaction(reaction: MessageReaction, { user }: ReactionHandlerOptions) {
|
|
48
|
-
* console.log(`User ${user.username} reacted with 👍`);
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* @ReactionHandler()
|
|
52
|
-
* async handleAnyReaction(reaction: MessageReaction, { user }: ReactionHandlerOptions) {
|
|
53
|
-
* console.log(`User ${user.username} reacted with ${reaction.emoji.name}`);
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*/ function ReactionHandler(emoji) {
|
|
57
|
-
return function(target, propertyKey, _descriptor) {
|
|
58
|
-
const handlers = Reflect.getMetadata(REACTION_HANDLER_METADATA_KEY, target) || [];
|
|
59
|
-
handlers.push({
|
|
60
|
-
emoji,
|
|
61
|
-
method: propertyKey.toString()
|
|
62
|
-
});
|
|
63
|
-
Reflect.defineMetadata(REACTION_HANDLER_METADATA_KEY, handlers, target);
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Retrieves reaction handlers metadata from a given controller.
|
|
68
|
-
*
|
|
69
|
-
* @param controller - The controller class instance.
|
|
70
|
-
* @returns An array of reaction handler metadata objects.
|
|
71
|
-
*/ function getReactionHandlers(controller) {
|
|
72
|
-
return Reflect.getMetadata(REACTION_HANDLER_METADATA_KEY, controller) || [];
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Retrieves message handlers metadata from a given controller.
|
|
76
|
-
*
|
|
77
|
-
* @param controller - The controller class instance.
|
|
78
|
-
* @returns An array of message handler method names.
|
|
79
|
-
*/ function getMessageHandlers(controller) {
|
|
80
|
-
return Reflect.getMetadata(MESSAGE_HANDLER_METADATA_KEY, controller) || [];
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Helper function to create regex and parameter mappings from a pattern string.
|
|
84
|
-
*
|
|
85
|
-
* @param pattern - The pattern string to parse.
|
|
86
|
-
* @returns An object containing the generated regex and parameter names.
|
|
87
|
-
*/ function createRegexFromPattern(pattern) {
|
|
88
|
-
const params = [];
|
|
89
|
-
// Escape special characters except for {} and -
|
|
90
|
-
const escapedPattern = pattern.replace(/[/\\^$*+?.()|[\]]/g, '\\$&') // Removed hyphen `-` from this list
|
|
91
|
-
;
|
|
92
|
-
// Replace placeholders with named capturing groups
|
|
93
|
-
const regexPattern = escapedPattern.replace(/\{(\w+)}/g, (_, param)=>{
|
|
94
|
-
if (!/^\w+$/.test(param)) {
|
|
95
|
-
throw new Error(`Invalid parameter name: ${param}. Parameter names must be alphanumeric.`);
|
|
96
|
-
}
|
|
97
|
-
params.push(param);
|
|
98
|
-
return `(?<${param}>[a-zA-Z0-9]+)`;
|
|
99
|
-
});
|
|
100
|
-
// Construct the final regex
|
|
101
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
102
|
-
return {
|
|
103
|
-
regex,
|
|
104
|
-
params
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Decorator to register command methods in a controller.
|
|
109
|
-
*
|
|
110
|
-
* @param commandName - The name or pattern of the command.
|
|
111
|
-
* @param builderOrType - A command builder class or a command type from `CommandType`.
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```typescript
|
|
115
|
-
* @Command('help', CommandType.SLASH)
|
|
116
|
-
* public async handleHelp(interaction: ChatInputCommandInteraction) {
|
|
117
|
-
* await interaction.reply('This is the help command!')
|
|
118
|
-
* }
|
|
119
|
-
*
|
|
120
|
-
* @Command('stats-{id}', CommandType.BUTTON)
|
|
121
|
-
* public async handleStats(message: ButtonInteraction, { id }) {
|
|
122
|
-
* await message.reply(`Fetching stats for ID: ${id}`);
|
|
123
|
-
* }
|
|
124
|
-
* ```
|
|
125
|
-
*/ function Command(commandName, builderOrType) {
|
|
126
|
-
return function(target, propertyKey, _descriptor) {
|
|
127
|
-
const originalMethod = _descriptor.value;
|
|
128
|
-
if (!originalMethod) {
|
|
129
|
-
throw new Error(`Missing implementation for method ${propertyKey}`);
|
|
130
|
-
}
|
|
131
|
-
// Wrap original method for interaction type validation
|
|
132
|
-
_descriptor.value = function(interaction, params) {
|
|
133
|
-
const expectedInteraction = commandType === enum_index.CommandType.BUTTON && interaction instanceof discord_js.ButtonInteraction || commandType === enum_index.CommandType.SELECT_MENU && interaction instanceof discord_js.StringSelectMenuInteraction || commandType === enum_index.CommandType.SLASH && interaction instanceof discord_js.ChatInputCommandInteraction || commandType === enum_index.CommandType.CONTEXT_MENU && interaction instanceof discord_js.ContextMenuCommandInteraction || commandType === enum_index.CommandType.MODAL_SUBMIT && interaction instanceof discord_js.ModalSubmitInteraction;
|
|
134
|
-
if (!expectedInteraction) {
|
|
135
|
-
throw new Error(`Invalid interaction type passed to @Command for method: ${propertyKey}`);
|
|
136
|
-
}
|
|
137
|
-
return originalMethod.apply(this, [
|
|
138
|
-
interaction,
|
|
139
|
-
params
|
|
140
|
-
]);
|
|
141
|
-
};
|
|
142
|
-
// Retrieve existing metadata or initialize it
|
|
143
|
-
const commands = Reflect.getMetadata(COMMAND_METADATA_KEY, target) || {};
|
|
144
|
-
let builderInstance;
|
|
145
|
-
let commandType;
|
|
146
|
-
let regex;
|
|
147
|
-
let dynamicParams = [];
|
|
148
|
-
// Determine command type and builder
|
|
149
|
-
if (typeof builderOrType === 'function') {
|
|
150
|
-
const builderObj = new builderOrType();
|
|
151
|
-
builderInstance = builderObj.build(commandName);
|
|
152
|
-
commandType = Reflect.getMetadata(metadataKey_enum.MetadataKey.CommandType, builderOrType);
|
|
153
|
-
if (!(commandType in enum_index.CommandType)) {
|
|
154
|
-
throw new Error(`Metadata for 'commandType' is missing on builder ${builderOrType.name}`);
|
|
155
|
-
}
|
|
156
|
-
} else {
|
|
157
|
-
commandType = builderOrType;
|
|
158
|
-
}
|
|
159
|
-
if (commandType !== enum_index.CommandType.SLASH && commandType !== enum_index.CommandType.CONTEXT_MENU) {
|
|
160
|
-
const { regex: generatedRegex, params } = createRegexFromPattern(commandName);
|
|
161
|
-
regex = generatedRegex;
|
|
162
|
-
dynamicParams = params;
|
|
163
|
-
}
|
|
164
|
-
// Ensure commandName supports multiple entries
|
|
165
|
-
if (!commands[commandName]) {
|
|
166
|
-
commands[commandName] = [];
|
|
167
|
-
}
|
|
168
|
-
commands[commandName].push({
|
|
169
|
-
methodName: propertyKey,
|
|
170
|
-
builder: builderInstance,
|
|
171
|
-
type: commandType,
|
|
172
|
-
regex,
|
|
173
|
-
dynamicParams
|
|
174
|
-
});
|
|
175
|
-
Reflect.defineMetadata(COMMAND_METADATA_KEY, commands, target);
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Retrieves the command map for a given controller.
|
|
180
|
-
*
|
|
181
|
-
* @param controller - The controller class instance.
|
|
182
|
-
* @returns A record containing command metadata indexed by command names.
|
|
183
|
-
*/ function getCommandMap(controller) {
|
|
184
|
-
return Reflect.getMetadata(COMMAND_METADATA_KEY, controller);
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Decorator to mark a class as a controller that can later be registered to the App class `(app.ts)` using the `@MeoCord` decorator.
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* @Controller()
|
|
192
|
-
* export class PingSlashController {
|
|
193
|
-
* constructor(private pingService: PingService) {}
|
|
194
|
-
*
|
|
195
|
-
* @Command('ping', PingCommandBuilder)
|
|
196
|
-
* async ping(interaction: ChatInputCommandInteraction) {
|
|
197
|
-
* const response = await this.pingService.handlePing()
|
|
198
|
-
* await interaction.reply(response)
|
|
199
|
-
* }
|
|
200
|
-
* }
|
|
201
|
-
* ```
|
|
202
|
-
*/ function Controller() {
|
|
203
|
-
return function(target) {
|
|
204
|
-
if (!Reflect.hasMetadata(metadataKey_enum.MetadataKey.Injectable, target)) {
|
|
205
|
-
inversify.injectable()(target);
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
exports.Command = Command;
|
|
211
|
-
exports.Controller = Controller;
|
|
212
|
-
exports.MessageHandler = MessageHandler;
|
|
213
|
-
exports.ReactionHandler = ReactionHandler;
|
|
214
|
-
exports.getCommandMap = getCommandMap;
|
|
215
|
-
exports.getMessageHandlers = getMessageHandlers;
|
|
216
|
-
exports.getReactionHandlers = getReactionHandlers;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
declare enum CommandType {
|
|
2
|
-
SLASH = "SLASH",
|
|
3
|
-
BUTTON = "BUTTON",
|
|
4
|
-
CONTEXT_MENU = "CONTEXT_MENU",
|
|
5
|
-
SELECT_MENU = "SELECT_MENU",
|
|
6
|
-
MODAL_SUBMIT = "MODAL_SUBMIT"
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Enum representing actions that can be performed on a message reaction.
|
|
10
|
-
*/
|
|
11
|
-
declare enum ReactionHandlerAction {
|
|
12
|
-
/** Reaction added to a message. */
|
|
13
|
-
ADD = "ADD",
|
|
14
|
-
/** Reaction removed from a message. */
|
|
15
|
-
REMOVE = "REMOVE"
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { CommandType as C, ReactionHandlerAction as R };
|