1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Commands; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \addtogroup Commands |
25
|
|
|
* @{ |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** \class MultiCharacterCommand |
29
|
|
|
*/ |
30
|
|
|
class MultiCharacterCommand extends BasicCommand |
31
|
|
|
{ |
32
|
|
|
/** @} */ |
33
|
|
|
|
34
|
|
|
public static $type = 'message'; |
35
|
|
|
|
36
|
|
|
public static $object_class = 'PhpBotFramework\Entities\Message'; |
37
|
|
|
|
38
|
|
|
public static $priority = 1; |
39
|
|
|
|
40
|
|
|
private $command; |
41
|
|
|
|
42
|
|
|
private $length; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* \brief Add a function that will be executed everytime a message contain the selected command |
46
|
|
|
* \details Use this syntax to create a command: |
47
|
|
|
* |
48
|
|
|
* $help_command = new PhpBotFramework\Commands\MultiCharacterCommand("help", |
49
|
|
|
* function ($bot, $message) { |
50
|
|
|
* $bot->sendMessage("This is a help message."); |
51
|
|
|
* }, '!', '.', '/' |
52
|
|
|
* ); |
53
|
|
|
* |
54
|
|
|
* Then you can add it to the bot's commands using <code>addCommand</code> method: |
55
|
|
|
* |
56
|
|
|
* $bot->addCommand($help_command); |
57
|
|
|
* |
58
|
|
|
* @param string $command The command that will trigger this function (e.g. start) |
59
|
|
|
* @param callable $script The function that will be triggered by a command. |
60
|
|
|
* Must take an object(the bot) and an array(the message received). |
61
|
|
|
*/ |
62
|
|
|
public function __construct(string $command, callable $script, string ...$chars) |
63
|
|
|
{ |
64
|
|
|
$chars_count = count($chars); |
65
|
|
|
if ( $chars_count === 0) |
66
|
|
|
{ |
67
|
|
|
throw new BotException("No character given for matching the command"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($chars_count === 1) |
71
|
|
|
{ |
72
|
|
|
// Build a regex using the only character |
73
|
|
|
$this->regex_rule = $chars[0] . $command; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
// Build regex including all characters |
76
|
|
|
// Eg: (!|.)command |
|
|
|
|
77
|
|
|
else |
78
|
|
|
{ |
79
|
|
|
$this->regex_rule = '(' . $chars[0]; |
80
|
|
|
foreach($chars as $char) |
81
|
|
|
{ |
82
|
|
|
$this->regex_rule .= '|' . $char; |
83
|
|
|
} |
84
|
|
|
$this->regex_rule .= ')' . $command; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->script = $script; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @internal |
92
|
|
|
* \brief Process a message checking if it trigger any MessageCommand. |
93
|
|
|
* @param string $message Message to process. |
94
|
|
|
* @return bool True if the message trigger any command. |
95
|
|
|
*/ |
96
|
|
|
public function checkCommand(array $message) : bool |
97
|
|
|
{ |
98
|
|
|
// Check the regex |
99
|
|
|
if (preg_match("/{$this->regex_rule}/", $message['text'])) { |
100
|
|
|
// Return |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: