1
|
|
|
<?php |
2
|
|
|
namespace Sovereign\Plugins\onMessage; |
3
|
|
|
|
4
|
|
|
use Discord\Discord; |
5
|
|
|
use Discord\Parts\Channel\Message; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Sovereign\Lib\Config; |
|
|
|
|
8
|
|
|
use Sovereign\Lib\cURL; |
9
|
|
|
use Sovereign\Lib\Db; |
10
|
|
|
use Sovereign\Lib\Permissions; |
11
|
|
|
use Sovereign\Lib\ServerConfig; |
12
|
|
|
use Sovereign\Lib\Settings; |
13
|
|
|
use Sovereign\Lib\Users; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class cleverBotMessage |
17
|
|
|
* @package Sovereign |
18
|
|
|
*/ |
19
|
|
|
class cleverBotMessage extends \Threaded implements \Collectable |
20
|
|
|
{ |
21
|
|
|
/** @var Message $message */ |
22
|
|
|
private $message; |
23
|
|
|
/** @var Discord $discord */ |
24
|
|
|
private $discord; |
25
|
|
|
/** |
26
|
|
|
* @var Logger |
27
|
|
|
*/ |
28
|
|
|
private $log; |
29
|
|
|
/** |
30
|
|
|
* @var Config |
31
|
|
|
*/ |
32
|
|
|
private $config; |
33
|
|
|
/** |
34
|
|
|
* @var Db |
35
|
|
|
*/ |
36
|
|
|
private $db; |
37
|
|
|
/** |
38
|
|
|
* @var cURL |
39
|
|
|
*/ |
40
|
|
|
private $curl; |
41
|
|
|
/** |
42
|
|
|
* @var Settings |
43
|
|
|
*/ |
44
|
|
|
private $settings; |
45
|
|
|
/** |
46
|
|
|
* @var Permissions |
47
|
|
|
*/ |
48
|
|
|
private $permissions; |
49
|
|
|
/** |
50
|
|
|
* @var ServerConfig |
51
|
|
|
*/ |
52
|
|
|
private $serverConfig; |
53
|
|
|
/** |
54
|
|
|
* @var Users |
55
|
|
|
*/ |
56
|
|
|
private $users; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* cleverBotMessage constructor. |
60
|
|
|
* @param $message |
61
|
|
|
* @param $discord |
62
|
|
|
* @param $log |
63
|
|
|
* @param $config |
64
|
|
|
* @param $db |
65
|
|
|
* @param $curl |
66
|
|
|
* @param $settings |
67
|
|
|
* @param $permissions |
68
|
|
|
* @param $serverConfig |
69
|
|
|
* @param $users |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function __construct($message, $discord, $log, $config, $db, $curl, $settings, $permissions, $serverConfig, $users) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->message = $message; |
74
|
|
|
$this->discord = $discord; |
75
|
|
|
$this->log = $log; |
76
|
|
|
$this->config = $config; |
77
|
|
|
$this->db = $db; |
78
|
|
|
$this->curl = $curl; |
79
|
|
|
$this->settings = $settings; |
80
|
|
|
$this->permissions = $permissions; |
81
|
|
|
$this->serverConfig = $serverConfig; |
82
|
|
|
$this->users = $users; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function run() |
89
|
|
|
{ |
90
|
|
|
$guildID = $this->message->getFullChannelAttribute()->guild_id; |
91
|
|
|
|
92
|
|
|
$cleverBotNick = $this->db->queryField("SELECT nick FROM cleverbot WHERE serverID = :serverID", "nick", array(":serverID" => $guildID)); |
93
|
|
|
|
94
|
|
|
// Simply remove the <id> part of the string, since it seems to make the responses from Cleverbot be less idiotic and terrible.. |
95
|
|
|
$msg = str_replace("<@{$this->discord->getClient()->id}>", "", $this->message->content); |
96
|
|
|
$response = $this->curl->post("https://cleverbot.io/1.0/ask", array("user" => $this->config->get("user", "cleverbot"), "key" => $this->config->get("key", "cleverbot"), "nick" => $cleverBotNick, "text" => $msg)); |
97
|
|
|
|
98
|
|
|
if ($response) { |
99
|
|
|
$resp = @json_decode($response); |
100
|
|
|
$reply = isset($resp->response) ? $resp->response : false; |
101
|
|
|
if ($reply) { |
102
|
|
|
$this->message->getChannelAttribute()->broadcastTyping(); |
103
|
|
|
$this->message->reply($reply); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: