1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Commands; |
4
|
|
|
|
5
|
|
|
trait MessageCommand { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* \addtogroup Commands |
9
|
|
|
* \brief What commands are |
10
|
|
|
* @{ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** \brief Store the command triggered on message. */ |
14
|
|
|
protected $_message_commands; |
15
|
|
|
|
16
|
|
|
/** \brief Does the bot has message commands? Set by initBot. */ |
17
|
|
|
protected $_message_commands_set; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* \brief Add a function that will be executed everytime a message contain the selected command |
21
|
|
|
* \details Use this syntax: |
22
|
|
|
* |
23
|
|
|
* addMessageCommand("start", function($bot, $message) { |
24
|
|
|
* $bot->sendMessage("Hi"); }); |
25
|
|
|
* @param $command The command that will trigger this function (without slash). Eg: "start", "help", "about" |
26
|
|
|
* @param $script The function that will be triggered by a command. Must take an object(the bot) and an array(the message received). |
27
|
|
|
*/ |
28
|
|
|
public function addMessageCommand(string $command, callable $script) { |
29
|
|
|
|
30
|
|
|
$this->_message_commands[] = [ |
31
|
|
|
'script' => $script, |
32
|
|
|
'command' => '/' . $command, |
33
|
|
|
'length' => strlen($command) + 1, |
34
|
|
|
'regex_active' => false |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* \brief Add a function that will be executed everytime a message contain a command that match the regex |
41
|
|
|
* \details Use this syntax: |
42
|
|
|
* |
43
|
|
|
* addMessageCommandRegex("number\d", function($bot, $message, $result) { |
44
|
|
|
* $bot->sendMessage("You sent me a number"); }); |
45
|
|
|
* @param $regex_rule Regex rule that will called for evalueting the command received. |
46
|
|
|
* @param $script The function that will be triggered by a command. Must take an object(the bot) and an array(the message received). |
47
|
|
|
*/ |
48
|
|
|
public function addMessageCommandRegex(string $regex_rule, callable $script) { |
49
|
|
|
|
50
|
|
|
$this->_message_commands[] = [ |
51
|
|
|
'script' => $script, |
52
|
|
|
'regex_active' => true, |
53
|
|
|
'regex_rule' => $regex_rule |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|