-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
199 lines (179 loc) · 7.13 KB
/
index.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-disable no-unused-vars */
import { eventSource, event_types } from '../../../../script.js';
import { SlashCommand } from '../../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../../slash-commands/SlashCommandArgument.js';
import { SlashCommandClosure } from '../../../slash-commands/SlashCommandClosure.js';
import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js';
import { SlashCommandScope } from '../../../slash-commands/SlashCommandScope.js';
const listeners = new Map();
/**
* Finds an event ID by name.
* @param {string} event Event name
* @returns {string} Event ID
*/
function findEventId(event) {
return Object.entries(event_types).find(([key, value]) => value.toLowerCase() === event || key.toLowerCase() === event)?.[1];
}
/**
* Assigns nested variables to a scope.
* @param {SlashCommandScope} scope Scope to assign variables to.
* @param {object} arg Object to assign variables from.
* @param {string} prefix Prefix for the variable names.
*/
function assignNestedVariables(scope, arg, prefix) {
Object.entries(arg).forEach(([key, value]) => {
const newPrefix = `${prefix}.${key}`;
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
value.forEach((item, i) => {
assignNestedVariables(scope, { [i]: item }, newPrefix);
});
} else {
assignNestedVariables(scope, value, newPrefix);
}
} else {
scope.letVariable(newPrefix, String(value));
}
});
}
/**
* Makes a listener for a closure.
* @param {SlashCommandClosure} closure Closure to make a listener for.
* @returns {{listener: function, id: string}} Object with listener and ID.
*/
function makeListener(closure) {
const id = Math.random().toString(36).substring(2);
const originalScope = closure.scope.getCopy();
const listener = (...args) => {
const scope = originalScope.getCopy();
args.forEach((arg, index) => {
if (arg === null || arg === undefined) return;
scope.letVariable(`arg${index}`, String(arg));
if (typeof arg === 'object' && arg !== null) {
if (Array.isArray(arg)) {
arg.forEach((item, i) => {
assignNestedVariables(scope, { [i]: item }, `arg${index}`);
});
} else {
assignNestedVariables(scope, arg, `arg${index}`);
}
}
});
closure.scope = scope;
return closure.execute();
};
listeners.set(id, listener);
return { listener, id };
}
function validateAndPrepareEvent(args, closure) {
if (Array.isArray(closure)) {
closure = closure.find((c) => c instanceof SlashCommandClosure);
}
if (!(closure instanceof SlashCommandClosure)) {
toastr.error('Callback is not a closure.');
return null;
}
const event = String(args.event).trim().toLowerCase();
if (!event) {
toastr.warning('Event name is required.');
return null;
}
const eventId = findEventId(event);
if (!eventId) {
toastr.warning(`Event ${event} not found.`);
return null;
}
const { listener, id } = makeListener(closure);
return { eventId, listener, id };
}
function eventOn(args, closure) {
const eventDetails = validateAndPrepareEvent(args, closure);
if (!eventDetails) return '';
eventSource.on(eventDetails.eventId, eventDetails.listener);
return eventDetails.id;
}
function eventOnce(args, closure) {
const eventDetails = validateAndPrepareEvent(args, closure);
if (!eventDetails) return '';
eventSource.once(eventDetails.eventId, eventDetails.listener);
return eventDetails.id;
}
function eventOff(_, value) {
const id = String(value).trim();
const listener = listeners.get(id);
if (!listener) {
toastr.warning(`Listener with ID ${id} not found.`);
return;
}
eventSource.removeListener(listener);
listeners.delete(id);
}
(function init() {
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'event-on',
helpString: `
<div>Sets up an event listener for a known event. Returns an event listener ID to use with <code>/event-off</code>.</div>
<div>Use <code>/var arg#</code> in the closure code to access the event arguments, where # is an index of the argument (usually 0).<br>For objects, use <code>/var arg#.key</code>.</div>
<div>Example:</div><ul><li>Output a chat name: <code>/event-on event=CHAT_CHANGED {: /var arg0 | /echo :}</code></li></ul></div>`,
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Event callback closure',
isRequired: true,
acceptsMultiple: false,
typeList: ARGUMENT_TYPE.CLOSURE,
}),
],
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'event',
description: 'Event name',
typeList: ARGUMENT_TYPE.STRING,
isRequired: true,
acceptsMultiple: false,
enumList: Object.keys(event_types),
}),
],
callback: eventOn,
returns: 'listener ID',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'event-once',
helpString: `
<div>Sets up an event listener for a known event that will be removed after the first call. Returns an event listener ID to use with <code>/event-off</code>.</div>
<div>Use <code>/var arg#</code> in the closure code to access the event arguments, where # is an index of the argument (usually 0).<br>For objects, use <code>/var arg#.key</code>.</div>
<div>Example:</div><ul><li>Output a chat name: <code>/event-once event=CHAT_CHANGED {: /var arg0 | /echo :}</code></li></ul></div>`,
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Event callback closure',
isRequired: true,
acceptsMultiple: false,
typeList: ARGUMENT_TYPE.CLOSURE,
}),
],
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'event',
description: 'Event name',
typeList: ARGUMENT_TYPE.STRING,
isRequired: true,
acceptsMultiple: false,
enumList: Object.keys(event_types),
}),
],
callback: eventOnce,
returns: 'listener ID',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'event-off',
helpString: '<div>Removes an event listener by ID.</div>',
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'listener ID',
typeList: ARGUMENT_TYPE.STRING,
isRequired: true,
acceptsMultiple: false,
}),
],
callback: eventOff,
}));
})();