|
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
|
|
|
use discord\discord; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class twitterOutput |
|
30
|
|
|
*/ |
|
31
|
|
|
class twitterOutput |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var |
|
35
|
|
|
*/ |
|
36
|
|
|
var $config; |
|
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var |
|
39
|
|
|
*/ |
|
40
|
|
|
var $discord; |
|
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var |
|
43
|
|
|
*/ |
|
44
|
|
|
var $logger; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var |
|
48
|
|
|
*/ |
|
49
|
|
|
var $twitter; |
|
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var |
|
52
|
|
|
*/ |
|
53
|
|
|
var $lastCheck; |
|
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var |
|
56
|
|
|
*/ |
|
57
|
|
|
var $lastID; |
|
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var |
|
60
|
|
|
*/ |
|
61
|
|
|
var $channelID; |
|
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var |
|
64
|
|
|
*/ |
|
65
|
|
|
var $maxID; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $config |
|
69
|
|
|
* @param $discord |
|
70
|
|
|
* @param $logger |
|
71
|
|
|
*/ |
|
72
|
|
|
function init($config, $discord, $logger) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$this->config = $config; |
|
75
|
|
|
$this->discord = $discord; |
|
76
|
|
|
$this->logger = $logger; |
|
77
|
|
|
$this->guild = $config["bot"]["guild"]; |
|
|
|
|
|
|
78
|
|
|
$this->twitter = new Twitter($config["twitter"]["consumerKey"], $config["twitter"]["consumerSecret"], $config["twitter"]["accessToken"], $config["twitter"]["accessTokenSecret"]); |
|
79
|
|
|
$this->lastCheck = time(); |
|
80
|
|
|
$this->maxID = 0; |
|
81
|
|
|
$this->channelID = $config["plugins"]["twitterOutput"]["channelID"]; // outputs to the news channel on the 4M server |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
|
|
function tick() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$discord = $this->discord; |
|
90
|
|
|
|
|
91
|
|
|
$continue = false; |
|
92
|
|
|
$data = array(); |
|
93
|
|
|
// If last check + 60 seconds is larger or equal to the current time(), we run |
|
94
|
|
|
if ($this->lastCheck <= time()) { |
|
95
|
|
|
// Fetch the last 25 twitter replies and/or searches |
|
96
|
|
|
try { |
|
97
|
|
|
$data = $this->twitter->load(Twitter::ME_AND_FRIENDS, 5); |
|
98
|
|
|
foreach ($data as $message) { |
|
|
|
|
|
|
99
|
|
|
$text = (array) $message->text; |
|
100
|
|
|
$createdAt = (array) $message->created_at; |
|
101
|
|
|
$postedBy = (array) $message->user->name; |
|
102
|
|
|
$screenName = (array) $message->user->screen_name; |
|
103
|
|
|
$id = (int) $message->id; |
|
104
|
|
|
$this->lastID = getPermCache("twitterLatestID"); // get the last posted ID |
|
105
|
|
|
|
|
106
|
|
|
if ($id <= $this->lastID) { |
|
107
|
|
|
continue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->maxID = max($id, $this->maxID); |
|
111
|
|
|
|
|
112
|
|
|
$url = "https://twitter.com/" . $screenName[0] . "/status/" . $id; |
|
113
|
|
|
$message = array("message" => $text[0], "postedAt" => $createdAt[0], "postedBy" => $postedBy[0], "screenName" => $screenName[0], "url" => $url . $id[0]); |
|
114
|
|
|
$msg = "**@" . $screenName[0] . "** (" . $message["postedBy"] . ") / " . htmlspecialchars_decode($message["message"]); |
|
115
|
|
|
$messages[$id] = $msg; |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$continue = true; |
|
118
|
|
|
|
|
119
|
|
|
if (sizeof($data)) { |
|
120
|
|
|
setPermCache("twitterLatestID", $this->maxID); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} catch (Exception $e) { |
|
124
|
|
|
//$this->logger->err("Twitter Error: " . $e->getMessage()); // Don't show there was an error, it's most likely just a rate limit |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($continue == true) { |
|
|
|
|
|
|
128
|
|
|
ksort($messages); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($messages as $id => $msg) { |
|
131
|
|
|
// Send the tweets to the channel |
|
132
|
|
|
$channelID = $this->channelID; |
|
133
|
|
|
$guild = $discord->guilds->get('id', $this->guild); |
|
134
|
|
|
$channel = $guild->channels->get('id', $channelID); |
|
135
|
|
|
$channel->sendMessage($msg, false); |
|
136
|
|
|
sleep(1); // Lets sleep for a second, so we don't rage spam |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
$this->lastCheck = time() + 60; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param $url |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
function shortenUrl($url) |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
|
|
return file_get_contents("http://is.gd/api.php?longurl=" . $url); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $msgData |
|
154
|
|
|
*/ |
|
155
|
|
|
function onMessage($msgData) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
function information() |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
return array( |
|
165
|
|
|
"name" => "", |
|
166
|
|
|
"trigger" => array(""), |
|
167
|
|
|
"information" => "" |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
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.