@somehiddenkey/discord-command-utils 2.0.1 → 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/README.md CHANGED
@@ -62,10 +62,48 @@ Your local configuration JSON file should look like this:
62
62
  ### Global configuration
63
63
  Loads the global configuration that contains information related to all bot instances, like snowflake IDs of channels, categories, guild, etc. Load a new instance of your global config through the following code:
64
64
  ```js
65
- export const globalConfig = GlobalConfig.load("../global_config.json");
65
+ export const globalConfig = GlobalConfig.load("../global_config.json", bot);
66
66
  ```
67
67
  It contains the IDs of the following sections: guilds, categories, channels, log channels and roles. These can be used as constant values throughout your code, making sure that if an ID ever changes, that all bot instances would take the new updated value by simply updating the json once.
68
68
 
69
+ #### Properties
70
+ The properties of the different guilds can read per scope/guild. We differentiate the main, tutor, staff and community server:
71
+ - Get the ID of the main server : `globalConfig.main.guild.id`
72
+ - Get the ID of the general chat in the main server: `globalConfig.main.channels.international.id`
73
+
74
+ #### Caching
75
+ You can fetch and cache the value of a guild, channel or role from discordjs, as to access it immidiately afterwards. However, at all times, the id is available even without fetching
76
+ ```js
77
+ console.write(globalConfig.main.guild.name); //error: not fetched yet
78
+ console.write(globalConfig.main.guild.id); //still fine, ID always accessible
79
+
80
+ await globalConfig.main.guild.fetch();
81
+ await globalConfig.main.channels.international.fetch();
82
+ console.write(globalConfig.main.guild.name); //uses the cached value
83
+ ```
84
+
85
+ Please keep in mind that cached value may contain outdated data! If you call `fetch()` it will either return the cached value or fetch the value once. If you want to reload the value e.g. to read the updated members that are in a specific voicechannel, you must call `refresh()`
86
+
87
+ ```js
88
+ console.write(globalConfig.main.channels.break_afk.members.length);
89
+ //shows the member count at the time of the fetch()
90
+
91
+ await globalConfig.main.channels.break_afk.refresh();
92
+ //fetches the value again, and caches it again
93
+
94
+ console.write(globalConfig.main.channels.break_afk.members.length);
95
+ //shows the now updated member count
96
+ ```
97
+
98
+ You can fetch and refresh entire groups or guilds. Please keep in mind that, if you fetch the values of a specific guild, the bot has to actually be part of that server. A value that cannot be fetched will result in an error.
99
+
100
+ ```js
101
+ await globalConfig.community.fetch();
102
+ await globalConfig.main.channels.fetch();
103
+
104
+ console.write(globalConfig.main.channel.international.name); //uses the cached value
105
+ ```
106
+
69
107
  ### Secrets configuration
70
108
  Loads the environment variables that contain all secrets for that bot, like your bot token and database credentials. It goes without saying that these actual values may never be git traced. Load new instance of your secret config through the following code:
