@spatulox/simplediscordbot 2.1.0 → 2.1.2

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/CHANGELOG.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # Changelog
2
2
  Date format : dd/mm/yyy
3
3
 
4
- ### 15/04/2026 - 2.1.0
4
+ ### 15/04/2026 - 2.1.2
5
+ - CacheManager should now take the botname in lowercase and escape weird char
6
+
7
+ ### 15/04/2026 - 2.1.0 & 2.1.1
5
8
  - Add a CacheManager for simple persisting data
6
9
  - Add QoL method to FileManager (fileExist(), deleteFile())
7
10
  - Fix :
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.1",
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,190 @@ 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
+ let folderPath = "simplediscordbot";
14626
+ if (Bot.config?.botName) {
14627
+ folderPath = this.cleanCacheId(Bot.config?.botName?.toLowerCase()).replace(" ", "_");
14628
+ }
14629
+ return import_path2.default.join(process.cwd(), `.${folderPath}cache`);
14630
+ }
14631
+ static cleanCacheId(key) {
14632
+ return key.replace(/[^a-zA-Z0-9_-]/g, "_");
14633
+ }
14634
+ static createFilePath(filename) {
14635
+ return import_path2.default.join(this.cacheDir, `${filename}.json`);
14636
+ }
14637
+ /**
14638
+ * New file with cache_id as name
14639
+ * @param cache_id - id which become the cache file name
14640
+ * @param initialData - optional initial data
14641
+ * @returns true if success, false otherwise
14642
+ */
14643
+ static async createCache(cache_id, initialData = {}) {
14644
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14645
+ Bot.log.error('"Key" to create a cache must be a non-empty string');
14646
+ return false;
14647
+ }
14648
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14649
+ Bot.log.info(`Creating cache file: ${cleanCacheId}.json`);
14650
+ return await FileManager.writeJsonFile(
14651
+ this.cacheDir,
14652
+ this.cleanCacheId(cache_id),
14653
+ initialData,
14654
+ true
14655
+ );
14656
+ }
14657
+ /**
14658
+ * Read cache data or create empty one
14659
+ * @param cache_id
14660
+ * @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
14661
+ * @returns true if success, false otherwise
14662
+ */
14663
+ static async getOrCreateCache(cache_id, default_data) {
14664
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14665
+ Log.error('"cache_id" to get or create a cache must be a non-empty string');
14666
+ return false;
14667
+ }
14668
+ const cleanCacheId = this.cleanCacheId(cache_id);
14669
+ const filePath = this.createFilePath(cleanCacheId);
14670
+ if (await FileManager.fileExists(filePath)) {
14671
+ Log.info(`Cache already exists: ${cleanCacheId}.json`);
14672
+ return FileManager.readJsonFile(filePath);
14673
+ }
14674
+ Log.info(`Creating new cache file: ${this.cacheDir}/${cleanCacheId}.json`);
14675
+ const res = await FileManager.writeJsonFile(
14676
+ this.cacheDir,
14677
+ cleanCacheId,
14678
+ default_data,
14679
+ false
14680
+ );
14681
+ return res ? default_data : false;
14682
+ }
14683
+ /**
14684
+ * Read cache data
14685
+ * @param cache_id - cache_id to read
14686
+ * @returns Json data, false otherwise
14687
+ */
14688
+ static async readCache(cache_id) {
14689
+ if (!cache_id || typeof cache_id !== "string") {
14690
+ Bot.log.error('"Key" must be a string');
14691
+ return false;
14692
+ }
14693
+ const cleanCacheId = this.cleanCacheId(cache_id);
14694
+ const filePath = this.createFilePath(cleanCacheId);
14695
+ if (!await FileManager.fileExists(filePath)) {
14696
+ Log.debug(`Cache not found: ${cleanCacheId}.json`);
14697
+ return false;
14698
+ }
14699
+ return await FileManager.readJsonFile(filePath);
14700
+ }
14701
+ /**
14702
+ * Overwrite cache data with the new data
14703
+ * @param cache_id - ID of the cache
14704
+ * @param data - new data
14705
+ * @returns true if success, false otherwise
14706
+ */
14707
+ static async writeCache(cache_id, data) {
14708
+ if (!cache_id || typeof cache_id !== "string") {
14709
+ Log.error("Cache ID must be a string");
14710
+ return false;
14711
+ }
14712
+ const cleanCacheId = this.cleanCacheId(cache_id);
14713
+ Bot.log.info(`Writing to cache: ${cleanCacheId}.json`);
14714
+ return await FileManager.writeJsonFile(
14715
+ this.cacheDir,
14716
+ cleanCacheId,
14717
+ data,
14718
+ true
14719
+ );
14720
+ }
14721
+ /**
14722
+ * Update specific property of the cache
14723
+ * @param cache_id - ID of the cache
14724
+ * @param property - property to update, with key and value
14725
+ * @returns true if success, false otherwise
14726
+ */
14727
+ static async updateCacheProperty(cache_id, property) {
14728
+ const cacheData = await this.readCache(cache_id);
14729
+ if (cacheData === false) {
14730
+ Log.error(`Cache ${cache_id} not found`);
14731
+ return false;
14732
+ }
14733
+ cacheData[property.key] = property.value;
14734
+ return await this.writeCache(cache_id, cacheData);
14735
+ }
14736
+ /**
14737
+ * Reset an entire cache
14738
+ * @param cache_id - cache ID to reset
14739
+ * @returns true if success, false otherwise
14740
+ */
14741
+ static async resetCache(cache_id) {
14742
+ try {
14743
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14744
+ await this.writeCache(cache_id, {});
14745
+ Log.info(`Reset cache: ${cleanCacheId}.json`);
14746
+ return true;
14747
+ } catch (error) {
14748
+ Log.error(`Failed to reset cache ${cache_id}: ${error}`);
14749
+ return false;
14750
+ }
14751
+ }
14752
+ /**
14753
+ * Delete an entire cache
14754
+ * @param cache_id - cache ID to delete
14755
+ * @returns true if success, false otherwise
14756
+ */
14757
+ static async deleteCache(cache_id) {
14758
+ try {
14759
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14760
+ const filePath = this.createFilePath(cleanCacheId);
14761
+ if (await FileManager.deleteFile(filePath)) {
14762
+ Log.info(`Deleted cache: ${cleanCacheId}.json`);
14763
+ return true;
14764
+ }
14765
+ return false;
14766
+ } catch (error) {
14767
+ Log.error(`Failed to delete cache ${cache_id}: ${error}`);
14768
+ return false;
14769
+ }
14770
+ }
14771
+ /**
14772
+ * List every existing cache
14773
+ * @returns Array of cache_id, false otherwise
14774
+ */
14775
+ static async listCaches() {
14776
+ return await FileManager.listJsonFiles(this.cacheDir);
14777
+ }
14778
+ /**
14779
+ * Clean cache data
14780
+ * @returns true if success, false otherwise
14781
+ */
14782
+ static async clearAllCaches() {
14783
+ try {
14784
+ const caches = await this.listCaches();
14785
+ if (caches === false || caches.length === 0) {
14786
+ return true;
14787
+ }
14788
+ let success = true;
14789
+ for (const cache of caches) {
14790
+ const result = await this.deleteCache(cache.replace(".json", ""));
14791
+ if (!result) success = false;
14792
+ }
14793
+ return success;
14794
+ } catch (error) {
14795
+ Log.error(`Failed to clear caches: ${error}`);
14796
+ return false;
14797
+ }
14798
+ }
14799
+ };
14800
+
14620
14801
  // src/manager/messages/WebhookManager.ts
