@@ -17,6 +17,12 @@ $ npm install necord discord.js
17
17
To utilize Necord in your project, import the ` NecordModule ` and configure it with the necessary options.
18
18
19
19
``` typescript
20
+ @@filename (app .module )
21
+ import { Module } from ' @nestjs/common' ;
22
+ import { NecordModule } from ' necord' ;
23
+ import { IntentsBitField } from ' discord.js' ;
24
+ import { AppService } from ' ./app.service' ;
25
+
20
26
@Module ({
21
27
imports: [
22
28
NecordModule .forRoot ({
@@ -35,6 +41,11 @@ export class AppModule {}
35
41
With this setup, you can inject the ` AppService ` into your providers to easily register commands, events, and more.
36
42
37
43
``` typescript
44
+ @@filename (app .service )
45
+ import { Injectable , Logger } from ' @nestjs/common' ;
46
+ import { Context , On , Once , ContextOf } from ' necord' ;
47
+ import { Client } from ' discord.js' ;
48
+
38
49
@Injectable ()
39
50
export class AppService {
40
51
private readonly logger = new Logger (AppService .name );
@@ -62,6 +73,10 @@ You may have noticed the `@Context` decorator in the examples above. This decora
62
73
Here's how to create a simple command handler for messages using the ` @TextCommand ` decorator.
63
74
64
75
``` typescript
76
+ @@filename (app .commands )
77
+ import { Injectable } from ' @nestjs/common' ;
78
+ import { Context , TextCommand , TextCommandContext , Arguments } from ' necord' ;
79
+
65
80
@Injectable ()
66
81
export class AppCommands {
67
82
@TextCommand ({
@@ -81,7 +96,7 @@ export class AppCommands {
81
96
82
97
Application commands provide a native way for users to interact with your app within the Discord client. There are three types of application commands that can be accessed through different interfaces: chat input, message context menu (accessed by right-clicking a message), and user context menu (accessed by right-clicking a user).
83
98
84
- <figure ><img src =" https://i.imgur.com/4EmG8G8.png " /></figure >
99
+ <figure ><img class = " illustrative-image " src =" https://i.imgur.com/4EmG8G8.png " /></figure >
85
100
86
101
#### Slash commands
87
102
@@ -90,6 +105,10 @@ Slash commands are an excellent way to engage with users in a structured manner.
90
105
To define a slash command using Necord, you can use the ` SlashCommand ` decorator.
91
106
92
107
``` typescript
108
+ @@filename (app .commands )
109
+ import { Injectable } from ' @nestjs/common' ;
110
+ import { Context , SlashCommand , SlashCommandContext } from ' necord' ;
111
+
93
112
@Injectable ()
94
113
export class AppCommands {
95
114
@SlashCommand ({
@@ -109,6 +128,9 @@ export class AppCommands {
109
128
You can define parameters for your slash commands using option decorators. Let's create a ` TextDto ` class for this purpose:
110
129
111
130
``` typescript
131
+ @@filename (text .dto )
132
+ import { StringOption } from ' necord' ;
133
+
112
134
export class TextDto {
113
135
@StringOption ({
114
136
name: ' text' ,
@@ -122,6 +144,11 @@ export class TextDto {
122
144
You can then use this DTO in the ` AppCommands ` class:
123
145
124
146
``` typescript
147
+ @@filename (app .commands )
148
+ import { Injectable } from ' @nestjs/common' ;
149
+ import { Context , SlashCommand , Options , SlashCommandContext } from ' necord' ;
150
+ import { TextDto } from ' ./length.dto' ;
151
+
125
152
@Injectable ()
126
153
export class AppCommands {
127
154
@SlashCommand ({
@@ -146,6 +173,11 @@ For a complete list of built-in option decorators, check out [this documentation
146
173
To implement autocomplete functionality for your slash commands, you'll need to create an interceptor. This interceptor will handle requests as users type in the autocomplete field.
147
174
148
175
``` typescript
176
+ @@filename (cats - autocomplete .interceptor )
177
+ import { Injectable } from ' @nestjs/common' ;
178
+ import { AutocompleteInteraction } from ' discord.js' ;
179
+ import { AutocompleteInterceptor } from ' necord' ;
180
+
149
181
@Injectable ()
150
182
class CatsAutocompleteInterceptor extends AutocompleteInterceptor {
151
183
public transformOptions(interaction : AutocompleteInteraction ) {
@@ -168,6 +200,9 @@ class CatsAutocompleteInterceptor extends AutocompleteInterceptor {
168
200
You will also need to mark your options class with ` autocomplete: true ` :
169
201
170
202
``` typescript
203
+ @@filename (cat .dto )
204
+ import { StringOption } from ' necord' ;
205
+
171
206
export class CatDto {
172
207
@StringOption ({
173
208
name: ' cat' ,
@@ -182,6 +217,12 @@ export class CatDto {
182
217
Finally, apply the interceptor to your slash command:
183
218
184
219
``` typescript
220
+ @@filename (cats .commands )
221
+ import { Injectable , UseInterceptors } from ' @nestjs/common' ;
222
+ import { Context , SlashCommand , Options , SlashCommandContext } from ' necord' ;
223
+ import { CatDto } from ' /cat.dto' ;
224
+ import { CatsAutocompleteInterceptor } from ' ./cats-autocomplete.interceptor' ;
225
+
185
226
@Injectable ()
186
227
export class CatsCommands {
187
228
@UseInterceptors (CatsAutocompleteInterceptor )
@@ -205,6 +246,11 @@ export class CatsCommands {
205
246
User commands appear on the context menu that appears when right-clicking (or tapping) on users. These commands provide quick actions that target users directly.
206
247
207
248
``` typescript
249
+ @@filename (app .commands )
250
+ import { Injectable } from ' @nestjs/common' ;
251
+ import { Context , UserCommand , UserCommandContext , TargetUser } from ' necord' ;
252
+ import { User } from ' discord.js' ;
253
+
208
254
@Injectable ()
209
255
export class AppCommands {
210
256
@UserCommand ({ name: ' Get avatar' })
@@ -228,6 +274,11 @@ export class AppCommands {
228
274
Message commands show up in the context menu when right-clicking on messages, allowing for quick actions relevant to those messages.
229
275
230
276
``` typescript
277
+ @@filename (app .commands )
278
+ import { Injectable } from ' @nestjs/common' ;
279
+ import { Context , MessageCommand , MessageCommandContext , TargetMessage } from ' necord' ;
280
+ import { Message } from ' discord.js' ;
281
+
231
282
@Injectable ()
232
283
export class AppCommands {
233
284
@MessageCommand ({ name: ' Copy Message' })
@@ -245,6 +296,10 @@ export class AppCommands {
245
296
[ Buttons] ( https://discord.com/developers/docs/interactions/message-components#buttons ) are interactive elements that can be included in messages. When clicked, they send an [ interaction] ( https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object ) to your application.
246
297
247
298
``` typescript
299
+ @@filename (app .components )
300
+ import { Injectable } from ' @nestjs/common' ;
301
+ import { Context , Button , ButtonContext } from ' necord' ;
302
+
248
303
@Injectable ()
249
304
export class AppComponents {
250
305
@Button (' BUTTON' )
@@ -259,12 +314,16 @@ export class AppComponents {
259
314
[ Select menus] ( https://discord.com/developers/docs/interactions/message-components#select-menus ) are another type of interactive component that appears on messages. They provide a dropdown-like UI for users to select options.
260
315
261
316
``` typescript
317
+ @@filename (app .components )
318
+ import { Injectable } from ' @nestjs/common' ;
319
+ import { Context , StringSelect , StringSelectContext , SelectedStrings } from ' necord' ;
320
+
262
321
@Injectable ()
263
322
export class AppComponents {
264
323
@StringSelect (' SELECT_MENU' )
265
324
public onSelectMenu(
266
325
@Context () [interaction ]: StringSelectContext ,
267
- @Values () values : string [],
326
+ @SelectedStrings () values : string [],
268
327
) {
269
328
return interaction .reply ({ content: ` You selected: ${values .join (' , ' )} ` });
270
329
}
0 commit comments