Skip to content

Commit 80f7cb9

Browse files
committed
Completed Rate Limits
Updated Stable v1.0.0 Updated Channel Added Permission Overwrites
1 parent 677e9be commit 80f7cb9

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

src/Bot.php

+20-13
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
use Closure;
66
use Exception;
77
use Ourted\Interfaces\Channel;
8+
use Ourted\Interfaces\Emoji;
89
use Ourted\Interfaces\Guild;
910
use Ourted\Interfaces\Invite;
1011
use Ourted\Interfaces\Member;
12+
use Ourted\Interfaces\Settings;
1113
use Ourted\Interfaces\User;
1214
use Ourted\Interfaces\Webhook;
1315
use Ourted\Model\Channel\Embed;
1416
use Ourted\Utils\getaway;
1517
use Ourted\Utils\http;
16-
use Ourted\Interfaces\Settings;
1718
use Ratchet\Client\Connector;
1819
use Ratchet\Client\WebSocket;
1920
use Ratchet\RFC6455\Messaging\MessageInterface;
@@ -108,6 +109,8 @@ class Bot
108109
public $invite;
109110
/** @var Webhook */
110111
public $webhook;
112+
/** @var Emoji */
113+
public $emoji;
111114
/**
112115
* @var mixed
113116
*/
@@ -171,6 +174,7 @@ public function __construct($botToken, $botPrefix, $wssUrl = null)
171174
$this->channel = new Channel($this);
172175
$this->member = new Member($this);
173176
$this->invite = new Invite($this);
177+
$this->emoji = new Emoji($this);
174178
$this->guild = new Guild($this);
175179
$this->user = new User($this);
176180
$this->api = new http($this);
@@ -202,18 +206,20 @@ public function init()
202206

203207

204208
$conn->on('close', function ($code = null, $reason = null) use ($conn) {
205-
echo "\nConnection closed ({$code} - {$reason})\n";
209+
echo "\nConnection closed ({$code} ". $reason != null ? "- {$reason})\n" : "\n";
206210
if (!$this->reconnect) {
207211
die();
208212
} else {
209-
$conn->send(json_encode([
210-
"op" => 6,
211-
"d" => [
212-
"token" => $this->getBot()->token,
213-
"session_id" => $this->getBot()->session_id,
214-
"seq" => 1337
215-
]
216-
]));
213+
echo "We are begin of a rate limit, connect retrying after 60 seconds.";
214+
$this->loop->addTimer(60, function () use ($conn) {
215+
$conn->send(json_encode([
216+
"op" => 6,
217+
"d" => [
218+
"token" => $this->getBot()->token,
219+
"session_id" => $this->getBot()->session_id,
220+
"seq" => 1337
221+
]
222+
]));});
217223
}
218224
});
219225

@@ -272,10 +278,11 @@ public function addCommand($command_name, $function)
272278
$function($this, $command_name);
273279
}
274280

