-
-
Notifications
You must be signed in to change notification settings - Fork 714
/
Copy pathPingPlugin.php
85 lines (75 loc) · 2.32 KB
/
PingPlugin.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
<?php declare(strict_types=1);
namespace MadelinePlugin\Danogentili;
use danog\MadelineProto\EventHandler\Attributes\Cron;
use danog\MadelineProto\EventHandler\Filter\FilterText;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\PluginEventHandler;
/**
* Plugin event handler class.
*
* All properties returned by __sleep are automatically stored in the database.
*/
class PingPlugin extends PluginEventHandler
{
private int $pingCount = 0;
private string $pongText = 'pong';
/**
* You can set a custom pong text from the outside of the plugin:.
*
* ```
* if (!file_exists('madeline.php')) {
* copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
* }
* include 'madeline.php';
*
* $a = new API('bot.madeline');
* $plugin = $a->getPlugin(PingPlugin::class);
*
* $plugin->setPongText('UwU');
* ```
*
* This will automatically connect to the running instance of the plugin and call the specified method.
*/
public function setPongText(string $pong): void
{
$this->pongText = $pong;
}
/**
* Returns a list of names for properties that will be automatically saved to the session database (MySQL/postgres/redis if configured, the session file otherwise).
*/
public function __sleep(): array
{
return ['pingCount', 'pongText'];
}
/**
* Initialization logic.
*/
public function onStart(): void
{
$this->logger("The bot was started!");
$this->logger($this->getFullInfo('MadelineProto'));
$this->sendMessageToAdmins("The bot was started!");
}
/**
* Plugins may be enabled or disabled at startup by returning true or false from this function.
*/
public function isPluginEnabled(): bool
{
return true;
}
/**
* This cron function will be executed forever, every 60 seconds.
*/
#[Cron(period: 60.0)]
public function cron1(): void
{
$this->sendMessageToAdmins("The ping plugin is online, total pings so far: ".$this->pingCount);
}
#[FilterText('ping')]
public function pingCommand(Incoming&Message $message): void
{
$message->reply($this->pongText);
$this->pingCount++;
}
}