1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @property message |
28
|
|
|
*/ |
29
|
|
|
class help |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
public $config; |
32
|
|
|
public $discord; |
33
|
|
|
public $logger; |
34
|
|
|
private $excludeChannel; |
35
|
|
|
private $message; |
36
|
|
|
private $triggers; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $config |
40
|
|
|
* @param $discord |
41
|
|
|
* @param $logger |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function init($config, $discord, $logger) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->discord = $discord; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
$this->excludeChannel = $this->config['bot']['restrictedChannels']; |
49
|
|
|
$this->triggers[] = $this->config['bot']['trigger'] . 'help'; |
50
|
|
|
$this->triggers[] = $this->config['bot']['trigger'] . 'Help'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $msgData |
55
|
|
|
* @param $message |
56
|
|
|
* @return null |
57
|
|
|
*/ |
58
|
|
|
public function onMessage($msgData, $message) |
59
|
|
|
{ |
60
|
|
|
$channelID = (int) $msgData['message']['channelID']; |
61
|
|
|
|
62
|
|
|
if (in_array($channelID, $this->excludeChannel, true)) |
63
|
|
|
{ |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->message = $message; |
68
|
|
|
|
69
|
|
|
$message = $msgData['message']['message']; |
70
|
|
|
$user = $msgData['message']['from']; |
71
|
|
|
|
72
|
|
|
$data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
73
|
|
|
if (isset($data['trigger'])) { |
74
|
|
|
global $plugins; // Need to have the plugins that are loaded available, yes it's ugly, whatever, better than shitting up the rest of the code :P |
|
|
|
|
75
|
|
|
$messageString = $data['messageString']; |
76
|
|
|
|
77
|
|
|
if (!$messageString) { |
78
|
|
|
// Show all modules available |
79
|
|
|
$commands = array(); |
80
|
|
|
foreach ($plugins as $plugin) { |
81
|
|
|
$info = $plugin->information(); |
82
|
|
|
if (!empty($info['name'])) { |
83
|
|
|
$commands[] = $info['name']; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->message->reply('Here is a list of plugins available: **' . implode('** | **', $commands) . "** If you'd like help with a specific plugin simply use the command !help <PluginName>"); |
88
|
|
|
$this->logger->addInfo("Help: Sending help info to {$user}"); |
89
|
|
|
} else { |
90
|
|
|
foreach ($plugins as $plugin) { |
91
|
|
|
if (strtolower($messageString) === strtolower($plugin->information()['name'])) { |
92
|
|
|
$this->message->reply($plugin->information()['information']); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function information() |
103
|
|
|
{ |
104
|
|
|
return array( |
105
|
|
|
'name' => 'help', |
106
|
|
|
'trigger' => $this->triggers, |
107
|
|
|
'information' => 'Shows help for a plugin, or all the plugins available. Example: **!help pc**' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.