275-
public function getImageData($image_path){
276-
if(!file_exists($image_path) || str_ends_with($image_path, ("png" || "jpg" || "jpeg" | "PNG" || "JPG" || "JPEG"))) return "Fail";
281+
public function getImageData($image_path)
282+
{
283+
if (!file_exists($image_path) || str_ends_with($image_path, ("png" || "jpg" || "jpeg" | "PNG" || "JPG" || "JPEG"))) return "Fail";
277284
$imageData = base64_encode(file_get_contents($image_path));
278-
return 'data: '.mime_content_type($image_path).';base64,'.$imageData;
285+
return 'data: ' . mime_content_type($image_path) . ';base64,' . $imageData;
279286
}
280287

281288
/**

src/Interfaces/Channel.php

+21-6
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,28 @@ public function createOverwrite(array ...$overwrites)
113113
$r = array();
114114
array_keys($overwrites);
115115
foreach ($overwrites as $item) {
116-
$id = $item[0];
117-
$type = $item[1];
118-
$allow = $item[2];
119-
$deny = $item[3];
120-
$o = new Overwrite($id, $type, $allow, $deny);
121-
$r[] = $o->create_object();
116+
if(isset($overwrites[0][0])) {
117+
if (empty($item) || !array_key_exists(0, $item[0]) || !array_key_exists(1, $item[0]) || !array_key_exists(2, $item[0]) || !array_key_exists(3, $item[0])) return $r;
118+
$id = $item[0][0];
119+
$type = $item[0][1];
120+
$allow = $item[0][2];
121+
$deny = $item[0][3];
122+
$o = new Overwrite($id, $type, $allow, $deny);
123+
}else{
124+
if (empty($item) || !array_key_exists(0, $item) || !array_key_exists(1, $item) || !array_key_exists(2, $item) || !array_key_exists(3, $item)) return $r;
125+
$id = $item[0];
126+
$type = $item[1];
127+
$allow = $item[2];
128+
$deny = $item[3];
129+
$o = new Overwrite($id, $type, $allow, $deny);
130+
}
131+
$r[] = json_decode($o->create_object(), true);
122132
}
123133
return $r;
124134
}
135+
136+
public function createReaction(\Ourted\Model\Channel\Channel $channel, Message $message, \Ourted\Model\Guild\Emoji $emoji){
137+
return json_decode($this->bot->api->send("channels/{$channel->id}/messages/{$message->id}/reactions/{$emoji->name}:{$emoji->id}/@me", "", "PUT"));
138+
// /channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
139+
}
125140
}

src/Interfaces/Guild.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,10 @@ public function createChannel($guild, $channel_name, $type = 0, $topic = "", $pe
165165
$field .= ",\"topic\": \"{$topic}\"";
166166
if (!is_null($permissions)) {
167167
$n_item = "";
168-
foreach ($permissions as $item) {
169-
$n_item .= $item;
168+
foreach ($permissions as $key => $item) {
169+
$n_item .= $key == 0 ? $item : "," . $item;
170170
}
171-
$field .= ",\"permission_overwrites\": [{$item}]";
172-
171+
$field .= ",\"permission_overwrites\": [{$n_item}]";
173172
}
174173
if (!is_null($bitrate)) {
175174
if ($type == $this->bot->CHANNEL_GUILD_VOICE) {

src/Model/Channel/Channel.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ public function __construct($bot, $channel_id)
4141
$this->topic = $json->topic ?? "";
4242
$this->guild_id = $json->guild_id ?? null;
4343
$this->nsfw = $json->nsfw ?? false;
44-
$this->permission_overwrites = $json->permission_overwrites ?? null;
44+
if (!is_null($json->permission_overwrites)){
45+
$n_permissions = array();
46+
foreach (json_decode($result, true)["permission_overwrites"] as $item){
47+
if($item["type"] == "role"){
48+
$item["type"] = 0;
49+
}else{
50+
$item["type"] = 1;
51+
}
52+
$n_permissions[] = array($item["id"], $item["type"], $item["allow"], $item["deny"]);
53+
}
54+
$this->permission_overwrites = $bot->channel->createOverwrite($n_permissions);
55+
}
4556
$this->rate_limit_per_user = $json->rate_limit_per_user ?? null;
4657
return $this;
4758
}

src/Model/Channel/Overwrite.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public function __construct($id, $type, $allow = array(), $deny = array())
2525
$this->type = $type;
2626
$n_allow = 0;
2727
$n_deny = 0;
28-
foreach ($allow as $item) $n_allow += $item;
29-
foreach ($deny as $item) $n_deny += $item;
28+
if(is_array($allow)) foreach ($allow as $item) $n_allow += $item;
29+
else $n_allow = $allow;
30+
if(is_array($deny)) foreach ($deny as $item) $n_deny += $item;
31+
else $n_deny = $deny;
3032
$this->allow = $n_allow;
3133
$this->deny = $n_deny;
3234
}

src/Utils/http.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Ourted\Bot;
66

77

8-
98
class http
109
{
1110

@@ -51,13 +50,12 @@ public function send($url, $field, $request = "POST")
5150
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
5251
$result = curl_exec($ch);
5352
if (isset(json_decode($result, true)["X-Ratelimit-Remaining"])) {
54-
$remaining = json_decode($result, true)["X-Ratelimit-Remaining"];
55-
if ($remaining == 0) {
56-
$reset = json_decode($result, true)["X-Ratelimit-Reset"] - time();
57-
echo "We are begin of a rate limit, connect retrying after {$reset} seconds.";
58-
sleep($reset);
59-
return $this->send($url, $field, $request);
60-
}
53+
$reset = json_decode($result, true)["X-Ratelimit-Reset"] - time();
54+
echo "We are begin of a rate limit, connect retrying after {$reset} seconds.";
55+
$this->bot->loop->addTimer($reset, function () use ($url, $field, $request) {
56+
$this->send($url, $field, $request);
57+
});
58+
return sleep($reset);
6159
}
6260
return $result;
6361
}

0 commit comments

Comments
 (0)