-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.php
126 lines (101 loc) · 3.16 KB
/
Commands.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
use TwitchBot\Utils;
/**
* Class Commands
*/
class Commands
{
use \TwitchBot\Module {
\TwitchBot\Module::__construct as private moduleConstructor;
}
private $commands;
private $delay;
private $lastCommands;
/**
* Commands constructor.
* @param array $infos
* @param \TwitchBot\IrcConnect $client
*/
public function __construct(array $infos, $client)
{
$this->moduleConstructor($infos, $client);
$this->commands = json_decode(file_get_contents(__DIR__ . '/commands.json'), true);
$this->delay = $this->getConfig('delay');
$this->lastCommands = [];
}
public function onConnect()
{
if ($this->getInfo('connect_message')) {
$this->getClient()->sendMessage('Commands Plugin Activated !');
}
}
/**
* @param \TwitchBot\Command $command
*/
public function onCommand($command)
{
if (key_exists($command->getCommand(), $this->getCommands())) {
$this->sendResponse($command);
$this->getClient()->sendToLog("Command " . $command . " was send");
}
}
/**
* @param null $key
* @return array|string
*/
public function getCommands($key = null)
{
if ($key) {
return $this->commands[$key];
} else {
return $this->commands;
}
}
/**
* @param $command
* @return null|string
*/
public function getRealCommand($command)
{
if (key_exists($command, $this->getCommands())) {
$commandSymbol = $this->getInfo('command_prefix');
$commandSymbolLength = strlen($commandSymbol);
if (substr($this->getCommands($command), 0, $commandSymbolLength) == $commandSymbol) {
return substr($this->getCommands($command), $commandSymbolLength);
} else {
return $command;
}
} else {
return null;
}
}
/**
* @param \TwitchBot\Command $command
*/
public function sendResponse(\TwitchBot\Command $command)
{
$cmd = $this->getRealCommand($command->getCommand());
if (isset($this->lastCommands[$cmd])) {
$time = $this->lastCommands[$cmd];
} else {
$time = time() - $this->delay;
}
$diff = time() - $time;
if ($diff >= $this->delay
// check if user has bypass roles
OR Utils::hasOneOfRoles($this->getConfig('no_delay_for'), $command->getMessage())) {
$userToPing = false;
if (count($command->getArgs()) == 2) {
$userToPing = $command->getArgs()[1];
}
if ($userToPing != false) {
$message = sprintf($this->getConfig('message_replytouser'), $userToPing, $this->getCommands($cmd));
$this->getClient()->sendMessage($message);
} else {
$message = sprintf($this->getConfig('message_reply'), $this->getCommands($cmd));
$this->getClient()->sendMessage($message);
}
$this->lastCommands[$command->getCommand()] = time();
}
}
}