Completed
Push — master ( a95f7d...eaa6fe )
by Danilo
02:55
created

MessageCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 2
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
/**
22
 * \addtogroup Commands
23
 * @{
24
 */
25
26
/** \class MessageCommand
27
 */
28
class MessageCommand extends BasicCommand
29
{
30
    /** @} */
31
32
    public static $type = 'message';
33
34
    public static $object_class = 'PhpBotFramework\Entities\Message';
35
36
    public static $priority = 1;
37
38
    private $command;
39
40
    private $length;
41
42
    /**
43
     * \brief Add a function that will be executed everytime a message contain the selected command
44
     * \details Use this syntax:
45
     *
46
     *     addMessageCommand("start", function($bot, $message) {
47
     *         $bot->sendMessage("Hi"); });
48
     * @param string $command The command that will trigger this function (without slash). Eg: "start", "help", "about"
49
     * @param callable $script The function that will be triggered by a command.
50
     * Must take an object(the bot) and an array(the message received).
51
     */
52
    public function __construct(string $command, callable $script)
53
    {
54
        $this->command = "/$command";
55
        $this->script = $script;
56
        $this->length = strlen($command) + 1;
57
    }
58
59
    /**
60
     * @internal
61
     * \brief Process a message checking if it trigger any MessageCommand.
62
     * @param string $message Message to process.
63
     * @return bool True if the message trigger any command.
64
     */
65
    public function checkCommand(array $message) : bool
66
    {
67
        // If the message contains a bot command at the start
68
        $message_is_command = (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') ? true : false;
69
70
        // If we found a valid command (check first lenght, then use strpos)
71
        if ($message_is_command && $this->length == $message['entities'][0]['length'] && mb_strpos($this->command, $message['text'], $message['entities'][0]['offset']) !== false) {
72
                    // Return
73
                    return true;
74
        }
75
76
        return false;
77
    }
78
}
79