14621
14802
  var import_discord7 = require("discord.js");
14622
14803
  var import_promises2 = require("fs/promises");
14623
- var import_path2 = require("path");
14804
+ var import_path3 = require("path");
14624
14805
  var WebhookManager = class {
14625
14806
  constructor(client, name, avatarPathOrUrl) {
14626
14807
  this.client = client;
@@ -14634,7 +14815,7 @@ var WebhookManager = class {
14634
14815
  return this.avatarPathOrUrl;
14635
14816
  }
14636
14817
  try {
14637
- const resolvedPath = (0, import_path2.resolve)(process.cwd(), this.avatarPathOrUrl);
14818
+ const resolvedPath = (0, import_path3.resolve)(process.cwd(), this.avatarPathOrUrl);
14638
14819
  return await (0, import_promises2.readFile)(resolvedPath);
14639
14820
  } catch (error) {
14640
14821
  Bot.log.warn(`Failed to load avatar from ${this.avatarPathOrUrl}: ${error}`);
@@ -16238,6 +16419,7 @@ var SimpleMutex = class {
16238
16419
  Bot,
16239
16420
  BotEnv,
16240
16421
  ButtonManager,
16422
+ CacheManager,
16241
16423
  ComponentManager,
16242
16424
  DiscordRegex,
16243
16425
  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.1",
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,186 @@ 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
+ let folderPath = "simplediscordbot";
14619
+ if (Bot.config?.botName) {
14620
+ folderPath = this.cleanCacheId(Bot.config?.botName?.toLowerCase()).replace(" ", "_");
14621
+ }
14622
+ return path2.join(process.cwd(), `.${folderPath}cache`);
14623
+ }
14624
+ static cleanCacheId(key) {
14625
+ return key.replace(/[^a-zA-Z0-9_-]/g, "_");
14626
+ }
14627
+ static createFilePath(filename) {
14628
+ return path2.join(this.cacheDir, `${filename}.json`);
14629
+ }
14630
+ /**
14631
+ * New file with cache_id as name
14632
+ * @param cache_id - id which become the cache file name
14633
+ * @param initialData - optional initial data
14634
+ * @returns true if success, false otherwise
14635
+ */
14636
+ static async createCache(cache_id, initialData = {}) {
14637
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14638
+ Bot.log.error('"Key" to create a cache must be a non-empty string');
14639
+ return false;
14640
+ }
14641
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14642
+ Bot.log.info(`Creating cache file: ${cleanCacheId}.json`);
14643
+ return await FileManager.writeJsonFile(
14644
+ this.cacheDir,
14645
+ this.cleanCacheId(cache_id),
14646
+ initialData,
14647
+ true
14648
+ );
14649
+ }
14650
+ /**
14651
+ * Read cache data or create empty one
14652
+ * @param cache_id
14653
+ * @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
14654
+ * @returns true if success, false otherwise
14655
+ */
14656
+ static async getOrCreateCache(cache_id, default_data) {
14657
+ if (!cache_id || typeof cache_id !== "string" || cache_id.trim() === "") {
14658
+ Log.error('"cache_id" to get or create a cache must be a non-empty string');
14659
+ return false;
14660
+ }
14661
+ const cleanCacheId = this.cleanCacheId(cache_id);
14662
+ const filePath = this.createFilePath(cleanCacheId);
14663
+ if (await FileManager.fileExists(filePath)) {
14664
+ Log.info(`Cache already exists: ${cleanCacheId}.json`);
14665
+ return FileManager.readJsonFile(filePath);
14666
+ }
14667
+ Log.info(`Creating new cache file: ${this.cacheDir}/${cleanCacheId}.json`);
14668
+ const res = await FileManager.writeJsonFile(
14669
+ this.cacheDir,
14670
+ cleanCacheId,
14671
+ default_data,
14672
+ false
14673
+ );
14674
+ return res ? default_data : false;
14675
+ }
14676
+ /**
14677
+ * Read cache data
14678
+ * @param cache_id - cache_id to read
14679
+ * @returns Json data, false otherwise
14680
+ */
14681
+ static async readCache(cache_id) {
14682
+ if (!cache_id || typeof cache_id !== "string") {
14683
+ Bot.log.error('"Key" must be a string');
14684
+ return false;
14685
+ }
14686
+ const cleanCacheId = this.cleanCacheId(cache_id);
14687
+ const filePath = this.createFilePath(cleanCacheId);
14688
+ if (!await FileManager.fileExists(filePath)) {
14689
+ Log.debug(`Cache not found: ${cleanCacheId}.json`);
14690
+ return false;
14691
+ }
14692
+ return await FileManager.readJsonFile(filePath);
14693
+ }
14694
+ /**
14695
+ * Overwrite cache data with the new data
14696
+ * @param cache_id - ID of the cache
14697
+ * @param data - new data
14698
+ * @returns true if success, false otherwise
14699
+ */
14700
+ static async writeCache(cache_id, data) {
14701
+ if (!cache_id || typeof cache_id !== "string") {
14702
+ Log.error("Cache ID must be a string");
14703
+ return false;
14704
+ }
14705
+ const cleanCacheId = this.cleanCacheId(cache_id);
14706
+ Bot.log.info(`Writing to cache: ${cleanCacheId}.json`);
14707
+ return await FileManager.writeJsonFile(
14708
+ this.cacheDir,
14709
+ cleanCacheId,
14710
+ data,
14711
+ true
14712
+ );
14713
+ }
14714
+ /**
14715
+ * Update specific property of the cache
14716
+ * @param cache_id - ID of the cache
14717
+ * @param property - property to update, with key and value
14718
+ * @returns true if success, false otherwise
14719
+ */
14720
+ static async updateCacheProperty(cache_id, property) {
14721
+ const cacheData = await this.readCache(cache_id);
14722
+ if (cacheData === false) {
14723
+ Log.error(`Cache ${cache_id} not found`);
14724
+ return false;
14725
+ }
14726
+ cacheData[property.key] = property.value;
14727
+ return await this.writeCache(cache_id, cacheData);
14728
+ }
14729
+ /**
14730
+ * Reset an entire cache
14731
+ * @param cache_id - cache ID to reset
14732
+ * @returns true if success, false otherwise
14733
+ */
14734
+ static async resetCache(cache_id) {
14735
+ try {
14736
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14737
+ await this.writeCache(cache_id, {});
14738
+ Log.info(`Reset cache: ${cleanCacheId}.json`);
14739
+ return true;
14740
+ } catch (error) {
14741
+ Log.error(`Failed to reset cache ${cache_id}: ${error}`);
14742
+ return false;
14743
+ }
14744
+ }
14745
+ /**
14746
+ * Delete an entire cache
14747
+ * @param cache_id - cache ID to delete
14748
+ * @returns true if success, false otherwise
14749
+ */
14750
+ static async deleteCache(cache_id) {
14751
+ try {
14752
+ const cleanCacheId = cache_id.replace(/[^a-zA-Z0-9_-]/g, "_");
14753
+ const filePath = this.createFilePath(cleanCacheId);
14754
+ if (await FileManager.deleteFile(filePath)) {
14755
+ Log.info(`Deleted cache: ${cleanCacheId}.json`);
14756
+ return true;
14757
+ }
14758
+ return false;
14759
+ } catch (error) {
14760
+ Log.error(`Failed to delete cache ${cache_id}: ${error}`);
14761
+ return false;
14762
+ }
14763
+ }
14764
+ /**
14765
+ * List every existing cache
14766
+ * @returns Array of cache_id, false otherwise
14767
+ */
14768
+ static async listCaches() {
14769
+ return await FileManager.listJsonFiles(this.cacheDir);
14770
+ }
14771
+ /**
14772
+ * Clean cache data
14773
+ * @returns true if success, false otherwise
14774
+ */
14775
+ static async clearAllCaches() {
14776
+ try {
14777
+ const caches = await this.listCaches();
14778
+ if (caches === false || caches.length === 0) {
14779
+ return true;
14780
+ }
14781
+ let success = true;
14782
+ for (const cache of caches) {
14783
+ const result = await this.deleteCache(cache.replace(".json", ""));
14784
+ if (!result) success = false;
14785
+ }
14786
+ return success;
14787
+ } catch (error) {
14788
+ Log.error(`Failed to clear caches: ${error}`);
14789
+ return false;
14790
+ }
14791
+ }
14792
+ };
14793
+
14614
14794
  // src/manager/messages/WebhookManager.ts
14615
14795
  import {
14616
14796
  BaseGuildTextChannel,
@@ -16259,6 +16439,7 @@ export {
16259
16439
  Bot,
16260
16440
  BotEnv,
16261
16441
  ButtonManager,
16442
+ CacheManager,
16262
16443
  ComponentManager,
16263
16444
  DiscordRegex,
16264
16445
  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.2",
4
4
  "author": "Spatulox",
5
5
  "description": "Simple discord bot framework to set up a bot under 30 secondes",
6
6
  "exports": {