Skip to content

Commit 52bf915

Browse files
committed
log messages to console
1 parent 7b1301e commit 52bf915

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
"revolt/event-loop": "^1.0",
66
"monolog/monolog": "^3.7",
77
"vlucas/phpdotenv": "^5.6"
8+
},
9+
"scripts": {
10+
"dev": [
11+
"Composer\\Config::disableProcessTimeout",
12+
"php index.php"
13+
]
814
}
915
}

disco.php

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

55
use Amp\Websocket\Client\WebsocketConnection;
66
use Amp\Websocket\Client\WebsocketHandshake;
7+
use Monolog\Formatter\LineFormatter;
78
use Monolog\Handler\StreamHandler;
89
use Monolog\Logger;
910
use Revolt\EventLoop;
@@ -15,7 +16,9 @@
1516

1617
function disco(int $gatewayIntents, string $botToken, string $botName) {
1718
$logger = new Logger('disco');
18-
$logger->pushHandler(new StreamHandler('php://stdout'));
19+
$handler = new StreamHandler('php://stdout');
20+
$handler->setFormatter(new LineFormatter(null, null, false, true));
21+
$logger->pushHandler($handler);
1922
$handshake = new WebsocketHandshake(GATEWAY::JSON->value);
2023

2124
try {
@@ -27,9 +30,12 @@ function disco(int $gatewayIntents, string $botToken, string $botName) {
2730
$parsed = json_decode($payload);
2831

2932
// debug payload
30-
print_r($parsed);
33+
// print_r($parsed);
3134

3235
switch($parsed->op) {
36+
case OPCODE::zero->value:
37+
if($parsed->t === 'MESSAGE_CREATE') handleMessage($logger, $parsed);
38+
break;
3339
case OPCODE::ten->value:
3440
handleHeartbeat($conn, $logger, $parsed);
3541
handleIdentify($conn, $logger, $gatewayIntents, $botToken, $botName);
@@ -47,6 +53,12 @@ function disco(int $gatewayIntents, string $botToken, string $botName) {
4753
EventLoop::run();
4854
}
4955

56+
function handleMessage(Logger $logger, object $parsed) {
57+
$sender = $parsed->d->author->global_name;
58+
$content = $parsed->d->content;
59+
$logger->notice($sender . ': ' . $content);
60+
}
61+
5062
function handleHeartbeat(WebsocketConnection $conn, Logger $logger, object $parsed): void {
5163
$interval = $parsed->d->heartbeat_interval;
5264
$jitter = mt_rand() / mt_getrandmax();

bot.php renamed to index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$dotenv->load();
1010

1111
disco(
12-
gatewayIntents: 513,
12+
gatewayIntents: 2130793,
1313
botToken: $_ENV['DISCORD_TOKEN'],
1414
botName: 'Hailstone'
1515
);

0 commit comments

Comments
 (0)