discord-bot-shared 0.3.2 → 0.3.3
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 +137 -0
- package/dist/guildCache.d.ts +4 -4
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -1 +1,138 @@
|
|
|
1
1
|
# discord-bot-shared
|
|
2
|
+
|
|
3
|
+
A module that makes creating discord.js bots a bit easier.
|
|
4
|
+
|
|
5
|
+
## Installation/Usage
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install discord-bot-shared
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- This module expects a `BOT_TOKEN` and `CLIENT_ID` environment variable to login.
|
|
12
|
+
- By default, commands are registered globally. To register commands to a specific guild, you can optionally provide `GUILD_ID`.
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
// index.ts
|
|
16
|
+
import login from 'discord-bot-shared'
|
|
17
|
+
import { ClientOptions, GatewayIntentBits as Intents, Partials } from 'discord.js'
|
|
18
|
+
|
|
19
|
+
const botIntents: ClientOptions = {
|
|
20
|
+
intents: [Intents.Guilds],
|
|
21
|
+
partials: [Partials.Reaction],
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const bot = await login(botIntents, import.meta.url)
|
|
25
|
+
|
|
26
|
+
export default bot
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**You must have a directory names `events` and `commands` next to your `index.ts` (or wherever you are importing `login()` from).**
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
.
|
|
33
|
+
└── src/
|
|
34
|
+
├── events/
|
|
35
|
+
│ └── someEvent.ts
|
|
36
|
+
├── commands/
|
|
37
|
+
│ └── someCommand.ts
|
|
38
|
+
└── index.ts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Events
|
|
42
|
+
|
|
43
|
+
This module registers your bot's events by `import`ing each file under your `events` directory. Events are registered as a side-effect of the import.
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
// guildMemberAdd.ts
|
|
47
|
+
import bot from '../index.js'
|
|
48
|
+
|
|
49
|
+
bot.on('guildMemberAdd', (member) => {
|
|
50
|
+
// do stuff
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Commands
|
|
55
|
+
|
|
56
|
+
Your commands are registered by `import`ing the default export of each command file.
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
// myCommand.ts
|
|
60
|
+
import { Command } from 'discord-bot-shared'
|
|
61
|
+
import { SlashCommandBuilder } from 'discord.js'
|
|
62
|
+
|
|
63
|
+
const myCommand: Command = {
|
|
64
|
+
command: new SlashCommandBuilder()
|
|
65
|
+
.setName('my-command')
|
|
66
|
+
.setDescription('This command does stuff.') as SlashCommandBuilder,
|
|
67
|
+
run: (interaction) => {
|
|
68
|
+
// do stuff
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export default myCommand
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Your `run` function is passed the command interaction that initiated the command.
|
|
76
|
+
|
|
77
|
+
### Exports
|
|
78
|
+
|
|
79
|
+
The following functions and interfaces/types are exported by this module:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
async function login(
|
|
83
|
+
botIntents: ClientOptions,
|
|
84
|
+
projectMetaURL: string,
|
|
85
|
+
interactionCheck?: InteractionCheck,
|
|
86
|
+
): Promise<Client>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- `botIntents` is a discord.js `ClientOptions` object.
|
|
90
|
+
- `projectMetaURL` should always be passed `import.meta.url`. This is used to find and register your events and commands.
|
|
91
|
+
- Optionally provide an `interactionCheck` function that returns a boolean. This function is called right before trying to run a command. The command will only run if `interactionCheck` returns true.
|
|
92
|
+
- The `InteractionCheck` type is also exported by this module.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
Returns an object that contains the guild and the following freshly fetched guild collections: `channels, emojis, members, roles`.
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
async function getGuildCache(baseGuild?: Guild)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Optionally provide a Guild to retrieve the caches from. Otherwise, the first guild on the bot is used.
|
|
103
|
+
- For example, a common pattern I use is something like this:
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
const { members } = await getGuildCache()
|
|
107
|
+
// do stuff with members
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
This interface defines the structure of your bot's commands. See above for an example of a command.
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
interface Command {
|
|
116
|
+
requiredRoles?: string[]
|
|
117
|
+
command: SlashCommandBuilder
|
|
118
|
+
run: (interaction: ChatInputCommandInteraction) => void | Promise<void>
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- Optionally provide `requiredRoles` string array. If the member initiating the command has _any_ of the roles provided in this array, they will be allowed to run the command.
|
|
123
|
+
- **You may have to type cast your command `as SlashCommandBuilder` due to the way discord.js' `SlashCommandBuilder` works.**
|
|
124
|
+
- `run` is the final function that will be called to run your command.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
These are type guards that you will likely need to use.
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
function isTextChannel(channel: BaseChannel | APIPartialChannel): channel is TextChannel
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
function isCategoryChannel(channel: BaseChannel): channel is CategoryChannel
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- Almost every channel type in discord.js extends `BaseChannel`, so you should be able to pass in whatever channel you need to here.
|
package/dist/guildCache.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { Client, Guild } from 'discord.js';
|
|
|
2
2
|
declare function setBot(botClient: Client): void;
|
|
3
3
|
declare function getGuildCache(baseGuild?: Guild): Promise<{
|
|
4
4
|
guild: Guild;
|
|
5
|
-
channels: import("
|
|
6
|
-
emojis: import("
|
|
7
|
-
members: import("
|
|
8
|
-
roles: import("
|
|
5
|
+
channels: import("discord.js").Collection<string, import("discord.js").NonThreadGuildBasedChannel>;
|
|
6
|
+
emojis: import("discord.js").Collection<string, import("discord.js").GuildEmoji>;
|
|
7
|
+
members: import("discord.js").Collection<string, import("discord.js").GuildMember>;
|
|
8
|
+
roles: import("discord.js").Collection<string, import("discord.js").Role>;
|
|
9
9
|
} | undefined>;
|
|
10
10
|
export default getGuildCache;
|
|
11
11
|
export { setBot };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "discord-bot-shared",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Modules for creating discord bots.",
|
|
6
6
|
"repository": "github:adamhl8/discord-bot-shared",
|
|
@@ -18,26 +18,26 @@
|
|
|
18
18
|
"lint": "eslint -f pretty --fix ."
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@discordjs/rest": "^1.0.
|
|
22
|
-
"discord-api-types": "^0.
|
|
23
|
-
"discord.js": "^14.
|
|
21
|
+
"@discordjs/rest": "^1.0.1",
|
|
22
|
+
"discord-api-types": "^0.37.1",
|
|
23
|
+
"discord.js": "^14.1.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/eslint": "^8.4.5",
|
|
27
|
-
"@types/node": "^18.6.
|
|
28
|
-
"@types/prettier": "^2.
|
|
29
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
30
|
-
"@typescript-eslint/parser": "^5.
|
|
31
|
-
"eslint": "^8.
|
|
27
|
+
"@types/node": "^18.6.5",
|
|
28
|
+
"@types/prettier": "^2.7.0",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^5.33.0",
|
|
30
|
+
"@typescript-eslint/parser": "^5.33.0",
|
|
31
|
+
"eslint": "^8.21.0",
|
|
32
32
|
"eslint-config-prettier": "^8.5.0",
|
|
33
33
|
"eslint-formatter-pretty": "^4.1.0",
|
|
34
34
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
35
|
-
"eslint-plugin-sonarjs": "^0.
|
|
35
|
+
"eslint-plugin-sonarjs": "^0.15.0",
|
|
36
36
|
"eslint-plugin-unicorn": "^43.0.2",
|
|
37
37
|
"prettier": "^2.7.1",
|
|
38
|
-
"prettier-plugin-organize-imports": "^3.0.
|
|
39
|
-
"prettier-plugin-pkg": "^0.16.
|
|
40
|
-
"prettier-plugin-sh": "^0.12.
|
|
38
|
+
"prettier-plugin-organize-imports": "^3.0.3",
|
|
39
|
+
"prettier-plugin-pkg": "^0.16.1",
|
|
40
|
+
"prettier-plugin-sh": "^0.12.8",
|
|
41
41
|
"typescript": "^4.7.4"
|
|
42
42
|
}
|
|
43
43
|
}
|