71
109
  ```js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@somehiddenkey/discord-command-utils",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "A utility library for building Discord bot commands using discord.js",
5
5
  "author": {
6
6
  "email": "k3y.throwaway@gmail.com",
@@ -25,6 +25,13 @@ export default class Fetchable {
25
25
  if (prop === "id")
26
26
  return id;
27
27
 
28
+ if (prop === "refresh") {
29
+ return async () => {
30
+ cachedObject = await fetcher(id);
31
+ return cachedObject;
32
+ };
33
+ }
34
+
28
35
  if (prop === "fetch") {
29
36
  return async () => {
30
37
  if (!cachedObject){
@@ -66,4 +73,13 @@ export default class Fetchable {
66
73
  this._config_keys.map((key) => this[key]?.fetch())
67
74
  );
68
75
  }
76
+
77
+ /**
78
+ * @returns {Promise<void[]>}
79
+ */
80
+ refresh() {
81
+ return Promise.all(
82
+ this._config_keys.map((key) => this[key]?.refresh())
83
+ );
84
+ }
69
85
  }
@@ -10,21 +10,22 @@ import {default as GuildCommunity} from './GlobalConfigCommunity.js';
10
10
  import {default as GuildTutor} from './GlobalConfigTutor.js';
11
11
  import {default as GuildStaff} from './GlobalConfigStaff.js';
12
12
  import { GuildBased } from '../NestedGlobalConfig.js';
13
+ import { Client } from 'discord.js';
13
14
 
14
15
  export default class GlobalConfig {
15
16
  main
16
17
  community
17
18
  tutor
18
19
  staff
19
-
20
+
20
21
  /**
21
22
  * Loads the JSON file and returns a GlobalConfig instance.
22
23
  * @param {string} path
23
- * @param {string} environment
24
+ * @param {Client} bot
24
25
  * @returns {GlobalConfig}
25
26
  */
26
- static async load(path) {
27
- var json
27
+ static async load(path, bot) {
28
+ var json;
28
29
  try {
29
30
  const raw = fs.readFileSync(path, { encoding: 'utf8', flag: 'r' });
30
31
  json = JSON.parse(raw);
@@ -32,7 +33,7 @@ export default class GlobalConfig {
32
33
  consola.error(`Failed to load GlobalConfig from path: ${path}`, error);
33
34
  throw error;
34
35
  }
35
- return new GlobalConfig(json.guilds);
36
+ return new GlobalConfig(json.guilds, bot);
36
37
  }
37
38
 
38
39
  /**
@@ -73,11 +74,11 @@ export default class GlobalConfig {
73
74
  }
74
75
  }
75
76
 
76
- constructor(data) {
77
- this.main = new GuildMain(data.main);
78
- this.community = new GuildCommunity(data.community);
79
- this.tutor = new GuildTutor(data.tutor);
80
- this.staff = new GuildStaff(data.staff);
77
+ constructor(data, bot) {
78
+ this.main = new GuildMain(data.main, bot);
79
+ this.community = new GuildCommunity(data.community, bot);
80
+ this.tutor = new GuildTutor(data.tutor, bot);
81
+ this.staff = new GuildStaff(data.staff, bot);
81
82
  }
82
83
  }
83
84
 
@@ -17,11 +17,11 @@ export default class Guild extends GuildBased {
17
17
  /** @type {Roles} */
18
18
  roles;
19
19
 
20
- constructor(data) {
20
+ constructor(data, bot) {
21
21
  if (!data)
22
22
  throw new ConfigError('No data initialized for GlobalConfig');
23
23
 
24
- super(data);
24
+ super(data, bot);
25
25
  this.categories = new Categories(data.categories, this.guild);
26
26
  this.channels = new Channels(data.channels, this.guild);
27
27
  this.logs = new Logs(data.logs, this.guild);
@@ -65,8 +65,8 @@ class Categories extends ChannelBased {
65
65
  /** @type {Set<Snowflake>} */
66
66
  study_rooms;
67
67
 
68
- constructor(data) {
69
- super(data);
68
+ constructor(data, guild) {
69
+ super(data, guild);
70
70
  this.custom_study_rooms = new Set(data.custom_rooms.concat(data.verified_com));
71
71
  this.study_rooms = new Set([...this.custom_study_rooms, data.music, data.timer_25, data.timer_50, data.screen_cam]);
72
72
  }
@@ -166,8 +166,8 @@ class Roles extends RoleBased {
166
166
  all_staff;
167
167
 
168
168
 
169
- constructor(data) {
170
- Object.assign(this, data);
169
+ constructor(data, guild) {
170
+ super(data, guild);
171
171
  this.all_staff = [this.jr_staff, this.sr_staff, this.st_team, this.coordinator];
172
172
  }
173
173
  }
@@ -17,11 +17,11 @@ export default class Guild extends GuildBased {
17
17
  /** @type {Roles} */
18
18
  roles;
19
19
 
20
- constructor(data) {
20
+ constructor(data, bot) {
21
21
  if (!data)
22
22
  throw new ConfigError('No data initialized for GlobalConfig');
23
23
 
24
- super(data);
24
+ super(data, bot);
25
25
  this.categories = new Categories(data.categories, data.id);
26
26
  this.channels = new Channels(data.channels, data.id);
27
27
  this.logs = new Logs(data.logs, data.id);
@@ -17,11 +17,11 @@ export default class Guild extends GuildBased {
17
17
  /** @type {Roles} */
18
18
  roles;
19
19
 
20
- constructor(data) {
20
+ constructor(data, bot) {
21
21
  if (!data)
22
22
  throw new ConfigError('No data initialized for GlobalConfig');
23
23
 
24
- super(data);
24
+ super(data, bot);
25
25
  this.categories = new Categories(data.categories, data.id);
26
26
  this.channels = new Channels(data.channels, data.id);
27
27
  this.logs = new Logs(data.logs, data.id);
@@ -17,12 +17,12 @@ export class GuildBased extends Fetchable {
17
17
  * @param {Snowflake} id
18
18
  * @returns {Proxy}
19
19
  */
20
- _createLazyObject(id) {
20
+ _createLazyObject(id, bot) {
21
21
  return super._createLazyObject(id, async (id) => await bot.guilds.fetch(id));
22
22
  }
23
23
 
24
- constructor(data){
25
- this.guild = this._createLazyObject(data.id);
24
+ constructor(data, bot) {
25
+ this.guild = this._createLazyObject(data.id, bot);
26
26
  this._config_keys = Object.keys(data).filter(k => k !== 'id').push('guild');
27
27
  }
28
28
  }
@@ -49,7 +49,7 @@ export class RoleBased extends Fetchable {
49
49
  /** @type {Guild} */
50
50
  #guild;
51
51
 
52
- constructor(data) {
52
+ constructor(data, guild) {
53
53
  this.#guild = guild;
54
54
  super(data);
55
55
  }