-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.php
115 lines (95 loc) · 3.21 KB
/
run.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* This file is a part of the D.PHP project.
*
* Copyright (c) 2020-present David Cole <[email protected]>
*
* This source file is subject to the GNU Affero General Public License v3.0 or later
* that is bundled with this source code in the LICENSE.md file.
*/
ini_set('memory_limit', '-1');
/*
* This file is a part of the D.PHP project.
*
* Copyright (c) 2020-present David Cole <[email protected]>
*
* This source file is subject to the GNU Affero General Public License v3.0 or later
* that is bundled with this source code in the LICENSE.md file.
*/
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\Parts\Embed\Embed;
use Discord\WebSockets\Intents;
use Dotenv\Dotenv;
use DPHP\Commands\Events;
use DPHP\Commands\Reflect;
use DPHP\Commands\Stats;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use React\EventLoop\Factory;
use React\Sh\Shell;
use React\Sh\StdioHandler;
include __DIR__.'/vendor/autoload.php';
// load environment file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['TOKEN', 'LOG_FILE']);
$dotenv->required('LOGGER_LEVEL')->allowedValues(array_keys(Logger::getLevels()));
$logger = new Logger('D.PHP');
$loop = Factory::create();
$discord = new Discord([
'token' => $_ENV['TOKEN'],
'loop' => $loop,
'logger' => $logger,
'loadAllMembers' => true,
'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS | Intents::GUILD_PRESENCES,
]);
$shell = new Shell($loop);
if (strtolower($_ENV['LOG_FILE']) == 'stdout') {
$logger->pushHandler(new StdioHandler($shell->getStdio()));
} else {
$logger->pushHandler(new StreamHandler($_ENV['LOG_FILE'], Logger::getLevels()[$_ENV['LOGGER_LEVEL']]));
}
/**
* Generates help for the bot.
*
* @param Discord $discord
* @param array $commands
* @return Embed
*/
function generateHelpCommand(Discord $discord, array $commands): Embed
{
$embed = new Embed($discord);
$embed->setTitle('DiscordPHP');
foreach ($commands as $name => $command) {
$embed->addFieldValues("@{$discord->username} ".$name, $command->getHelp());
}
return $embed;
}
$commands = [
'reflect' => new Reflect($discord),
'info' => new Stats($discord),
'events' => new Events($discord),
];
$discord->on('ready', function (Discord $discord) use ($commands, $shell) {
$discord->on('message', function (Message $message, Discord $discord) use ($commands) {
// check if message starts with mention
if (strpos($message->content, '<@'.$discord->id.'>') === 0 || strpos($message->content, '<@!'.$discord->id.'>') === 0) {
$args = explode(' ', $message->content);
array_shift($args);
if (count($args) > 0) {
$command = array_shift($args);
if (isset($commands[$command])) {
$commands[$command]->handle($message, $args);
} else {
$embed = generateHelpCommand($discord, $commands);
$message->channel->sendEmbed($embed);
}
} else {
$commands['info']->handle($message, $args);
}
}
});
$shell->setScope(get_defined_vars());
});
$loop->run();