@somehiddenkey/discord-command-utils 2.0.0 → 2.1.0

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
@@ -66,6 +66,44 @@ export const globalConfig = GlobalConfig.load("../global_config.json");
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.0",
3
+ "version": "2.1.0",
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
  }
@@ -2,7 +2,7 @@
2
2
  * @typedef {string} Snowflake
3
3
  */
4
4
 
5
- import ConfigError from "./ConfigError.js";
5
+ import ConfigError from "../ConfigError.js";
6
6
  import pkg from 'discord.js';
7
7
  import { GuildBased, ChannelBased, RoleBased } from '../NestedGlobalConfig.js';
8
8
  const { GuildBasedChannel } = pkg;
@@ -2,7 +2,7 @@
2
2
  * @typedef {string} Snowflake
3
3
  */
4
4
 
5
- import ConfigError from "./ConfigError.js";
5
+ import ConfigError from "../ConfigError.js";
6
6
  import pkg from 'discord.js';
7
7
  import { GuildBased, ChannelBased, RoleBased } from '../NestedGlobalConfig.js';
8
8
  const { GuildBasedChannel } = pkg;
@@ -22,10 +22,10 @@ export default class Guild extends GuildBased {
22
22
  throw new ConfigError('No data initialized for GlobalConfig');
23
23
 
24
24
  super(data);
25
- this.categories = new Categories(data.categories, data.id);
26
- this.channels = new Channels(data.channels, data.id);
27
- this.logs = new Logs(data.logs, data.id);
28
- this.roles = new Roles(data.roles, data.id);
25
+ this.categories = new Categories(data.categories, this.guild);
26
+ this.channels = new Channels(data.channels, this.guild);
27
+ this.logs = new Logs(data.logs, this.guild);
28
+ this.roles = new Roles(data.roles, this.guild);
29
29
  }
30
30
  }
31
31
 
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @typedef {string} Snowflake
3
+ */
4
+
5
+ import ConfigError from "../ConfigError.js";
6
+ import pkg from 'discord.js';
7
+ import { GuildBased, ChannelBased, RoleBased } from '../NestedGlobalConfig.js';
8
+ const { GuildBasedChannel } = pkg;
9
+
10
+ export default class Guild extends GuildBased {
11
+ /** @type {Categories} */
12
+ categories;
13
+ /** @type {Channels} */
14
+ channels;
15
+ /** @type {Logs} */
16
+ logs;
17
+ /** @type {Roles} */
18
+ roles;
19
+
20
+ constructor(data) {
21
+ if (!data)
22
+ throw new ConfigError('No data initialized for GlobalConfig');
23
+
24
+ super(data);
25
+ this.categories = new Categories(data.categories, data.id);
26
+ this.channels = new Channels(data.channels, data.id);
27
+ this.logs = new Logs(data.logs, data.id);
28
+ this.roles = new Roles(data.roles, data.id);
29
+ }
30
+ }
31
+
32
+ class Categories extends ChannelBased {}
33
+
34
+ class Channels extends ChannelBased {}
35
+
36
+ class Logs extends ChannelBased {}
37
+
38
+ class Roles extends RoleBased {}
@@ -2,7 +2,7 @@
2
2
  * @typedef {string} Snowflake
3
3
  */
4
4
 
5
- import ConfigError from "./ConfigError.js";
5
+ import ConfigError from "../ConfigError.js";
6
6
  import pkg from 'discord.js';
7
7
  import { GuildBased, ChannelBased, RoleBased } from '../NestedGlobalConfig.js';
8
8
  const { GuildBasedChannel } = pkg;
@@ -3,8 +3,10 @@
3
3
  */
4
4
 
5
5
  import Fetchable from './Fetchable.js';
6
+ import {Guild} from 'discord.js';
6
7
 
7
8
  export class GuildBased extends Fetchable {
9
+ /** @type {Guild} */
8
10
  guild;
9
11
  categories;
10
12
  channels;
@@ -20,13 +22,17 @@ export class GuildBased extends Fetchable {
20
22
  }
21
23
 
22
24
  constructor(data){
23
- this._guild = this._createLazyObject(data.id);
25
+ this.guild = this._createLazyObject(data.id);
24
26
  this._config_keys = Object.keys(data).filter(k => k !== 'id').push('guild');
25
27
  }
26
28
  }
27
29
 
28
30
  export class ChannelBased extends Fetchable {
29
- constructor(data) {
31
+ /** @type {Guild} */
32
+ #guild;
33
+
34
+ constructor(data, guild) {
35
+ this.#guild = guild;
30
36
  super(data);
31
37
  }
32
38
 
@@ -35,12 +41,16 @@ export class ChannelBased extends Fetchable {
35
41
  * @returns {Proxy}
36
42
  */
37
43
  _createLazyObject(id) {
38
- return super._createLazyObject(id, async (id) => await bot.channels.fetch(id));
44
+ return super._createLazyObject(id, async (id) => await this.#guild.fetch().then(guild => guild.channels.fetch(id)));
39
45
  }
40
46
  }
41
47
 
42
48
  export class RoleBased extends Fetchable {
49
+ /** @type {Guild} */
50
+ #guild;
51
+
43
52
  constructor(data) {
53
+ this.#guild = guild;
44
54
  super(data);
45
55
  }
46
56
 
@@ -49,6 +59,6 @@ export class RoleBased extends Fetchable {
49
59
  * @returns {Proxy}
50
60
  */
51
61
  _createLazyObject(id) {
52
- return super._createLazyObject(id, async (id) => await bot.roles.fetch(id));
62
+ return super._createLazyObject(id, async (id) => await this.#guild.fetch().then(guild => guild.roles.fetch(id)));
53
63
  }
54
64
  }