forked from discord-php/DiscordPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.php
66 lines (50 loc) · 2.06 KB
/
browser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Example Bot with Discord-PHP CommandClient and ReactPHP HTTP Browser
*
* When a User says "@Bot discordstatus", the Bot will reply Discord service status
*
* @link https://reactphp.org/http/#browser
*
* Run this example bot from main directory using command:
* php examples/browser.php
*/
include __DIR__.'/../vendor/autoload.php';
// Import classes, install a LSP such as Intelephense to auto complete imports
use Discord\DiscordCommandClient;
use Discord\Parts\Channel\Message;
use Psr\Http\Message\ResponseInterface;
use React\Http\Browser;
use function React\Async\coroutine;
// Create a $discord BOT
$discord = new DiscordCommandClient([
'token' => '', // Put your Bot token here from https://discord.com/developers/applications/
]);
// Create a $browser with same loop as $discord
$browser = new Browser(null, $discord->getLoop());
$discord->registerCommand('discordstatus', function (Message $message, $params) use ($browser) {
coroutine(function (Message $message, $params) use ($browser) {
// Ignore messages from any Bots
if ($message->author->bot) return;
try {
// Make GET request to API of discordstatus.com
$response = yield $browser->get('https://discordstatus.com/api/v2/status.json');
assert($response instanceof ResponseInterface); // Check if request succeed
// Get response body
$result = (string) $response->getBody();
// Uncomment to debug result
//var_dump($result);
// Parse JSON
$discordstatus = json_decode($result);
// Send reply about the discord status
$message->reply('Discord status: ' . $discordstatus->status->description);
} catch (Exception $e) { // Request failed
// Uncomment to debug exceptions
//var_dump($e);
// Send reply about the discord status
$message->reply('Unable to acesss the Discord status API :(');
}
}, $message, $params);
});
// Start the Bot (must be at the bottom)
$discord->run();