Skip to content

Commit 421d647

Browse files
committed
added some endpoint function for api calls, added get/delete commands
1 parent 15e1cf1 commit 421d647

File tree

5 files changed

+99
-52
lines changed

5 files changed

+99
-52
lines changed

constants.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
enum GATEWAY: string {
66
case JSON = 'wss://gateway.discord.gg/?v=10&encoding=json';
7+
case API = 'https://discord.com/api/v10';
78
}
89

910
enum OPCODE: int {

disco.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ function disco(Logger $logger, int $gatewayIntents, string $botToken, string $bo
3333
case OPCODE::DISPATCH->value:
3434
$logger->notice('Event triggered: ' . $parsed->event);
3535
if($parsed->event === 'MESSAGE_CREATE') logMessage($logger, $parsed->data);
36-
if($parsed->event === 'INTERACTION_CREATE') handleInteraction($parsed->data);
36+
if($parsed->event === 'INTERACTION_CREATE') {
37+
// print_r($parsed->data);
38+
handleInteraction($parsed->data);
39+
}
3740
break;
3841

3942
case OPCODE::HELLO->value:

index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
require __DIR__ . '/vendor/autoload.php';
1010
require __DIR__ . '/disco.php';
11+
require __DIR__ . '/util/functions.php';
1112
require __DIR__ . '/util/command.php';
1213

1314
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
@@ -22,7 +23,6 @@
2223
$appId = $_ENV['DISCORD_APP_ID'];
2324

2425
setGlobalCommands(
25-
logger: $logger,
2626
appId: $appId,
2727
token: $token
2828
);

util/command.php

+51-50
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,74 @@ function registerCommand(string $name, string $desc, callable $callback, int $ty
1616
];
1717
}
1818

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) {
2052
$headers = [
2153
'Authorization: Bot ' . $token,
2254
'Content-Type: application/json'
2355
];
24-
$endpoint = 'https://discord.com/api/v10/applications/'. $appId .'/commands';
56+
$endpoint = GATEWAY::API->value.'/applications/'.$appId.'/commands';
2557

2658
global $commands;
2759
foreach($commands as $cmd) {
2860
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);
4467
}
4568
}
4669
}
4770

71+
/**
72+
* @desc Handle command response
73+
*/
4874
function handleCommand(string $userName, string $commandName, string $interactionId, string $interactionToken) {
4975
global $commands;
5076

5177
$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';
5379
$headers = ['Content-Type: application/json'];
54-
5580
$payload = [
5681
'type' => 4,
5782
'data' => ['content' => $content],
5883
];
5984

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);
7487
}
7588

7689
registerCommand(
@@ -83,23 +96,11 @@ function handleCommand(string $userName, string $commandName, string $interactio
8396
);
8497

8598
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',
98101
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);
104105
}
105106
);

util/functions.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @desc Perform a custom request with cURL
7+
*/
8+
function endpointRequest(array $headers, string $endpoint, string $request): object {
9+
$ch = curl_init();
10+
11+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
12+
curl_setopt($ch, CURLOPT_URL, $endpoint);
13+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request);
14+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
15+
16+
$result = curl_exec($ch);
17+
$error = curl_errno($ch);
18+
curl_close($ch);
19+
20+
return (object)[
21+
'result' => $result,
22+
'error' => $error
23+
];
24+
}
25+
26+
function endpointPost(array $headers, string $endpoint, array $payload) {
27+
$ch = curl_init($endpoint);
28+
29+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
30+
curl_setopt($ch, CURLOPT_POST, 1);
31+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
32+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
33+
34+
$result = curl_exec($ch);
35+
$error = curl_errno($ch);
36+
curl_close($ch);
37+
38+
return (object)[
39+
'result' => $result,
40+
'error' => $error
41+
];
42+
}

0 commit comments

Comments
 (0)