discord-bot-shared 0.14.0 → 0.14.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/LICENSE +1 -1
- package/README.md +3 -2
- package/dist/bot.js +4 -4
- package/dist/bot.js.map +1 -0
- package/dist/command-manager.js +33 -28
- package/dist/command-manager.js.map +1 -0
- package/dist/event-manager.js +8 -5
- package/dist/event-manager.js.map +1 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -0
- package/dist/types/bot.d.ts +4 -4
- package/dist/types/command-manager.d.ts +5 -5
- package/dist/types/event-manager.d.ts +4 -4
- package/dist/types/index.d.ts +1 -1
- package/dist/types/util.d.ts +31 -1
- package/dist/util.js +35 -4
- package/dist/util.js.map +1 -0
- package/package.json +35 -28
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A small package that makes creating [discord.js](https://github.com/discordjs/di
|
|
|
13
13
|
- [Unregistering commands](#unregistering-commands)
|
|
14
14
|
- [Global Command Hook](#global-command-hook)
|
|
15
15
|
- [Events](#events)
|
|
16
|
-
- [Listening for
|
|
16
|
+
- [Listening for events](#listening-for-events)
|
|
17
17
|
- [Adding and registering events](#adding-and-registering-events)
|
|
18
18
|
- [Error Handling](#error-handling)
|
|
19
19
|
- [Commands](#commands-1)
|
|
@@ -52,6 +52,7 @@ In **`index.ts`:**
|
|
|
52
52
|
```ts
|
|
53
53
|
import Bot from "discord-bot-shared"
|
|
54
54
|
import { ClientOptions, GatewayIntentBits } from "discord.js"
|
|
55
|
+
|
|
55
56
|
import ping from "./ping"
|
|
56
57
|
|
|
57
58
|
// Get applicationId and token from environment variables
|
|
@@ -173,7 +174,7 @@ It's intended that you throw an error in the case you don't want the command to
|
|
|
173
174
|
|
|
174
175
|
## Events
|
|
175
176
|
|
|
176
|
-
### Listening for
|
|
177
|
+
### Listening for events
|
|
177
178
|
|
|
178
179
|
You can create an `Event` object that will be added to the bot later.
|
|
179
180
|
|
package/dist/bot.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Client, Events, REST } from "discord.js";
|
|
2
|
-
import CommandManager from "./command-manager.js";
|
|
3
|
-
import EventManager from "./event-manager.js";
|
|
2
|
+
import { CommandManager } from "./command-manager.js";
|
|
3
|
+
import { EventManager } from "./event-manager.js";
|
|
4
4
|
class Bot {
|
|
5
5
|
#discord;
|
|
6
6
|
commands;
|
|
@@ -24,5 +24,5 @@ class Bot {
|
|
|
24
24
|
await this.#discord.client.login(this.#discord.token);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
export
|
|
28
|
-
//# sourceMappingURL=
|
|
27
|
+
export { Bot };
|
|
28
|
+
//# sourceMappingURL=bot.js.map
|
package/dist/bot.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bot.js","sourceRoot":"","sources":["../src/bot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAsB,MAAM,YAAY,CAAA;AAErE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAejD,MAAM,GAAG;IACE,QAAQ,CAAgB;IAEjB,QAAQ,CAAgB;IACxB,MAAM,CAAc;IAEpC,YAAmB,OAAmB;QACpC,IAAI,CAAC,QAAQ,GAAG;YACd,aAAa,EAAE,OAAO,CAAC,aAAa;YACpC,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,MAAM,EAAE,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC;YACzC,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC;SACzC,CAAA;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACjD,IAAI,CAAC,MAAM,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC/C,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,EAAE;YACjD,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;QAEF,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAA;QACvB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAA;QACrB,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IACvD,CAAC;CACF;AAED,OAAO,EAAE,GAAG,EAAE,CAAA"}
|
package/dist/command-manager.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Collection, Events, Routes, } from "discord.js";
|
|
2
|
-
import {
|
|
1
|
+
import { Collection, Events, MessageFlags, Routes, } from "discord.js";
|
|
2
|
+
import { throwUserError, UserError } from "./util.js";
|
|
3
3
|
class CommandManager {
|
|
4
4
|
#commands = new Collection();
|
|
5
5
|
#globalCommandHook;
|
|
@@ -20,17 +20,13 @@ class CommandManager {
|
|
|
20
20
|
const payload = this.#commands.map((c) => c.command);
|
|
21
21
|
const route = Routes.applicationCommands(this.#discord.applicationId);
|
|
22
22
|
await this.#discord.rest.put(route, { body: payload });
|
|
23
|
-
console.log(`Registered ${this.#commands.size} (/) commands.`);
|
|
23
|
+
console.log(`Registered ${this.#commands.size.toString()} (/) commands.`);
|
|
24
24
|
}
|
|
25
25
|
async unregisterGuildCommands() {
|
|
26
|
-
if (this.#discord.client.readyAt)
|
|
27
|
-
await this._unregisterGuildCommands()
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.#discord.client.once(Events.ClientReady, async () => {
|
|
31
|
-
await this._unregisterGuildCommands().catch(console.error);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
26
|
+
if (this.#discord.client.readyAt)
|
|
27
|
+
await this._unregisterGuildCommands();
|
|
28
|
+
else
|
|
29
|
+
this.#discord.client.once(Events.ClientReady, () => void this._unregisterGuildCommands());
|
|
34
30
|
}
|
|
35
31
|
async _unregisterGuildCommands() {
|
|
36
32
|
let guilds;
|
|
@@ -41,11 +37,16 @@ class CommandManager {
|
|
|
41
37
|
console.error("Unable to unregister guild commands. Failed to fetch guilds.");
|
|
42
38
|
throw error;
|
|
43
39
|
}
|
|
40
|
+
const unregisterPromises = [];
|
|
44
41
|
for (const guild of guilds.values()) {
|
|
45
42
|
const route = Routes.applicationGuildCommands(this.#discord.applicationId, guild.id);
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
const unregisterGuildCommands = async () => {
|
|
44
|
+
await this.#discord.rest.put(route, { body: [] });
|
|
45
|
+
console.log(`Unregistered commands from guild: ${guild.name}`);
|
|
46
|
+
};
|
|
47
|
+
unregisterPromises.push(unregisterGuildCommands());
|
|
48
48
|
}
|
|
49
|
+
await Promise.all(unregisterPromises);
|
|
49
50
|
}
|
|
50
51
|
async unregisterApplicationCommands() {
|
|
51
52
|
const route = Routes.applicationCommands(this.#discord.applicationId);
|
|
@@ -53,7 +54,7 @@ class CommandManager {
|
|
|
53
54
|
console.log("Unregistered application commands.");
|
|
54
55
|
}
|
|
55
56
|
_listen() {
|
|
56
|
-
|
|
57
|
+
const listen = async (interaction) => {
|
|
57
58
|
if (!interaction.isChatInputCommand())
|
|
58
59
|
return;
|
|
59
60
|
if (!interaction.guildId)
|
|
@@ -61,16 +62,16 @@ class CommandManager {
|
|
|
61
62
|
if (!interaction.inCachedGuild())
|
|
62
63
|
await interaction.client.guilds.fetch(interaction.guildId).catch(console.error);
|
|
63
64
|
if (!interaction.inCachedGuild()) {
|
|
64
|
-
|
|
65
|
+
CommandManager.interactionReply(interaction, "Guild is not cached. Try again.");
|
|
65
66
|
return;
|
|
66
67
|
}
|
|
67
68
|
const command = this.#commands.get(interaction.commandName);
|
|
68
69
|
if (!command) {
|
|
69
|
-
|
|
70
|
+
CommandManager.interactionReply(interaction, `Failed to get command with name: ${interaction.commandName}`);
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
72
|
-
if (!(await
|
|
73
|
-
|
|
73
|
+
if (!(await CommandManager.checkRoles(command, interaction))) {
|
|
74
|
+
CommandManager.interactionReply(interaction, "You do not have one of the required roles to run this command.");
|
|
74
75
|
return;
|
|
75
76
|
}
|
|
76
77
|
try {
|
|
@@ -80,12 +81,13 @@ class CommandManager {
|
|
|
80
81
|
await command.run(interaction);
|
|
81
82
|
}
|
|
82
83
|
catch (error) {
|
|
83
|
-
|
|
84
|
+
CommandManager.interactionReply(interaction, error);
|
|
84
85
|
}
|
|
85
|
-
}
|
|
86
|
+
};
|
|
87
|
+
this.#discord.client.on(Events.InteractionCreate, (interaction) => void listen(interaction));
|
|
86
88
|
console.log("Listening for commands.");
|
|
87
89
|
}
|
|
88
|
-
async checkRoles(command, interaction) {
|
|
90
|
+
static async checkRoles(command, interaction) {
|
|
89
91
|
if (!command.requiredRoles)
|
|
90
92
|
return true;
|
|
91
93
|
if (command.requiredRoles.length > 0) {
|
|
@@ -96,8 +98,8 @@ class CommandManager {
|
|
|
96
98
|
}
|
|
97
99
|
return false;
|
|
98
100
|
}
|
|
99
|
-
interactionReply(interaction, error) {
|
|
100
|
-
let errorMessage
|
|
101
|
+
static interactionReply(interaction, error) {
|
|
102
|
+
let errorMessage;
|
|
101
103
|
if (error instanceof UserError)
|
|
102
104
|
errorMessage = error.message;
|
|
103
105
|
else if (error instanceof Error && error.stack)
|
|
@@ -105,10 +107,13 @@ class CommandManager {
|
|
|
105
107
|
else
|
|
106
108
|
errorMessage = String(error);
|
|
107
109
|
const message = `There was an error while running this command.\n\`\`\`${errorMessage}\`\`\``;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
const handleInteractionReply = async () => {
|
|
111
|
+
await (interaction.deferred
|
|
112
|
+
? interaction.editReply(message).catch(console.error)
|
|
113
|
+
: interaction.reply({ content: message, flags: MessageFlags.Ephemeral }).catch(console.error));
|
|
114
|
+
};
|
|
115
|
+
void handleInteractionReply();
|
|
111
116
|
}
|
|
112
117
|
}
|
|
113
|
-
export
|
|
114
|
-
//# sourceMappingURL=
|
|
118
|
+
export { CommandManager };
|
|
119
|
+
//# sourceMappingURL=command-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-manager.js","sourceRoot":"","sources":["../src/command-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,MAAM,EACN,YAAY,EACZ,MAAM,GAIP,MAAM,YAAY,CAAA;AAGnB,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAUrD,MAAM,cAAc;IACT,SAAS,GAAG,IAAI,UAAU,EAAmB,CAAA;IACtD,kBAAkB,CAAc;IACvB,QAAQ,CAAgB;IAEjC,YAAmB,OAAuB;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;IACzB,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IACnD,CAAC;IAEM,oBAAoB,CAAC,WAAwB;QAClD,IAAI,CAAC,kBAAkB,GAAG,WAAW,CAAA;IACvC,CAAC;IAEM,KAAK,CAAC,QAAQ;QACnB,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;QACpD,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAA;QACrE,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,C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C,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAC1E,CAAA;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAEO,MAAM,CAAC,gBAAgB,CAAC,WAAwC,EAAE,KAAc;QACtF,IAAI,YAAoB,CAAA;QACxB,IAAI,KAAK,YAAY,SAAS;YAAE,YAAY,GAAG,KAAK,CAAC,OAAO,CAAA;aACvD,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,KAAK;YAAE,YAAY,GAAG,KAAK,CAAC,KAAK,CAAA;;YACrE,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;QAEjC,MAAM,OAAO,GAAG,yDAAyD,YAAY,QAAQ,CAAA;QAC7F,MAAM,sBAAsB,GAAG,KAAK,IAAI,EAAE;YACxC,MAAM,CAAC,WAAW,CAAC,QAAQ;gBACzB,CAAC,CAAC,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACrD,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,YAAY,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;QAClG,CAAC,CAAA;QACD,KAAK,sBAAsB,EAAE,CAAA;IAC/B,CAAC;CACF;AAED,OAAO,EAAE,cAAc,EAAE,CAAA"}
|
package/dist/event-manager.js
CHANGED
|
@@ -12,17 +12,20 @@ class EventManager {
|
|
|
12
12
|
}
|
|
13
13
|
_listen() {
|
|
14
14
|
for (const event of this.#events) {
|
|
15
|
-
|
|
15
|
+
const listen = async (...args) => {
|
|
16
16
|
try {
|
|
17
|
+
// TS Error: Expression produces a union type that is too complex to represent.
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
17
19
|
await event.handler(this.#discord.client, ...args);
|
|
18
20
|
}
|
|
19
21
|
catch (error) {
|
|
20
22
|
console.error(error);
|
|
21
23
|
}
|
|
22
|
-
}
|
|
24
|
+
};
|
|
25
|
+
this.#discord.client.on(event.event, (...args) => void listen(...args));
|
|
23
26
|
}
|
|
24
|
-
console.log(`Listening for (${this.#events.length}) events.`);
|
|
27
|
+
console.log(`Listening for (${this.#events.length.toString()}) events.`);
|
|
25
28
|
}
|
|
26
29
|
}
|
|
27
|
-
export
|
|
28
|
-
//# sourceMappingURL=
|
|
30
|
+
export { EventManager };
|
|
31
|
+
//# sourceMappingURL=event-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-manager.js","sourceRoot":"","sources":["../src/event-manager.ts"],"names":[],"mappings":"AAoBA,MAAM,YAAY;IACP,OAAO,GAAkB,EAAE,CAAA;IAC3B,QAAQ,CAAgB;IAEjC,YAAmB,OAAuB;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;IACzB,CAAC;IAED;;OAEG;IACI,GAAG,CAAwB,KAAqB;QACrD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;IAEM,OAAO;QACZ,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjC,MAAM,MAAM,GAAG,KAAK,EAAE,GAAG,IAAsC,EAAE,EAAE;gBACjE,IAAI,CAAC;oBACH,+EAA+E;oBAC/E,uEAAuE;oBACvE,MAAO,KAAK,CAAC,OAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAA;gBACtE,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;gBACtB,CAAC;YACH,CAAC,CAAA;YAED,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,KAAK,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QACzE,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAA;IAC1E,CAAC;CACF;AAMD,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { Bot } from "./bot.js";
|
|
2
2
|
export { getChannel, throwError, throwUserError } from "./util.js";
|
|
3
|
-
//# sourceMappingURL=
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAG9B,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA"}
|
package/dist/types/bot.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Client,
|
|
2
|
-
import CommandManager from "./command-manager.js";
|
|
3
|
-
import EventManager from "./event-manager.js";
|
|
1
|
+
import { Client, REST, type ClientOptions } from "discord.js";
|
|
2
|
+
import { CommandManager } from "./command-manager.js";
|
|
3
|
+
import { EventManager } from "./event-manager.js";
|
|
4
4
|
interface BotOptions {
|
|
5
5
|
applicationId: string;
|
|
6
6
|
token: string;
|
|
@@ -19,5 +19,5 @@ declare class Bot {
|
|
|
19
19
|
constructor(options: BotOptions);
|
|
20
20
|
login(): Promise<void>;
|
|
21
21
|
}
|
|
22
|
-
export
|
|
22
|
+
export { Bot };
|
|
23
23
|
export type { BotOptions, DiscordContext };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ChatInputCommandInteraction, RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord.js";
|
|
2
|
-
import { DiscordContext } from "./bot.js";
|
|
1
|
+
import { type ChatInputCommandInteraction, type RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord.js";
|
|
2
|
+
import type { DiscordContext } from "./bot.js";
|
|
3
3
|
interface Command {
|
|
4
4
|
requiredRoles?: string[];
|
|
5
5
|
command: RESTPostAPIChatInputApplicationCommandsJSONBody;
|
|
@@ -16,8 +16,8 @@ declare class CommandManager {
|
|
|
16
16
|
private _unregisterGuildCommands;
|
|
17
17
|
unregisterApplicationCommands(): Promise<void>;
|
|
18
18
|
_listen(): void;
|
|
19
|
-
private checkRoles;
|
|
20
|
-
private interactionReply;
|
|
19
|
+
private static checkRoles;
|
|
20
|
+
private static interactionReply;
|
|
21
21
|
}
|
|
22
|
-
export
|
|
22
|
+
export { CommandManager };
|
|
23
23
|
export type { Command, CommandHook };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Client, ClientEvents
|
|
2
|
-
import { DiscordContext } from "./bot.js";
|
|
3
|
-
type ValidEvents =
|
|
1
|
+
import type { Client, ClientEvents } from "discord.js";
|
|
2
|
+
import type { DiscordContext } from "./bot.js";
|
|
3
|
+
type ValidEvents = keyof ClientEvents;
|
|
4
4
|
type EventHandler<E extends ValidEvents = ValidEvents> = (client: Client, ...args: ClientEvents[E]) => void | Promise<void>;
|
|
5
5
|
type EventHandlerMap = {
|
|
6
6
|
[E in ValidEvents]: EventHandler<E>;
|
|
@@ -18,5 +18,5 @@ declare class EventManager {
|
|
|
18
18
|
type Event = {
|
|
19
19
|
[E in ValidEvents]: SingleEvent<E>;
|
|
20
20
|
}[ValidEvents];
|
|
21
|
-
export
|
|
21
|
+
export { EventManager };
|
|
22
22
|
export type { Event };
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CategoryChannel, ChannelType, ForumChannel, Guild, NewsChannel, StageChannel, TextChannel, VoiceChannel } from "discord.js";
|
|
1
|
+
import type { CategoryChannel, ChannelType, ForumChannel, Guild, NewsChannel, StageChannel, TextChannel, VoiceChannel } from "discord.js";
|
|
2
2
|
interface ChannelTypeToChannelMap {
|
|
3
3
|
[ChannelType.GuildCategory]: CategoryChannel;
|
|
4
4
|
[ChannelType.GuildAnnouncement]: NewsChannel;
|
|
@@ -7,8 +7,38 @@ interface ChannelTypeToChannelMap {
|
|
|
7
7
|
[ChannelType.GuildVoice]: VoiceChannel;
|
|
8
8
|
[ChannelType.GuildForum]: ForumChannel;
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns the guild channel of the given name/ID and type, otherwise throws.
|
|
12
|
+
*
|
|
13
|
+
* @param guild The Guild to fetch the channel from
|
|
14
|
+
* @param channelNameOrId The name or ID of the channel to fetch
|
|
15
|
+
* @param channelType The type of channel to fetch
|
|
16
|
+
* @returns The channel of the given name/ID and type
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { getChannel } from "discord-bot-shared"
|
|
20
|
+
* import { ChannelType } from "discord.js"
|
|
21
|
+
*
|
|
22
|
+
* // guild is of type Guild from discord.js
|
|
23
|
+
* const someTextChannel = await getChannel(guild, "some-text-channel", ChannelType.GuildText)
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Getting a properly typed channel with discord.js can be a bit of a pain, so this is an alternative.
|
|
27
|
+
*/
|
|
10
28
|
declare function getChannel<T extends keyof ChannelTypeToChannelMap>(guild: Guild, channelNameOrId: string, channelType: T): Promise<ChannelTypeToChannelMap[T]>;
|
|
29
|
+
/**
|
|
30
|
+
* Throws an error with the given message.
|
|
31
|
+
*
|
|
32
|
+
* @param message The message to throw
|
|
33
|
+
* @throws Error
|
|
34
|
+
*/
|
|
11
35
|
declare function throwError(message: string): never;
|
|
36
|
+
/**
|
|
37
|
+
* Throws a UserError with the given message.
|
|
38
|
+
*
|
|
39
|
+
* @param message The message to throw
|
|
40
|
+
* @throws UserError
|
|
41
|
+
*/
|
|
12
42
|
declare function throwUserError(message: string): never;
|
|
13
43
|
declare class UserError extends Error {
|
|
14
44
|
constructor(message: string);
|
package/dist/util.js
CHANGED
|
@@ -1,27 +1,58 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Returns the guild channel of the given name/ID and type, otherwise throws.
|
|
3
|
+
*
|
|
4
|
+
* @param guild The Guild to fetch the channel from
|
|
5
|
+
* @param channelNameOrId The name or ID of the channel to fetch
|
|
6
|
+
* @param channelType The type of channel to fetch
|
|
7
|
+
* @returns The channel of the given name/ID and type
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { getChannel } from "discord-bot-shared"
|
|
11
|
+
* import { ChannelType } from "discord.js"
|
|
12
|
+
*
|
|
13
|
+
* // guild is of type Guild from discord.js
|
|
14
|
+
* const someTextChannel = await getChannel(guild, "some-text-channel", ChannelType.GuildText)
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Getting a properly typed channel with discord.js can be a bit of a pain, so this is an alternative.
|
|
18
|
+
*/
|
|
2
19
|
async function getChannel(guild, channelNameOrId, channelType) {
|
|
3
20
|
const channels = await guild.channels.fetch();
|
|
4
21
|
let channel;
|
|
5
|
-
channel = channels.find((
|
|
22
|
+
channel = channels.find((chan) => (chan ? chan.name === channelNameOrId : false));
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
6
24
|
if (channel && channel.type === channelType)
|
|
7
25
|
return channel;
|
|
8
26
|
channel = channels.get(channelNameOrId);
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
9
28
|
if (channel && channel.type === channelType)
|
|
10
29
|
return channel;
|
|
11
30
|
throwError(`Failed to get channel: ${channelNameOrId}`);
|
|
12
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Throws an error with the given message.
|
|
34
|
+
*
|
|
35
|
+
* @param message The message to throw
|
|
36
|
+
* @throws Error
|
|
37
|
+
*/
|
|
13
38
|
function throwError(message) {
|
|
14
39
|
throw new Error(message);
|
|
15
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Throws a UserError with the given message.
|
|
43
|
+
*
|
|
44
|
+
* @param message The message to throw
|
|
45
|
+
* @throws UserError
|
|
46
|
+
*/
|
|
16
47
|
function throwUserError(message) {
|
|
17
48
|
throw new UserError(message);
|
|
18
49
|
}
|
|
19
50
|
class UserError extends Error {
|
|
20
51
|
constructor(message) {
|
|
21
52
|
super(message);
|
|
22
|
-
this.name =
|
|
53
|
+
this.name = "UserError";
|
|
23
54
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
24
55
|
}
|
|
25
56
|
}
|
|
26
57
|
export { UserError, getChannel, throwError, throwUserError };
|
|
27
|
-
//# sourceMappingURL=
|
|
58
|
+
//# sourceMappingURL=util.js.map
|
package/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;GAiBG;AACH,KAAK,UAAU,UAAU,CACvB,KAAY,EACZ,eAAuB,EACvB,WAAc;IAEd,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAA;IAE7C,IAAI,OAAsD,CAAA;IAC1D,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA;IACjF,uEAAuE;IACvE,IAAI,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW;QAAE,OAAO,OAAqC,CAAA;IAEzF,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;IACvC,uEAAuE;IACvE,IAAI,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW;QAAE,OAAO,OAAqC,CAAA;IAEzF,UAAU,CAAC,0BAA0B,eAAe,EAAE,CAAC,CAAA;AACzD,CAAC;AAED;;;;;GAKG;AACH,SAAS,UAAU,CAAC,OAAe;IACjC,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAA;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,OAAe;IACrC,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC,CAAA;AAC9B,CAAC;AAED,MAAM,SAAU,SAAQ,KAAK;IAC3B,YAAmB,OAAe;QAChC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,WAAW,CAAA;QAEvB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACnD,CAAC;CACF;AAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,40 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "discord-bot-shared",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Modules for creating discord bots.",
|
|
6
|
-
"repository":
|
|
7
|
-
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/adamhl8/discord-bot-shared"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/adamhl8/discord-bot-shared",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/adamhl8/discord-bot-shared/issues"
|
|
13
|
+
},
|
|
14
|
+
"author": {
|
|
15
|
+
"email": "adamhl@pm.me",
|
|
16
|
+
"name": "Adam Langbert",
|
|
17
|
+
"url": "https://github.com/adamhl8"
|
|
18
|
+
},
|
|
8
19
|
"license": "MIT",
|
|
9
|
-
"
|
|
10
|
-
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"types": "./dist/types/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
11
26
|
"files": [
|
|
12
|
-
"dist/"
|
|
13
|
-
"README.md",
|
|
14
|
-
"LICENSE"
|
|
27
|
+
"dist/"
|
|
15
28
|
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"bundle": "bun lint && rm -rf ./dist && tsc -p ./tsconfig.build.json",
|
|
31
|
+
"lint": "tsc --noEmit && prettier --write . && eslint .",
|
|
32
|
+
"prepare": "find .githooks -type f -exec ln -srf {} .git/hooks/ \\;",
|
|
33
|
+
"prepublish": "bun bundle"
|
|
34
|
+
},
|
|
16
35
|
"peerDependencies": {
|
|
17
36
|
"discord.js": "^14.0.0"
|
|
18
37
|
},
|
|
19
38
|
"devDependencies": {
|
|
20
|
-
"@types/
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"eslint
|
|
27
|
-
"eslint-plugin-unicorn": "^48.0.1",
|
|
28
|
-
"prettier": "^3.0.3",
|
|
29
|
-
"prettier-plugin-organize-imports": "^3.2.3",
|
|
30
|
-
"prettier-plugin-pkg": "^0.18.0",
|
|
31
|
-
"prettier-plugin-sh": "^0.13.1",
|
|
32
|
-
"typescript": "^5.2.2"
|
|
33
|
-
},
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "rm -rf dist && tsc",
|
|
36
|
-
"format": "prettier --write .",
|
|
37
|
-
"lint": "tsc --noEmit && eslint ./src/",
|
|
38
|
-
"prepublish": "pnpm build"
|
|
39
|
+
"@types/bun": "^1.2.17",
|
|
40
|
+
"discord.js": "^14.21.0",
|
|
41
|
+
"eslint": "^9.29.0",
|
|
42
|
+
"eslint-config-builder": "^0.21.2",
|
|
43
|
+
"prettier": "^3.6.1",
|
|
44
|
+
"typescript": "^5.8.3",
|
|
45
|
+
"typescript-eslint": "^8.35.0"
|
|
39
46
|
}
|
|
40
|
-
}
|
|
47
|
+
}
|