@somehiddenkey/discord-command-utils 1.0.3 → 1.2.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
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
# DiscordUtilsLibrary
|
|
2
2
|
The purpose of this Library is to hide and abstract many of the common aspects that comes with coding discord bots in javascript. The main focus is configurations as well as registering all types of commands and interactions between different guilds.
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
Use this if your GitHub account already has SSH access -> requires SSH key set up
|
|
3
|
+
## Setup
|
|
4
|
+
1. Installation
|
|
6
5
|
```bash
|
|
7
|
-
npm install
|
|
6
|
+
npm install @somehiddenkey/discord-command-utils --registry=https://registry.npmjs.org/
|
|
8
7
|
```
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
If you want to use HTTPS instead, you must use a GitHub Personal Access Token (classic) with repo permissions (replace "TOKEN" with your PAT)
|
|
12
|
-
```bash
|
|
13
|
-
npm install "https://TOKEN@github.com/studytogether-discord/DiscordUtilsLibrary.git"
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
### Libary Import
|
|
17
|
-
Then import it in your code
|
|
9
|
+
2. Libary Import
|
|
18
10
|
```c#
|
|
19
11
|
import { Something } from "DiscordUtilsLibrary";
|
|
20
12
|
// or
|
|
@@ -137,7 +129,7 @@ bot.on(Events.MessageCreate, async (message) => {
|
|
|
137
129
|
});
|
|
138
130
|
|
|
139
131
|
bot.on(Events.InteractionCreate, async (interaction) =>
|
|
140
|
-
await interactionContainer.call_interaction(
|
|
132
|
+
await interactionContainer.call_interaction(interaction, e => consola.error(e))
|
|
141
133
|
);
|
|
142
134
|
```
|
|
143
135
|
### Register slash commands to REST
|
package/package.json
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import ConfigError from "./ConfigError.js";
|
|
3
|
+
import consola from 'consola';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* @typedef {string} Snowflake
|
|
3
7
|
*/
|
|
@@ -22,7 +26,7 @@ export default class LocalConfig {
|
|
|
22
26
|
this.restOptions = data.rest_options || {};
|
|
23
27
|
this.activity = data.activity || {};
|
|
24
28
|
|
|
25
|
-
if(!this.
|
|
29
|
+
if(!this.clientId)
|
|
26
30
|
throw new ConfigError('No client_id initialized for LocalConfig');
|
|
27
31
|
}
|
|
28
32
|
|
|
@@ -30,7 +34,7 @@ export default class LocalConfig {
|
|
|
30
34
|
* Loads the JSON file and returns a GlobalConfig instance.
|
|
31
35
|
* @param {string} path
|
|
32
36
|
* @param {string} environment
|
|
33
|
-
* @returns {
|
|
37
|
+
* @returns {LocalConfig}
|
|
34
38
|
*/
|
|
35
39
|
static async load(path, environment) {
|
|
36
40
|
var json
|
|
@@ -38,12 +42,12 @@ export default class LocalConfig {
|
|
|
38
42
|
const raw = fs.readFileSync(path, { encoding: 'utf8', flag: 'r' });
|
|
39
43
|
json = JSON.parse(raw);
|
|
40
44
|
} catch (error) {
|
|
41
|
-
consola.error(`Failed to load
|
|
45
|
+
consola.error(`Failed to load LocalConfig from path: ${path}`, error);
|
|
42
46
|
throw error;
|
|
43
47
|
}
|
|
44
48
|
if (environment == "production")
|
|
45
|
-
return new
|
|
49
|
+
return new LocalConfig({...json.production || json.activity});
|
|
46
50
|
else
|
|
47
|
-
return new
|
|
51
|
+
return new LocalConfig({...json.development || json.activity});
|
|
48
52
|
}
|
|
49
53
|
}
|
|
@@ -44,9 +44,9 @@ export default class Interaction {
|
|
|
44
44
|
*/
|
|
45
45
|
static On(custom_id, type, scope, target) {
|
|
46
46
|
if(Array.isArray(scope))
|
|
47
|
-
scope.forEach(s => InteractionContainer.add(new Interaction(custom_id,
|
|
47
|
+
scope.forEach(s => InteractionContainer.add(new Interaction(custom_id, type, s, target)));
|
|
48
48
|
else;
|
|
49
|
-
InteractionContainer.add(new Interaction(custom_id,
|
|
49
|
+
InteractionContainer.add(new Interaction(custom_id, type, scope, target));
|
|
50
50
|
return custom_id;
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -74,7 +74,10 @@ export default class InteractionContainer {
|
|
|
74
74
|
*/
|
|
75
75
|
call_message_interaction(interaction, on_error = async () => {}) {
|
|
76
76
|
if (interaction.author.bot) return;
|
|
77
|
-
|
|
77
|
+
|
|
78
|
+
if(!interaction.content.startsWith(this.#local_config.prefix))
|
|
79
|
+
return;
|
|
80
|
+
|
|
78
81
|
const scope =
|
|
79
82
|
(interaction.channel.type === ChannelType.DM) ?
|
|
80
83
|
InteractionScope.DM : this.#global_config.guilds.get_scope(interaction.guild.id);
|
|
@@ -82,7 +85,7 @@ export default class InteractionContainer {
|
|
|
82
85
|
const customId = interaction.content.slice(this.#local_config.prefix?.length)
|
|
83
86
|
|
|
84
87
|
return InteractionContainer
|
|
85
|
-
.#call(InteractionType.Message, scope, customId, interaction)
|
|
88
|
+
.#call(InteractionType.Message, scope, [customId], interaction)
|
|
86
89
|
.catch(error => {
|
|
87
90
|
if (error instanceof CommandError)
|
|
88
91
|
return error.error_as_message()
|
|
@@ -137,16 +140,16 @@ export default class InteractionContainer {
|
|
|
137
140
|
*
|
|
138
141
|
* @param {InteractionType} type
|
|
139
142
|
* @param {InteractionScope} scope
|
|
140
|
-
* @param {InteractionID} customId
|
|
143
|
+
* @param {InteractionID[]} customId
|
|
141
144
|
* @param {DiscordBaseInteraction} interaction
|
|
142
145
|
* @returns {Promise<any>}
|
|
143
146
|
* @throws {CommandError}
|
|
144
147
|
*/
|
|
145
|
-
static #call(type, scope, customId, interaction) {
|
|
148
|
+
static async #call(type, scope, customId, interaction) {
|
|
146
149
|
const interaction_command = InteractionContainer.get(type, scope, customId?.[0]);
|
|
147
150
|
if(!interaction_command)
|
|
148
|
-
return consola.error(`No interaction found for id ${customId}`);
|
|
151
|
+
return consola.error(`No interaction found for id ${customId.join('?')} with type ${type} and scope ${scope}`);
|
|
149
152
|
|
|
150
|
-
return interaction_command.interaction_command_function.apply(null, [interaction, ...customId.slice(1)]);
|
|
153
|
+
return await interaction_command.interaction_command_function.apply(null, [interaction, ...customId.slice(1)]);
|
|
151
154
|
}
|
|
152
155
|
}
|