File tree 11 files changed +229
-0
lines changed
02 - Registering Slash Commands
11 files changed +229
-0
lines changed Original file line number Diff line number Diff line change
1
+ TOKEN = YOUR_BOT_TOKEN
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " discord-bot" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " src/index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords" : [],
10
+ "author" : " " ,
11
+ "license" : " ISC" ,
12
+ "dependencies" : {
13
+ "discord.js" : " ^14.7.1" ,
14
+ "dotenv" : " ^16.0.3"
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( ) ;
2
+ const { Client, IntentsBitField } = require ( 'discord.js' ) ;
3
+
4
+ const client = new Client ( {
5
+ intents : [
6
+ IntentsBitField . Flags . Guilds ,
7
+ IntentsBitField . Flags . GuildMembers ,
8
+ IntentsBitField . Flags . GuildMessages ,
9
+ IntentsBitField . Flags . MessageContent ,
10
+ ] ,
11
+ } ) ;
12
+
13
+ client . on ( 'ready' , ( c ) => {
14
+ console . log ( `✅ ${ c . user . tag } is online.` ) ;
15
+ } ) ;
16
+
17
+ client . on ( 'messageCreate' , ( message ) => {
18
+ if ( message . author . bot ) {
19
+ return ;
20
+ }
21
+
22
+ if ( message . content === 'hello' ) {
23
+ message . reply ( 'hello' ) ;
24
+ }
25
+ } ) ;
26
+
27
+ client . login ( process . env . TOKEN ) ;
Original file line number Diff line number Diff line change
1
+ TOKEN = YOUR_BOT_TOKEN
2
+ CLIENT_ID = YOUR_BOT_ID
3
+ GUILD_ID = YOUR_SERVER_ID
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " discord-bot" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " src/index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords" : [],
10
+ "author" : " " ,
11
+ "license" : " ISC" ,
12
+ "dependencies" : {
13
+ "discord.js" : " ^14.7.1" ,
14
+ "dotenv" : " ^16.0.3"
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( ) ;
2
+ const { Client, IntentsBitField } = require ( 'discord.js' ) ;
3
+
4
+ const client = new Client ( {
5
+ intents : [
6
+ IntentsBitField . Flags . Guilds ,
7
+ IntentsBitField . Flags . GuildMembers ,
8
+ IntentsBitField . Flags . GuildMessages ,
9
+ IntentsBitField . Flags . MessageContent ,
10
+ ] ,
11
+ } ) ;
12
+
13
+ client . on ( 'ready' , ( c ) => {
14
+ console . log ( `✅ ${ c . user . tag } is online.` ) ;
15
+ } ) ;
16
+
17
+ client . on ( 'interactionCreate' , ( interaction ) => {
18
+ if ( ! interaction . isChatInputCommand ( ) ) return ;
19
+
20
+ if ( interaction . commandName === 'hey' ) {
21
+ return interaction . reply ( 'hey!' ) ;
22
+ }
23
+
24
+ if ( interaction . commandName === 'ping' ) {
25
+ return interaction . reply ( 'Pong!' ) ;
26
+ }
27
+ } ) ;
28
+
29
+ client . login ( process . env . TOKEN ) ;
Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( ) ;
2
+ const { REST , Routes } = require ( 'discord.js' ) ;
3
+
4
+ const commands = [
5
+ {
6
+ name : 'hey' ,
7
+ description : 'Replies with hey!' ,
8
+ } ,
9
+ {
10
+ name : 'ping' ,
11
+ description : 'Pong!' ,
12
+ } ,
13
+ ] ;
14
+
15
+ const rest = new REST ( { version : '10' } ) . setToken ( process . env . TOKEN ) ;
16
+
17
+ ( async ( ) => {
18
+ try {
19
+ console . log ( 'Registering slash commands...' ) ;
20
+
21
+ await rest . put (
22
+ Routes . applicationGuildCommands (
23
+ process . env . CLIENT_ID ,
24
+ process . env . GUILD_ID
25
+ ) ,
26
+ { body : commands }
27
+ ) ;
28
+
29
+ console . log ( 'Slash commands were registered successfully!' ) ;
30
+ } catch ( error ) {
31
+ console . log ( `There was an error: ${ error } ` ) ;
32
+ }
33
+ } ) ( ) ;
Original file line number Diff line number Diff line change
1
+ TOKEN = YOUR_BOT_TOKEN
2
+ CLIENT_ID = YOUR_BOT_ID
3
+ GUILD_ID = YOUR_SERVER_ID
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " discord-bot" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " src/index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords" : [],
10
+ "author" : " " ,
11
+ "license" : " ISC" ,
12
+ "dependencies" : {
13
+ "discord.js" : " ^14.7.1" ,
14
+ "dotenv" : " ^16.0.3"
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( ) ;
2
+ const { Client, IntentsBitField } = require ( 'discord.js' ) ;
3
+
4
+ const client = new Client ( {
5
+ intents : [
6
+ IntentsBitField . Flags . Guilds ,
7
+ IntentsBitField . Flags . GuildMembers ,
8
+ IntentsBitField . Flags . GuildMessages ,
9
+ IntentsBitField . Flags . MessageContent ,
10
+ ] ,
11
+ } ) ;
12
+
13
+ client . on ( 'ready' , ( c ) => {
14
+ console . log ( `✅ ${ c . user . tag } is online.` ) ;
15
+ } ) ;
16
+
17
+ client . on ( 'interactionCreate' , ( interaction ) => {
18
+ if ( ! interaction . isChatInputCommand ( ) ) return ;
19
+
20
+ if ( interaction . commandName === 'add' ) {
21
+ const num1 = interaction . options . get ( 'first-number' ) . value ;
22
+ const num2 = interaction . options . get ( 'second-number' ) . value ;
23
+
24
+ interaction . reply ( `The sum is ${ num1 + num2 } ` ) ;
25
+ }
26
+ } ) ;
27
+
28
+ client . login ( process . env . TOKEN ) ;
Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( ) ;
2
+ const { REST , Routes, ApplicationCommandOptionType } = require ( 'discord.js' ) ;
3
+
4
+ const commands = [
5
+ {
6
+ name : 'add' ,
7
+ description : 'Adds two numbers.' ,
8
+ options : [
9
+ {
10
+ name : 'first-number' ,
11
+ description : 'The first number.' ,
12
+ type : ApplicationCommandOptionType . String ,
13
+ choices : [
14
+ {
15
+ name : 'one' ,
16
+ value : '1' ,
17
+ } ,
18
+ {
19
+ name : 'two' ,
20
+ value : '2' ,
21
+ } ,
22
+ {
23
+ name : 'three' ,
24
+ value : '3' ,
25
+ } ,
26
+ ] ,
27
+ required : true ,
28
+ } ,
29
+ {
30
+ name : 'second-number' ,
31
+ description : 'The second number.' ,
32
+ type : ApplicationCommandOptionType . Number ,
33
+ required : true ,
34
+ } ,
35
+ ] ,
36
+ } ,
37
+ ] ;
38
+
39
+ const rest = new REST ( { version : '10' } ) . setToken ( process . env . TOKEN ) ;
40
+
41
+ ( async ( ) => {
42
+ try {
43
+ console . log ( 'Registering slash commands...' ) ;
44
+
45
+ await rest . put (
46
+ Routes . applicationGuildCommands (
47
+ process . env . CLIENT_ID ,
48
+ process . env . GUILD_ID
49
+ ) ,
50
+ { body : commands }
51
+ ) ;
52
+
53
+ console . log ( 'Slash commands were registered successfully!' ) ;
54
+ } catch ( error ) {
55
+ console . log ( `There was an error: ${ error } ` ) ;
56
+ }
57
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments