-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_object.php
95 lines (83 loc) · 2.66 KB
/
stats_object.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
<?php
class Stats
{
/**
* Start time of bot.
*
* @var Carbon
*/
private $startTime;
/**
* Last reconnect time of bot.
*
* @var Carbon
*/
private $lastReconnect;
private $discord;
public function init(&$discord): void
{
$this->startTime = $this->lastReconnect = Carbon\Carbon::now();
$this->discord = $discord;
$this->discord->on('reconnect', function () {
$this->lastReconnect = Carbon\Carbon::now();
});
}
/**
* Returns the number of channels visible to the bot.
*
* @return int
*/
private function getChannelCount(): int
{
$channelCount = $this->discord->private_channels->count();
/* @var \Discord\Parts\Guild\Guild */
foreach ($this->discord->guilds as $guild) {
$channelCount += $guild->channels->count();
}
return $channelCount;
}
/**
* Returns the current commit of DiscordPHP.
*
* @return string
*/
private function getDiscordPHPVersion(): string
{
return str_replace(
"\n", ' ',
'cd ' . getcwd() . '/vendor/team-reflex/discord-php; git rev-parse --abbrev-ref HEAD; git log --oneline -1'
);
}
/**
* Returns the memory usage of the PHP process in a user-friendly format.
*
* @return string
*/
private function getMemoryUsageFriendly(): string
{
$size = memory_get_usage(true);
$unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$unit[$i];
}
public function handle($message): void
{
$embed = new \Discord\Parts\Embed\Embed($this->discord);
$embed
->setTitle('DiscordPHP')
->setDescription('This bot runs with DiscordPHP.')
->addFieldValues('PHP Version', phpversion())
->addFieldValues('DiscordPHP Version', $this->getDiscordPHPVersion())
->addFieldValues('Start time', $this->startTime->longRelativeToNowDiffForHumans(3))
->addFieldValues('Last reconnected', $this->lastReconnect->longRelativeToNowDiffForHumans(3))
->addFieldValues('Guild count', $this->discord->guilds->count())
->addFieldValues('Channel count', $this->getChannelCount())
->addFieldValues('User count', $this->discord->users->count())
->addFieldValues('Memory usage', $this->getMemoryUsageFriendly());
$message->channel->sendEmbed($embed);
}
public function getHelp(): string
{
return 'Provides statistics relating to the bots health.';
}
}
?>