@@ -16,61 +16,74 @@ function registerCommand(string $name, string $desc, callable $callback, int $ty
16
16
];
17
17
}
18
18
19
- function setGlobalCommands (Logger $ logger , string $ appId , string $ token ) {
19
+ function getCommands (): array {
20
+ $ token = $ _ENV ['DISCORD_TOKEN ' ];
21
+ $ appId = $ _ENV ['DISCORD_APP_ID ' ];
22
+
23
+ $ headers = ['Authorization: Bot ' . $ token ];
24
+ $ endpoint = GATEWAY ::API ->value . '/applications/ ' . $ appId . '/commands ' ;
25
+
26
+ $ response = endpointRequest ($ headers , $ endpoint , 'GET ' );
27
+ $ commands = $ response ->result ;
28
+ $ commands = json_decode ($ commands );
29
+
30
+ $ list = [];
31
+ $ count = 0 ;
32
+ foreach ($ commands as $ cmd ) {
33
+ $ list [$ count ]['name ' ] = $ cmd ->name ;
34
+ $ list [$ count ]['desc ' ] = $ cmd ->description ;
35
+ $ count ++;
36
+ }
37
+ return $ list ;
38
+ }
39
+
40
+ function deleteCommand (string $ commandId ) {
41
+ $ token = $ _ENV ['DISCORD_TOKEN ' ];
42
+ $ appId = $ _ENV ['DISCORD_APP_ID ' ];
43
+
44
+ $ headers = ['Authorization: Bot ' . $ token ];
45
+ $ endpoint = GATEWAY ::API ->value . '/applications/ ' . $ appId . '/commands/ ' . $ commandId ;
46
+
47
+ $ response = endpointRequest ($ headers , $ endpoint , 'DELETE ' );
48
+ print_r ($ response );
49
+ }
50
+
51
+ function setGlobalCommands (string $ appId , string $ token ) {
20
52
$ headers = [
21
53
'Authorization: Bot ' . $ token ,
22
54
'Content-Type: application/json '
23
55
];
24
- $ endpoint = ' https://discord.com/api/v10/ applications/ ' . $ appId .'/commands ' ;
56
+ $ endpoint = GATEWAY :: API -> value . ' / applications/ ' .$ appId .'/commands ' ;
25
57
26
58
global $ commands ;
27
59
foreach ($ commands as $ cmd ) {
28
60
unset($ cmd ['callback ' ]);
29
- $ cmd = (object )$ cmd ;
30
-
31
- $ ch = curl_init ($ endpoint );
32
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
33
- curl_setopt ($ ch , CURLOPT_POST , 1 );
34
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ cmd ));
35
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
36
- curl_exec ($ ch );
37
-
38
- if (curl_errno ($ ch )) {
39
- print_r (curl_error ($ ch ));
40
- $ logger ->notice ('Failed to set application command: ' . $ cmd ->name );
41
- }
42
- else {
43
- $ logger ->notice ('Set application command: ' . $ cmd ->name );
61
+
62
+ $ response = endpointPost ($ headers , $ endpoint , $ cmd );
63
+ if ($ response ->error ) {
64
+ print_r ($ response ->error );
65
+ } else {
66
+ print_r ($ response ->result );
44
67
}
45
68
}
46
69
}
47
70
71
+ /**
72
+ * @desc Handle command response
73
+ */
48
74
function handleCommand (string $ userName , string $ commandName , string $ interactionId , string $ interactionToken ) {
49
75
global $ commands ;
50
76
51
77
$ content = call_user_func ($ commands [$ commandName ]['callback ' ], $ userName );
52
- $ endpoint = ' https://discord.com/api/v10 /interactions/ ' .$ interactionId .'/ ' .$ interactionToken .'/callback ' ;
78
+ $ endpoint = GATEWAY :: API -> value . ' /interactions/ ' .$ interactionId .'/ ' .$ interactionToken .'/callback ' ;
53
79
$ headers = ['Content-Type: application/json ' ];
54
-
55
80
$ payload = [
56
81
'type ' => 4 ,
57
82
'data ' => ['content ' => $ content ],
58
83
];
59
84
60
- $ ch = curl_init ($ endpoint );
61
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
62
- curl_setopt ($ ch , CURLOPT_POST , 1 );
63
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ payload ));
64
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
65
-
66
- $ response = curl_exec ($ ch );
67
-
68
- if (curl_errno ($ ch )) {
69
- print_r (curl_error ($ ch ));
70
- }
71
- else {
72
- print_r ($ response );
73
- }
85
+ $ response = endpointPost ($ headers , $ endpoint , $ payload );
86
+ print_r ($ response );
74
87
}
75
88
76
89
registerCommand (
@@ -83,23 +96,11 @@ function handleCommand(string $userName, string $commandName, string $interactio
83
96
);
84
97
85
98
registerCommand (
86
- name: 'coinflip ' ,
87
- desc: 'flip a coin! ' ,
88
- type: 1 ,
89
- callback: function (): string {
90
- $ result = mt_rand (0 , 1 );
91
- return $ result ? 'heads ' : 'tails ' ;
92
- }
93
- );
94
-
95
- registerCommand (
96
- name: 'aura ' ,
97
- desc: 'how much aura do you have? ' ,
99
+ name: 'list ' ,
100
+ desc: 'list commands ' ,
98
101
type: 1 ,
99
- callback: function (): string {
100
- do { $ result = mt_rand (-100000 , 100000 ); } while ($ result === 0 );
101
- return $ result > 0
102
- ? "Plus " . $ result . " Aura! "
103
- : "Minus " . abs ($ result ) . " Aura " ;
102
+ callback: function () {
103
+ $ commands = getCommands ();
104
+ return json_encode ($ commands );
104
105
}
105
106
);
0 commit comments