diff --git a/src/Bot.php b/src/Bot.php index c141890..333acf9 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -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; @@ -108,6 +109,8 @@ class Bot public $invite; /** @var Webhook */ public $webhook; + /** @var Emoji */ + public $emoji; /** * @var mixed */ @@ -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); @@ -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 + ] + ]));}); } }); @@ -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; } /** diff --git a/src/Interfaces/Channel.php b/src/Interfaces/Channel.php index 5654839..1160558 100644 --- a/src/Interfaces/Channel.php +++ b/src/Interfaces/Channel.php @@ -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 + } } \ No newline at end of file diff --git a/src/Interfaces/Guild.php b/src/Interfaces/Guild.php index a9cb178..75fa9e1 100644 --- a/src/Interfaces/Guild.php +++ b/src/Interfaces/Guild.php @@ -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) { diff --git a/src/Model/Channel/Channel.php b/src/Model/Channel/Channel.php index 4ec55ff..da3d772 100644 --- a/src/Model/Channel/Channel.php +++ b/src/Model/Channel/Channel.php @@ -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; } diff --git a/src/Model/Channel/Overwrite.php b/src/Model/Channel/Overwrite.php index 4b154dc..c2c4d5c 100644 --- a/src/Model/Channel/Overwrite.php +++ b/src/Model/Channel/Overwrite.php @@ -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; } diff --git a/src/Utils/http.php b/src/Utils/http.php index 042d8ea..a20bf28 100644 --- a/src/Utils/http.php +++ b/src/Utils/http.php @@ -5,7 +5,6 @@ use Ourted\Bot; - class http { @@ -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; }