-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeeperbot.php
60 lines (45 loc) · 1.65 KB
/
keeperbot.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
<?php
include __DIR__.'/vendor/autoload.php';
use Yani\KeeperBot\KeeperBot;
use Discord\Discord;
use Discord\WebSockets\Event;
use Discord\WebSockets\Intents;
use Discord\Parts\Channel\Message;
use Symfony\Component\Dotenv\Dotenv;
// Load .env
$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/.env');
if(!isset($_ENV['DISCORD_BOT_TOKEN']) || empty($_ENV['DISCORD_BOT_TOKEN'])){
die('Invalid Discord bot token');
}
// Setup discord bot
$discord = new Discord([
'token' => $_ENV['DISCORD_BOT_TOKEN'],
'intents' => Intents::getDefaultIntents() | Intents::MESSAGE_CONTENT
// | Intents::MESSAGE_CONTENT, // Note: MESSAGE_CONTENT is privileged, see https://dis.gd/mcfaq
]);
// Create our own bot instance
$keeperbot = new KeeperBot();
// Handle discord ready event (after connected and ready)
$discord->on('ready', function (Discord $discord) use ($keeperbot) {
echo "Bot is ready!", PHP_EOL;
// Handle messages
$discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) use ($keeperbot) {
// Show message in log
echo "{$message->author->username}: {$message->content}", PHP_EOL;
// Handle the message with our custom bot
$keeperbot->handleMessage($discord, $message);
});
});
// Get list of events that trigger the BackgroundTasks
$reflectionClass = new ReflectionClass(Event::class);
$events = $reflectionClass->getConstants();
$events[] = 'heartbeat-ack';
// Run background task handler
foreach($events as $event) {
$discord->on($event, function ($data) use ($keeperbot) {
$keeperbot->getBackgroundTaskHandler()->runTasks();
});
}
// Start discord bot
$discord->run();