@spatulox/simplediscordbot 2.1.0 → 2.1.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.
package/dist/index.d.mts CHANGED
@@ -208,6 +208,71 @@ declare class FileManager {
208
208
  static deleteFile(filePath: string): Promise<boolean>;
209
209
  }
210
210
 
211
+ declare class CacheManager {
212
+ private static get cacheDir();
213
+ private static cleanCacheId;
214
+ private static createFilePath;
215
+ /**
216
+ * New file with cache_id as name
217
+ * @param cache_id - id which become the cache file name
218
+ * @param initialData - optional initial data
219
+ * @returns true if success, false otherwise
220
+ */
221
+ static createCache(cache_id: string, initialData?: any): Promise<boolean>;
222
+ /**
223
+ * Read cache data or create empty one
224
+ * @param cache_id
225
+ * @param default_data default data with default key/value This will be send back if the cache doesn't exist and need to be created
226
+ * @returns true if success, false otherwise
227
+ */
228
+ static getOrCreateCache<T = any>(cache_id: string, default_data: T): Promise<T | false>;
229
+ /**
230
+ * Read cache data
231
+ * @param cache_id - cache_id to read
232
+ * @returns Json data, false otherwise
233
+ */
234
+ static readCache<T = any>(cache_id: string): Promise<T | false>;
235
+ /**
236
+ * Overwrite cache data with the new data
237
+ * @param cache_id - ID of the cache
238
+ * @param data - new data
239
+ * @returns true if success, false otherwise
240
+ */
241
+ static writeCache(cache_id: string, data: any): Promise<boolean>;
242
+ /**
243
+ * Update specific property of the cache
244
+ * @param cache_id - ID of the cache
245
+ * @param property - property to update, with key and value
246
+ * @returns true if success, false otherwise
247
+ */
248
+ static updateCacheProperty(cache_id: string, property: {
249
+ key: string;
250
+ value: any;
251
+ }): Promise<boolean>;
252
+ /**
253
+ * Reset an entire cache
254
+ * @param cache_id - cache ID to reset
255
+ * @returns true if success, false otherwise
256
+ */
257
+ static resetCache(cache_id: string): Promise<boolean>;
258
+ /**
259
+ * Delete an entire cache
260
+ * @param cache_id - cache ID to delete
261
+ * @returns true if success, false otherwise
262
+ */
263
+ static deleteCache(cache_id: string): Promise<boolean>;
264
+ /**
265
+ * List every existing cache
266
+ * @returns Array of cache_id, false otherwise
267
+ */
268
+ static listCaches(): Promise<string[] | false>;
269
+ /**
270
+ * Clean cache data
271
+ * @returns true if success, false otherwise
272
+ */
273
+ static clearAllCaches(): Promise<boolean>;
274
+ }
275
+
211
276
  declare class EmbedManager {
212
277
  private static get BOT_ICON();
213
278
  private static get DEFAULT_COLOR();
@@ -1095,4 +1160,4 @@ declare const SimpleDiscordBotInfo: {
1095
1160
  license: string;
1096
1161
  };
1097
1162
 
1098
- export { Bot, type BotConfig, BotEnv, ButtonManager, type ButtonOptions, ComponentManager, type ComponentManagerCreate, type ComponentManagerField, type ComponentManagerFileInput, DiscordRegex, EmbedManager, FileManager, GuildManager, Log, type ModalField, ModalFieldType, ModalManager, type RandomBotActivity, ReactionManager, type SelectMenuCreateOption, type SelectMenuList, SelectMenuManager, SimpleColor, SimpleDiscordBotInfo, SimpleMutex, Time, UserManager, WebhookManager };
1163
+ export { Bot, type BotConfig, BotEnv, ButtonManager, type ButtonOptions, CacheManager, ComponentManager, type ComponentManagerCreate, type ComponentManagerField, type ComponentManagerFileInput, DiscordRegex, EmbedManager, FileManager, GuildManager, Log, type ModalField, ModalFieldType, ModalManager, type RandomBotActivity, ReactionManager, type SelectMenuCreateOption, type SelectMenuList, SelectMenuManager, SimpleColor, SimpleDiscordBotInfo, SimpleMutex, Time, UserManager, WebhookManager };
package/dist/index.d.ts CHANGED
@@ -208,6 +208,71 @@ declare class FileManager {
208
208
  static deleteFile(filePath: string): Promise<boolean>;
209
209
  }
210
210
 
211
+ declare class CacheManager {
212
+ private static get cacheDir();
213
+ private static cleanCacheId;
214
+ private static createFilePath;
215
+ /**
216
+ * New file with cache_id as name
217
+ * @param cache_id - id which become the cache file name
218
+ * @param initialData - optional initial data
219
+ * @returns true if success, false otherwise
220
+ */
221
+ static createCache(cache_id: string, initialData?: any): Promise<boolean>;
222
+ /**
223
+ * Read cache data or create empty one
224
+ * @param cache_id
225
+ * @param default_data default data with default key/value This will be send back if the cache doesn't exist and need to be created
226
+ * @returns true if success, false otherwise
227
+ */
228
+ static getOrCreateCache<T = any>(cache_id: string, default_data: T): Promise<T | false>;
229
+ /**
230
+ * Read cache data
231
+ * @param cache_id - cache_id to read
232
+ * @returns Json data, false otherwise
233
+ */
234
+ static readCache<T = any>(cache_id: string): Promise<T | false>;
235
+ /**
236
+ * Overwrite cache data with the new data
237
+ * @param cache_id - ID of the cache
238
+ * @param data - new data
239
+ * @returns true if success, false otherwise
240
+ */
241
+ static writeCache(cache_id: string, data: any): Promise<boolean>;
242
+ /**
243
+ * Update specific property of the cache
244
+ * @param cache_id - ID of the cache
245
+ * @param property - property to update, with key and value
246
+ * @returns true if success, false otherwise
247
+ */
248
+ static updateCacheProperty(cache_id: string, property: {
249
+ key: string;
250
+ value: any;
251
+ }): Promise<boolean>;
252
+ /**
253
+ * Reset an entire cache
254
+ * @param cache_id - cache ID to reset
255
+ * @returns true if success, false otherwise
256
+ */
257
+ static resetCache(cache_id: string): Promise<boolean>;
258
+ /**
259
+ * Delete an entire cache
260
+ * @param cache_id - cache ID to delete
261
+ * @returns true if success, false otherwise
262
+ */
263
+ static deleteCache(cache_id: string): Promise<boolean>;
264
+ /**
265
+ * List every existing cache
266
+ * @returns Array of cache_id, false otherwise
267
+ */
268
+ static listCaches(): Promise<string[] | false>;
269
+ /**
270
+ * Clean cache data
271
+ * @returns true if success, false otherwise
272
+ */
273
+ static clearAllCaches(): Promise<boolean>;
274
+ }
275
+
211
276
  declare class EmbedManager {
212
277
  private static get BOT_ICON();
213
278
  private static get DEFAULT_COLOR();
@@ -1095,4 +1160,4 @@ declare const SimpleDiscordBotInfo: {
1095
1160
  license: string;
1096
1161
  };
1097
1162
 
1098
- export { Bot, type BotConfig, BotEnv, ButtonManager, type ButtonOptions, ComponentManager, type ComponentManagerCreate, type ComponentManagerField, type ComponentManagerFileInput, DiscordRegex, EmbedManager, FileManager, GuildManager, Log, type ModalField, ModalFieldType, ModalManager, type RandomBotActivity, ReactionManager, type SelectMenuCreateOption, type SelectMenuList, SelectMenuManager, SimpleColor, SimpleDiscordBotInfo, SimpleMutex, Time, UserManager, WebhookManager };
1163
+ export { Bot, type BotConfig, BotEnv, ButtonManager, type ButtonOptions, CacheManager, ComponentManager, type ComponentManagerCreate, type ComponentManagerField, type ComponentManagerFileInput, DiscordRegex, EmbedManager, FileManager, GuildManager, Log, type ModalField, ModalFieldType, ModalManager, type RandomBotActivity, ReactionManager, type SelectMenuCreateOption, type SelectMenuList, SelectMenuManager, SimpleColor, SimpleDiscordBotInfo, SimpleMutex, Time, UserManager, WebhookManager };
package/dist/index.js CHANGED
@@ -863,11 +863,11 @@ var require_baseGet = __commonJS({
863
863
  "use strict";
864
864
  var castPath = require_castPath();
865
865
  var toKey = require_toKey();
866
- function baseGet(object, path2) {
867
- path2 = castPath(path2, object);
868
- var index = 0, length = path2.length;
866
+ function baseGet(object, path3) {
867
+ path3 = castPath(path3, object);
868
+ var index = 0, length = path3.length;
869
869
  while (object != null && index < length) {
870
- object = object[toKey(path2[index++])];
870
+ object = object[toKey(path3[index++])];
871
871
  }
872
872
  return index && index == length ? object : void 0;
873
873
  }
@@ -880,8 +880,8 @@ var require_get = __commonJS({
880
880
  "node_modules/lodash/get.js"(exports2, module2) {
881
881
  "use strict";
882
882
  var baseGet = require_baseGet();
883
- function get2(object, path2, defaultValue) {
884
- var result = object == null ? void 0 : baseGet(object, path2);
883
+ function get2(object, path3, defaultValue) {
884
+ var result = object == null ? void 0 : baseGet(object, path3);
885
885
  return result === void 0 ? defaultValue : result;
886
886
  }
887
887
  module2.exports = get2;
@@ -4984,6 +4984,7 @@ __export(index_exports, {
4984
4984
  Bot: () => Bot,
4985
4985
  BotEnv: () => BotEnv,
4986
4986
  ButtonManager: () => ButtonManager,
4987
+ CacheManager: () => CacheManager,
4987
4988
  ComponentManager: () => ComponentManager,
4988
4989
  DiscordRegex: () => DiscordRegex,
4989
4990
  EmbedManager: () => EmbedManager,
@@ -14329,7 +14330,7 @@ var BotInteraction = class {
14329
14330
  // package.json
14330
14331
  var package_default = {
14331
14332
  name: "@spatulox/simplediscordbot",
14332
- version: "2.0.3",
14333
+ version: "2.1.0",
14333
14334
  author: "Spatulox",
14334
14335
  description: "Simple discord bot framework to set up a bot under 30 secondes",
14335
14336
  exports: {
@@ -14617,10 +14618,186 @@ var FileManager = class {
14617
14618
  }
14618
14619
  };
14619
14620
 
14621
+ // src/manager/CacheManager.ts
14622
+ var import_path2 = __toESM(require("path"));
14623
+ var CacheManager = class {
14624
+ static get cacheDir() {
14625
+ return import_path2.default.join(process.cwd(), `.${Bot.config?.botName ?? "simplediscordbot"}cache`);
14626
+ }
14627
+ static cleanCacheId(key) {
14628
+ return key.replace(/[^a-zA-Z0-9_-]/g, "_");
14629
+ }
14630
+ static createFilePath(filename) {
14631
+ return import_path2.default.join(this.cacheDir, `${filename}.json`);
14632
+ }
14633
+ /**
14634
+ * New file with cache_id as name
14635
+ * @param cache_id - id which become the cache file name
14636
+ * @param initialData - optional initial data
14637
+ * @returns true if success, false otherwise
14638
+ */
14639
+ static async createCache(cache_id, initialData = {}) {
14640
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14641
+ Bot.log.error('"Key" to create a cache must be a non-empty string');
14642
+ return false;
14643
+ }
14644
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14645
+ Bot.log.info(`Creating cache file: ${cleanCacheId}.json`);
14646
+ return await FileManager.writeJsonFile(
14647
+ this.cacheDir,
14648
+ this.cleanCacheId(cache_id),
14649
+ initialData,
14650
+ true
14651
+ );
14652
+ }
14653
+ /**
14654
+ * Read cache data or create empty one
14655
+ * @param cache_id
14656
+ * @param default_data default data with default key/value This will be send back if the cache doesn't exist and need to be created
14657
+ * @returns true if success, false otherwise
14658
+ */
14659
+ static async getOrCreateCache(cache_id, default_data) {
14660
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14661
+ Log.error('"cache_id" to get or create a cache must be a non-empty string');
14662
+ return false;
14663
+ }
14664
+ const cleanCacheId = this.cleanCacheId(cache_id);
14665
+ const filePath = this.createFilePath(cleanCacheId);
14666
+ if (await FileManager.fileExists(filePath)) {
14667
+ Log.info(`Cache already exists: ${cleanCacheId}.json`);
14668
+ return FileManager.readJsonFile(filePath);
14669
+ }
14670
+ Log.info(`Creating new cache file: ${this.cacheDir}/${cleanCacheId}.json`);
14671
+ const res = await FileManager.writeJsonFile(
14672
+ this.cacheDir,
14673
+ cleanCacheId,
14674
+ default_data,
14675
+ false
14676
+ );
14677
+ return res ? default_data : false;
14678
+ }
14679
+ /**
14680
+ * Read cache data
14681
+ * @param cache_id - cache_id to read
14682
+ * @returns Json data, false otherwise
14683
+ */
14684
+ static async readCache(cache_id) {
14685
+ if (!cache_id || typeof cache_id !== "string") {
14686
+ Bot.log.error('"Key" must be a string');
14687
+ return false;
14688
+ }
14689
+ const cleanCacheId = this.cleanCacheId(cache_id);
14690
+ const filePath = this.createFilePath(cleanCacheId);
14691
+ if (!await FileManager.fileExists(filePath)) {
14692
+ Log.debug(`Cache not found: ${cleanCacheId}.json`);
14693
+ return false;
14694
+ }
14695
+ return await FileManager.readJsonFile(filePath);
14696
+ }
14697
+ /**
14698
+ * Overwrite cache data with the new data
14699
+ * @param cache_id - ID of the cache
14700
+ * @param data - new data
14701
+ * @returns true if success, false otherwise
14702
+ */
14703
+ static async writeCache(cache_id, data) {
14704
+ if (!cache_id || typeof cache_id !== "string") {
14705
+ Log.error("Cache ID must be a string");
14706
+ return false;
14707
+ }
14708
+ const cleanCacheId = this.cleanCacheId(cache_id);
14709
+ Bot.log.info(`Writing to cache: ${cleanCacheId}.json`);
14710
+ return await FileManager.writeJsonFile(
14711
+ this.cacheDir,
14712
+ cleanCacheId,
14713
+ data,
14714
+ true
14715
+ );
14716
+ }
14717
+ /**
14718
+ * Update specific property of the cache
14719
+ * @param cache_id - ID of the cache
14720
+ * @param property - property to update, with key and value
14721
+ * @returns true if success, false otherwise
14722
+ */
14723
+ static async updateCacheProperty(cache_id, property) {
14724
+ const cacheData = await this.readCache(cache_id);
14725
+ if (cacheData === false) {
14726
+ Log.error(`Cache ${cache_id} not found`);
14727
+ return false;
14728
+ }
14729
+ cacheData[property.key] = property.value;
14730
+ return await this.writeCache(cache_id, cacheData);
14731
+ }
14732
+ /**
14733
+ * Reset an entire cache
14734
+ * @param cache_id - cache ID to reset
14735
+ * @returns true if success, false otherwise
14736
+ */
14737
+ static async resetCache(cache_id) {
14738
+ try {
14739
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14740
+ await this.writeCache(cache_id, {});
14741
+ Log.info(`Reset cache: ${cleanCacheId}.json`);
14742
+ return true;
14743
+ } catch (error) {
14744
+ Log.error(`Failed to reset cache ${cache_id}: ${error}`);
14745
+ return false;
14746
+ }
14747
+ }
14748
+ /**
14749
+ * Delete an entire cache
14750
+ * @param cache_id - cache ID to delete
14751
+ * @returns true if success, false otherwise
14752
+ */
14753
+ static async deleteCache(cache_id) {
14754
+ try {
14755
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14756
+ const filePath = this.createFilePath(cleanCacheId);
14757
+ if (await FileManager.deleteFile(filePath)) {
14758
+ Log.info(`Deleted cache: ${cleanCacheId}.json`);
14759
+ return true;
14760
+ }
14761
+ return false;
14762
+ } catch (error) {
14763
+ Log.error(`Failed to delete cache ${cache_id}: ${error}`);
14764
+ return false;
14765
+ }
14766
+ }
14767
+ /**
14768
+ * List every existing cache
14769
+ * @returns Array of cache_id, false otherwise
14770
+ */
14771
+ static async listCaches() {
14772
+ return await FileManager.listJsonFiles(this.cacheDir);
14773
+ }
14774
+ /**
14775
+ * Clean cache data
14776
+ * @returns true if success, false otherwise
14777
+ */
14778
+ static async clearAllCaches() {
14779
+ try {
14780
+ const caches = await this.listCaches();
14781
+ if (caches === false || caches.length === 0) {
14782
+ return true;
14783
+ }
14784
+ let success = true;
14785
+ for (const cache of caches) {
14786
+ const result = await this.deleteCache(cache.replace(".json", ""));
14787
+ if (!result) success = false;
14788
+ }
14789
+ return success;
14790
+ } catch (error) {
14791
+ Log.error(`Failed to clear caches: ${error}`);
14792
+ return false;
14793
+ }
14794
+ }
14795
+ };
14796
+
14620
14797
  // src/manager/messages/WebhookManager.ts
14621
14798
  var import_discord7 = require("discord.js");
14622
14799
  var import_promises2 = require("fs/promises");
14623
- var import_path2 = require("path");
14800
+ var import_path3 = require("path");
14624
14801
  var WebhookManager = class {
14625
14802
  constructor(client, name, avatarPathOrUrl) {
14626
14803
  this.client = client;
@@ -14634,7 +14811,7 @@ var WebhookManager = class {
14634
14811
  return this.avatarPathOrUrl;
14635
14812
  }
14636
14813
  try {
14637
- const resolvedPath = (0, import_path2.resolve)(process.cwd(), this.avatarPathOrUrl);
14814
+ const resolvedPath = (0, import_path3.resolve)(process.cwd(), this.avatarPathOrUrl);
14638
14815
  return await (0, import_promises2.readFile)(resolvedPath);
14639
14816
  } catch (error) {
14640
14817
  Bot.log.warn(`Failed to load avatar from ${this.avatarPathOrUrl}: ${error}`);
@@ -16238,6 +16415,7 @@ var SimpleMutex = class {
16238
16415
  Bot,
16239
16416
  BotEnv,
16240
16417
  ButtonManager,
16418
+ CacheManager,
16241
16419
  ComponentManager,
16242
16420
  DiscordRegex,
16243
16421
  EmbedManager,
package/dist/index.mjs CHANGED
@@ -857,11 +857,11 @@ var require_baseGet = __commonJS({
857
857
  "use strict";
858
858
  var castPath = require_castPath();
859
859
  var toKey = require_toKey();
860
- function baseGet(object, path2) {
861
- path2 = castPath(path2, object);
862
- var index = 0, length = path2.length;
860
+ function baseGet(object, path3) {
861
+ path3 = castPath(path3, object);
862
+ var index = 0, length = path3.length;
863
863
  while (object != null && index < length) {
864
- object = object[toKey(path2[index++])];
864
+ object = object[toKey(path3[index++])];
865
865
  }
866
866
  return index && index == length ? object : void 0;
867
867
  }
@@ -874,8 +874,8 @@ var require_get = __commonJS({
874
874
  "node_modules/lodash/get.js"(exports, module) {
875
875
  "use strict";
876
876
  var baseGet = require_baseGet();
877
- function get2(object, path2, defaultValue) {
878
- var result = object == null ? void 0 : baseGet(object, path2);
877
+ function get2(object, path3, defaultValue) {
878
+ var result = object == null ? void 0 : baseGet(object, path3);
879
879
  return result === void 0 ? defaultValue : result;
880
880
  }
881
881
  module.exports = get2;
@@ -14323,7 +14323,7 @@ var BotInteraction = class {
14323
14323
  // package.json
14324
14324
  var package_default = {
14325
14325
  name: "@spatulox/simplediscordbot",
14326
- version: "2.0.3",
14326
+ version: "2.1.0",
14327
14327
  author: "Spatulox",
14328
14328
  description: "Simple discord bot framework to set up a bot under 30 secondes",
14329
14329
  exports: {
@@ -14611,6 +14611,182 @@ var FileManager = class {
14611
14611
  }
14612
14612
  };
14613
14613
 
14614
+ // src/manager/CacheManager.ts
14615
+ import path2 from "path";
14616
+ var CacheManager = class {
14617
+ static get cacheDir() {
14618
+ return path2.join(process.cwd(), `.${Bot.config?.botName ?? "simplediscordbot"}cache`);
14619
+ }
14620
+ static cleanCacheId(key) {
14621
+ return key.replace(/[^a-zA-Z0-9_-]/g, "_");
14622
+ }
14623
+ static createFilePath(filename) {
14624
+ return path2.join(this.cacheDir, `${filename}.json`);
14625
+ }
14626
+ /**
14627
+ * New file with cache_id as name
14628
+ * @param cache_id - id which become the cache file name
14629
+ * @param initialData - optional initial data
14630
+ * @returns true if success, false otherwise
14631
+ */
14632
+ static async createCache(cache_id, initialData = {}) {
14633
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14634
+ Bot.log.error('"Key" to create a cache must be a non-empty string');
14635
+ return false;
14636
+ }
14637
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14638
+ Bot.log.info(`Creating cache file: ${cleanCacheId}.json`);
14639
+ return await FileManager.writeJsonFile(
14640
+ this.cacheDir,
14641
+ this.cleanCacheId(cache_id),
14642
+ initialData,
14643
+ true
14644
+ );
14645
+ }
14646
+ /**
14647
+ * Read cache data or create empty one
14648
+ * @param cache_id
14649
+ * @param default_data default data with default key/value This will be send back if the cache doesn't exist and need to be created
14650
+ * @returns true if success, false otherwise
14651
+ */
14652
+ static async getOrCreateCache(cache_id, default_data) {
14653
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14654
+ Log.error('"cache_id" to get or create a cache must be a non-empty string');
14655
+ return false;
14656
+ }
14657
+ const cleanCacheId = this.cleanCacheId(cache_id);
14658
+ const filePath = this.createFilePath(cleanCacheId);
14659
+ if (await FileManager.fileExists(filePath)) {
14660
+ Log.info(`Cache already exists: ${cleanCacheId}.json`);
14661
+ return FileManager.readJsonFile(filePath);
14662
+ }
14663
+ Log.info(`Creating new cache file: ${this.cacheDir}/${cleanCacheId}.json`);
14664
+ const res = await FileManager.writeJsonFile(
14665
+ this.cacheDir,
14666
+ cleanCacheId,
14667
+ default_data,
14668
+ false
14669
+ );
14670
+ return res ? default_data : false;
14671
+ }
14672
+ /**
14673
+ * Read cache data
14674
+ * @param cache_id - cache_id to read
14675
+ * @returns Json data, false otherwise
14676
+ */
14677
+ static async readCache(cache_id) {
14678
+ if (!cache_id || typeof cache_id !== "string") {
14679
+ Bot.log.error('"Key" must be a string');
14680
+ return false;
14681
+ }
14682
+ const cleanCacheId = this.cleanCacheId(cache_id);
14683
+ const filePath = this.createFilePath(cleanCacheId);
14684
+ if (!await FileManager.fileExists(filePath)) {
14685
+ Log.debug(`Cache not found: ${cleanCacheId}.json`);
14686
+ return false;
14687
+ }
14688
+ return await FileManager.readJsonFile(filePath);
14689
+ }
14690
+ /**
14691
+ * Overwrite cache data with the new data
14692
+ * @param cache_id - ID of the cache
14693
+ * @param data - new data
14694
+ * @returns true if success, false otherwise
14695
+ */
14696
+ static async writeCache(cache_id, data) {
14697
+ if (!cache_id || typeof cache_id !== "string") {
14698
+ Log.error("Cache ID must be a string");
14699
+ return false;
14700
+ }
14701
+ const cleanCacheId = this.cleanCacheId(cache_id);
14702
+ Bot.log.info(`Writing to cache: ${cleanCacheId}.json`);
14703
+ return await FileManager.writeJsonFile(
14704
+ this.cacheDir,
14705
+ cleanCacheId,
14706
+ data,
14707
+ true
14708
+ );
14709
+ }
14710
+ /**
14711
+ * Update specific property of the cache
14712
+ * @param cache_id - ID of the cache
14713
+ * @param property - property to update, with key and value
14714
+ * @returns true if success, false otherwise
14715
+ */
14716
+ static async updateCacheProperty(cache_id, property) {
14717
+ const cacheData = await this.readCache(cache_id);
14718
+ if (cacheData === false) {
14719
+ Log.error(`Cache ${cache_id} not found`);
14720
+ return false;
14721
+ }
14722
+ cacheData[property.key] = property.value;
14723
+ return await this.writeCache(cache_id, cacheData);
14724
+ }
14725
+ /**
14726
+ * Reset an entire cache
14727
+ * @param cache_id - cache ID to reset
14728
+ * @returns true if success, false otherwise
14729
+ */
14730
+ static async resetCache(cache_id) {
14731
+ try {
14732
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14733
+ await this.writeCache(cache_id, {});
14734
+ Log.info(`Reset cache: ${cleanCacheId}.json`);
14735
+ return true;
14736
+ } catch (error) {
14737
+ Log.error(`Failed to reset cache ${cache_id}: ${error}`);
14738
+ return false;
14739
+ }
14740
+ }
14741
+ /**
14742
+ * Delete an entire cache
14743
+ * @param cache_id - cache ID to delete
14744
+ * @returns true if success, false otherwise
14745
+ */
14746
+ static async deleteCache(cache_id) {
14747
+ try {
14748
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14749
+ const filePath = this.createFilePath(cleanCacheId);
14750
+ if (await FileManager.deleteFile(filePath)) {
14751
+ Log.info(`Deleted cache: ${cleanCacheId}.json`);
14752
+ return true;
14753
+ }
14754
+ return false;
14755
+ } catch (error) {
14756
+ Log.error(`Failed to delete cache ${cache_id}: ${error}`);
14757
+ return false;
14758
+ }
14759
+ }
14760
+ /**
14761
+ * List every existing cache
14762
+ * @returns Array of cache_id, false otherwise
14763
+ */
14764
+ static async listCaches() {
14765
+ return await FileManager.listJsonFiles(this.cacheDir);
14766
+ }
14767
+ /**
14768
+ * Clean cache data
14769
+ * @returns true if success, false otherwise
14770
+ */
14771
+ static async clearAllCaches() {
14772
+ try {
14773
+ const caches = await this.listCaches();
14774
+ if (caches === false || caches.length === 0) {
14775
+ return true;
14776
+ }
14777
+ let success = true;
14778
+ for (const cache of caches) {
14779
+ const result = await this.deleteCache(cache.replace(".json", ""));
14780
+ if (!result) success = false;
14781
+ }
14782
+ return success;
14783
+ } catch (error) {
14784
+ Log.error(`Failed to clear caches: ${error}`);
14785
+ return false;
14786
+ }
14787
+ }
14788
+ };
14789
+
14614
14790
  // src/manager/messages/WebhookManager.ts
14615
14791
  import {
14616
14792
  BaseGuildTextChannel,
@@ -16259,6 +16435,7 @@ export {
16259
16435
  Bot,
16260
16436
  BotEnv,
16261
16437
  ButtonManager,
16438
+ CacheManager,
16262
16439
  ComponentManager,
16263
16440
  DiscordRegex,
16264
16441
  EmbedManager,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spatulox/simplediscordbot",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "author": "Spatulox",
5
5
  "description": "Simple discord bot framework to set up a bot under 30 secondes",
6
6
  "exports": {