Skip to content

Commit

Permalink
Completed Rate Limits
Browse files Browse the repository at this point in the history
Updated Stable v1.0.0
Updated Channel
Added Permission Overwrites
  • Loading branch information
gncdev committed Oct 17, 2020
1 parent 677e9be commit 80f7cb9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 34 deletions.
33 changes: 20 additions & 13 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use Closure;
use Exception;
use Ourted\Interfaces\Channel;
use Ourted\Interfaces\Emoji;
use Ourted\Interfaces\Guild;
use Ourted\Interfaces\Invite;
use Ourted\Interfaces\Member;
use Ourted\Interfaces\Settings;
use Ourted\Interfaces\User;
use Ourted\Interfaces\Webhook;
use Ourted\Model\Channel\Embed;
use Ourted\Utils\getaway;
use Ourted\Utils\http;
use Ourted\Interfaces\Settings;
use Ratchet\Client\Connector;
use Ratchet\Client\WebSocket;
use Ratchet\RFC6455\Messaging\MessageInterface;
Expand Down Expand Up @@ -108,6 +109,8 @@ class Bot
public $invite;
/** @var Webhook */
public $webhook;
/** @var Emoji */
public $emoji;
/**
* @var mixed
*/
Expand Down Expand Up @@ -171,6 +174,7 @@ public function __construct($botToken, $botPrefix, $wssUrl = null)
$this->channel = new Channel($this);
$this->member = new Member($this);
$this->invite = new Invite($this);
$this->emoji = new Emoji($this);
$this->guild = new Guild($this);
$this->user = new User($this);
$this->api = new http($this);
Expand Down Expand Up @@ -202,18 +206,20 @@ public function init()


$conn->on('close', function ($code = null, $reason = null) use ($conn) {
echo "\nConnection closed ({$code} - {$reason})\n";
echo "\nConnection closed ({$code} ". $reason != null ? "- {$reason})\n" : "\n";
if (!$this->reconnect) {
die();
} else {
$conn->send(json_encode([
"op" => 6,
"d" => [
"token" => $this->getBot()->token,
"session_id" => $this->getBot()->session_id,
"seq" => 1337
]
]));
echo "We are begin of a rate limit, connect retrying after 60 seconds.";
$this->loop->addTimer(60, function () use ($conn) {
$conn->send(json_encode([
"op" => 6,
"d" => [
"token" => $this->getBot()->token,
"session_id" => $this->getBot()->session_id,
"seq" => 1337
]
]));});
}
});

Expand Down Expand Up @@ -272,10 +278,11 @@ public function addCommand($command_name, $function)
$function($this, $command_name);
}

public function getImageData($image_path){
if(!file_exists($image_path) || str_ends_with($image_path, ("png" || "jpg" || "jpeg" | "PNG" || "JPG" || "JPEG"))) return "Fail";
public function getImageData($image_path)
{
if (!file_exists($image_path) || str_ends_with($image_path, ("png" || "jpg" || "jpeg" | "PNG" || "JPG" || "JPEG"))) return "Fail";
$imageData = base64_encode(file_get_contents($image_path));
return 'data: '.mime_content_type($image_path).';base64,'.$imageData;
return 'data: ' . mime_content_type($image_path) . ';base64,' . $imageData;
}

/**
Expand Down
27 changes: 21 additions & 6 deletions src/Interfaces/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,28 @@ public function createOverwrite(array ...$overwrites)
$r = array();
array_keys($overwrites);
foreach ($overwrites as $item) {
$id = $item[0];
$type = $item[1];
$allow = $item[2];
$deny = $item[3];
$o = new Overwrite($id, $type, $allow, $deny);
$r[] = $o->create_object();
if(isset($overwrites[0][0])) {
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;
$id = $item[0][0];
$type = $item[0][1];
$allow = $item[0][2];
$deny = $item[0][3];
$o = new Overwrite($id, $type, $allow, $deny);
}else{
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;
$id = $item[0];
$type = $item[1];
$allow = $item[2];
$deny = $item[3];
$o = new Overwrite($id, $type, $allow, $deny);
}
$r[] = json_decode($o->create_object(), true);
}
return $r;
}

public function createReaction(\Ourted\Model\Channel\Channel $channel, Message $message, \Ourted\Model\Guild\Emoji $emoji){
return json_decode($this->bot->api->send("channels/{$channel->id}/messages/{$message->id}/reactions/{$emoji->name}:{$emoji->id}/@me", "", "PUT"));
// /channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
}
}
7 changes: 3 additions & 4 deletions src/Interfaces/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,10 @@ public function createChannel($guild, $channel_name, $type = 0, $topic = "", $pe
$field .= ",\"topic\": \"{$topic}\"";
if (!is_null($permissions)) {
$n_item = "";
foreach ($permissions as $item) {
$n_item .= $item;
foreach ($permissions as $key => $item) {
$n_item .= $key == 0 ? $item : "," . $item;
}
$field .= ",\"permission_overwrites\": [{$item}]";

$field .= ",\"permission_overwrites\": [{$n_item}]";
}
if (!is_null($bitrate)) {
if ($type == $this->bot->CHANNEL_GUILD_VOICE) {
Expand Down
13 changes: 12 additions & 1 deletion src/Model/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ public function __construct($bot, $channel_id)
$this->topic = $json->topic ?? "";
$this->guild_id = $json->guild_id ?? null;
$this->nsfw = $json->nsfw ?? false;
$this->permission_overwrites = $json->permission_overwrites ?? null;
if (!is_null($json->permission_overwrites)){
$n_permissions = array();
foreach (json_decode($result, true)["permission_overwrites"] as $item){
if($item["type"] == "role"){
$item["type"] = 0;
}else{
$item["type"] = 1;
}
$n_permissions[] = array($item["id"], $item["type"], $item["allow"], $item["deny"]);
}
$this->permission_overwrites = $bot->channel->createOverwrite($n_permissions);
}
$this->rate_limit_per_user = $json->rate_limit_per_user ?? null;
return $this;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Model/Channel/Overwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function __construct($id, $type, $allow = array(), $deny = array())
$this->type = $type;
$n_allow = 0;
$n_deny = 0;
foreach ($allow as $item) $n_allow += $item;
foreach ($deny as $item) $n_deny += $item;
if(is_array($allow)) foreach ($allow as $item) $n_allow += $item;
else $n_allow = $allow;
if(is_array($deny)) foreach ($deny as $item) $n_deny += $item;
else $n_deny = $deny;
$this->allow = $n_allow;
$this->deny = $n_deny;
}
Expand Down
14 changes: 6 additions & 8 deletions src/Utils/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Ourted\Bot;



class http
{

Expand Down Expand Up @@ -51,13 +50,12 @@ public function send($url, $field, $request = "POST")
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (isset(json_decode($result, true)["X-Ratelimit-Remaining"])) {
$remaining = json_decode($result, true)["X-Ratelimit-Remaining"];
if ($remaining == 0) {
$reset = json_decode($result, true)["X-Ratelimit-Reset"] - time();
echo "We are begin of a rate limit, connect retrying after {$reset} seconds.";
sleep($reset);
return $this->send($url, $field, $request);
}
$reset = json_decode($result, true)["X-Ratelimit-Reset"] - time();
echo "We are begin of a rate limit, connect retrying after {$reset} seconds.";
$this->bot->loop->addTimer($reset, function () use ($url, $field, $request) {
$this->send($url, $field, $request);
});
return sleep($reset);
}
return $result;
}
Expand Down

0 comments on commit 80f7cb9

Please sign in to comment.