@kevlid/discordmenus 0.1.4 → 0.1.5

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/index.d.ts CHANGED
@@ -3,10 +3,15 @@ export type GetFunction = (
3
3
  options?: Record<string, unknown>,
4
4
  ) => unknown | Promise<unknown>;
5
5
 
6
+ /** Passed to save(): decoded option key, value, and context (includes category = decoded category key). */
6
7
  export type SaveFunction = (
7
8
  key: string,
8
9
  value: unknown,
9
- options?: Record<string, unknown>,
10
+ options?: Record<string, unknown> & {
11
+ guildId?: string | null;
12
+ userId?: string | null;
13
+ category?: string | null;
14
+ },
10
15
  ) => unknown | Promise<unknown>;
11
16
 
12
17
  export type PermissionResolvable = string | number | bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevlid/discordmenus",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Components v2 settings menus for Discord bots",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
@@ -77,7 +77,11 @@ async function handleListBranch({
77
77
  arrayClone(await menu.get(optionKey, { guildId, userId })),
78
78
  );
79
79
  addItems.push(shapeObjectListItem(props, {}));
80
- await menu.save(optionKey, addItems, { guildId, userId });
80
+ await menu.save(optionKey, addItems, {
81
+ guildId,
82
+ userId,
83
+ category: menu.getCategoryKey(category),
84
+ });
81
85
  return updateMessage(
82
86
  await menu.renderListObjectItemPage(optionKey, addItems.length - 1, {
83
87
  ...menuCtx,
@@ -116,11 +120,11 @@ async function handleListBranch({
116
120
  const currentItem = doneItems[itemIndex];
117
121
  if (objectListItemFailsRequired(option, currentItem)) {
118
122
  doneItems.splice(itemIndex, 1);
119
- await menu.save(
120
- optionKey,
121
- normalizeObjectListItems(option.itemConfig.properties, doneItems),
122
- { guildId, userId },
123
- );
123
+ await menu.save(optionKey, normalizeObjectListItems(option.itemConfig.properties, doneItems), {
124
+ guildId,
125
+ userId,
126
+ category: menu.getCategoryKey(category),
127
+ });
124
128
  }
125
129
  return updateMessage(await renderMenu(menu_key, menuCtx));
126
130
  }
@@ -151,7 +155,11 @@ async function handleListBranch({
151
155
  ...asObject(boolItems[itemIndex]),
152
156
  [propertyKey]: newBoolValue,
153
157
  });
154
- await menu.save(optionKey, boolItems, { guildId, userId });
158
+ await menu.save(optionKey, boolItems, {
159
+ guildId,
160
+ userId,
161
+ category: menu.getCategoryKey(category),
162
+ });
155
163
  return updateMessage(
156
164
  await menu.renderListObjectItemPage(optionKey, itemIndex, { ...menuCtx, listPage: sub }),
157
165
  );
@@ -165,7 +173,11 @@ async function handleListBranch({
165
173
  deleteItems = normalizeObjectListItems(props, deleteItems);
166
174
  }
167
175
  deleteItems.splice(itemIndex, 1);
168
- await menu.save(optionKey, deleteItems, { guildId, userId });
176
+ await menu.save(optionKey, deleteItems, {
177
+ guildId,
178
+ userId,
179
+ category: menu.getCategoryKey(category),
180
+ });
169
181
  if (objectItems) {
170
182
  if (deleteItems.length <= 0) {
171
183
  return updateMessage(await renderMenu(menu_key, menuCtx));
@@ -222,7 +234,11 @@ async function handleObjectBranch({
222
234
  const defaults = boolObjectOption ? objectDefaultsFromKeys(boolObjectOption.options) : {};
223
235
  const updatedObj = Object.assign(defaults, baseObj);
224
236
  updatedObj[subKey] = newValue;
225
- await menu.save(objectKey, updatedObj, { guildId, userId });
237
+ await menu.save(objectKey, updatedObj, {
238
+ guildId,
239
+ userId,
240
+ category: menu.getCategoryKey(category),
241
+ });
226
242
  return updateMessage(await menu.renderObjectPage(objectKey, objectCtx));
227
243
  }
228
244
  if (subAction === Presets.subModal) {
@@ -276,7 +292,11 @@ async function handleObjectBranch({
276
292
  const obj = asObject(await menu.get(objectKey, { guildId, userId }));
277
293
  const items = arrayClone(obj[subKey]);
278
294
  items.splice(itemIndex, 1);
279
- await menu.save(objectKey, Object.assign({}, obj, { [subKey]: items }), { guildId, userId });
295
+ await menu.save(objectKey, Object.assign({}, obj, { [subKey]: items }), {
296
+ guildId,
297
+ userId,
298
+ category: menu.getCategoryKey(category),
299
+ });
280
300
  return updateMessage(
281
301
  await menu.renderObjectListPage(objectKey, subKey, sub, { ...menuCtx, objectPage }),
282
302
  );
@@ -325,7 +345,11 @@ async function handleComponent(interaction, menu, menu_key, renderMenu) {
325
345
  } else if (action === OptionTypes.String || action === OptionTypes.Number) {
326
346
  result = openModal(await menu.renderModal(values[0], { category, index: page, guildId }));
327
347
  } else if (action === OptionTypes.Boolean) {
328
- await menu.save(values[0], values[2] === "true", { guildId, userId });
348
+ await menu.save(values[0], values[2] === "true", {
349
+ guildId,
350
+ userId,
351
+ category: menu.getCategoryKey(category),
352
+ });
329
353
  result = updateMessage(
330
354
  await renderMenu(menu_key, { category, index: page, userId, guildId }),
331
355
  );
@@ -387,7 +411,11 @@ async function handleComponent(interaction, menu, menu_key, renderMenu) {
387
411
  saveValue = only;
388
412
  }
389
413
  }
390
- await menu.save(optionKey, saveValue, { guildId, userId });
414
+ await menu.save(optionKey, saveValue, {
415
+ guildId,
416
+ userId,
417
+ category: menu.getCategoryKey(category),
418
+ });
391
419
  result = updateMessage(
392
420
  await renderMenu(menu_key, { category, index: page, userId, guildId }),
393
421
  );
@@ -65,7 +65,11 @@ async function handleListModal(menu, values, options, rawValue, category, page,
65
65
  ...asObject(objItems[objItemIndex]),
66
66
  [property.key]: parsedPropertyValue,
67
67
  });
68
- await menu.save(optionKey, objItems, { guildId, userId });
68
+ await menu.save(optionKey, objItems, {
69
+ guildId,
70
+ userId,
71
+ category: menu.getCategoryKey(category),
72
+ });
69
73
  return updateMessage(
70
74
  await menu.renderListObjectItemPage(optionKey, objItemIndex, { ...menuCtx, listPage: sub }),
71
75
  );
@@ -95,7 +99,11 @@ async function handleListModal(menu, values, options, rawValue, category, page,
95
99
  } else {
96
100
  throw new Error(`Unknown list sub-action "${subAction}"`);
97
101
  }
98
- await menu.save(optionKey, items, { guildId, userId });
102
+ await menu.save(optionKey, items, {
103
+ guildId,
104
+ userId,
105
+ category: menu.getCategoryKey(category),
106
+ });
99
107
  return updateMessage(await menu.renderListPage(optionKey, sub, menuCtx));
100
108
  }
101
109
 
@@ -146,7 +154,11 @@ async function handleObjectModal(menu, values, options, rawValue, category, page
146
154
  const defaults = objectDefaultsFromKeys(option.options);
147
155
  const updatedObj = Object.assign(defaults, baseObj);
148
156
  updatedObj[subKey] = parsedValue;
149
- await menu.save(optionKey, updatedObj, { guildId, userId });
157
+ await menu.save(optionKey, updatedObj, {
158
+ guildId,
159
+ userId,
160
+ category: menu.getCategoryKey(category),
161
+ });
150
162
  return updateMessage(await menu.renderObjectPage(optionKey, menuCtx));
151
163
  }
152
164
 
@@ -177,7 +189,11 @@ async function handleObjectModal(menu, values, options, rawValue, category, page
177
189
  } else {
178
190
  throw new Error(`Unknown list action "${listAction}"`);
179
191
  }
180
- await menu.save(optionKey, Object.assign({}, baseObj, { [subKey]: items }), { guildId, userId });
192
+ await menu.save(optionKey, Object.assign({}, baseObj, { [subKey]: items }), {
193
+ guildId,
194
+ userId,
195
+ category: menu.getCategoryKey(category),
196
+ });
181
197
  return updateMessage(
182
198
  await menu.renderObjectListPage(optionKey, subKey, sub, menuCtx),
183
199
  );
@@ -214,7 +230,11 @@ async function handleModal(interaction, menu, menu_key, renderMenu) {
214
230
  return handleObjectModal(menu, values, options, rawValue, category, page, guildId, userId);
215
231
  }
216
232
  if (optionType === OptionTypes.String) {
217
- await menu.save(values[0], rawValue, { guildId, userId });
233
+ await menu.save(values[0], rawValue, {
234
+ guildId,
235
+ userId,
236
+ category: menu.getCategoryKey(category),
237
+ });
218
238
  return updateMessage(await renderMenu(menu_key, menuCtx));
219
239
  }
220
240
  if (optionType === OptionTypes.Number) {
@@ -222,7 +242,11 @@ async function handleModal(interaction, menu, menu_key, renderMenu) {
222
242
  if (Number.isNaN(value)) {
223
243
  throw new Error("Invalid number");
224
244
  }
225
- await menu.save(values[0], value, { guildId, userId });
245
+ await menu.save(values[0], value, {
246
+ guildId,
247
+ userId,
248
+ category: menu.getCategoryKey(category),
249
+ });
226
250
  return updateMessage(await renderMenu(menu_key, menuCtx));
227
251
  }
228
252
  throw new Error(`Option type "${optionType}" does not support modal input`);
@@ -58,6 +58,7 @@ class MenuInstance {
58
58
  build() {
59
59
  const built = this.builder.build();
60
60
  this.pages = [];
61
+ this.categoryKeys = [];
61
62
 
62
63
  for (const category of built.categories) {
63
64
  const categoryPages = [];
@@ -84,6 +85,7 @@ class MenuInstance {
84
85
  }
85
86
  if (categoryPages.length > 0) {
86
87
  this.pages.push(categoryPages);
88
+ this.categoryKeys.push(decodeKey(category.key));
87
89
  }
88
90
  }
89
91
 
@@ -108,6 +110,14 @@ class MenuInstance {
108
110
  return page.options.find(o => o.key === optionKey) ?? null;
109
111
  }
110
112
 
113
+ getCategoryKey(categoryIndex) {
114
+ this.ensureBuilt();
115
+ if (typeof categoryIndex !== "number" || categoryIndex < 0 || categoryIndex >= this.categoryKeys.length) {
116
+ return null;
117
+ }
118
+ return this.categoryKeys[categoryIndex] ?? null;
119
+ }
120
+
111
121
  findOption(optionKey) {
112
122
  for (let cat = 0; cat < this.pages.length; cat++) {
113
123
  const categoryPages = this.pages[cat];
@@ -214,7 +224,11 @@ class MenuInstance {
214
224
  const props = option.itemConfig.properties;
215
225
  if (objectListItemsNeedReshape(props, items)) {
216
226
  items = normalizeObjectListItems(props, arrayClone(items));
217
- await this.save(option.key, items, { guildId, userId });
227
+ await this.save(option.key, items, {
228
+ guildId,
229
+ userId,
230
+ category: this.getCategoryKey(category),
231
+ });
218
232
  }
219
233
  }
220
234
  return renderListPage(this.key, option, items, listPage, userId, category, index);
@@ -292,7 +306,11 @@ class MenuInstance {
292
306
  const props = option.itemConfig.properties;
293
307
  if (objectListItemsNeedReshape(props, items)) {
294
308
  items = normalizeObjectListItems(props, arrayClone(items));
295
- await this.save(option.key, items, { guildId, userId });
309
+ await this.save(option.key, items, {
310
+ guildId,
311
+ userId,
312
+ category: this.getCategoryKey(category),
313
+ });
296
314
  }
297
315
  }
298
316
  const safeIndex = items.length <= 0 ? 0 : Math.max(0, Math.min(itemIndex, items.length - 1));