Ejemplo n.º 1
0
 /**
  * Runs the WebSocket client.
  *
  * @return void 
  */
 public function run()
 {
     $this->wsfactory->__invoke($this->gateway)->then(function (WebSocketInstance $ws) {
         $this->ws = $ws;
         $ws->on('message', function ($data, $ws) {
             $this->emit('raw', [$data, $this->discord]);
             $data = json_decode($data);
             if (!is_null($handler = $this->handlers->getHandler($data->t))) {
                 $handler = new $handler();
                 $handlerData = $handler->getData($data->d, $this->discord);
                 $newDiscord = $handler->updateDiscordInstance($handlerData, $this->discord);
                 $this->emit($data->t, [$handlerData, $this->discord, $newDiscord]);
                 $this->discord = $newDiscord;
             }
             if ($data->t == Event::READY) {
                 $tts = $data->d->heartbeat_interval / 1000;
                 $this->loop->addPeriodicTimer($tts, function () use($ws) {
                     $this->send(['op' => 1, 'd' => microtime(true) * 1000]);
                 });
                 $content = $data->d;
                 // set user settings obtain guild data etc.
                 // user client settings
                 $this->discord->user_settings = $content->user_settings;
                 // guilds
                 $guilds = new Collection();
                 foreach ($content->guilds as $guild) {
                     $guildPart = new Guild(['id' => $guild->id, 'name' => $guild->name, 'icon' => $guild->icon, 'region' => $guild->region, 'owner_id' => $guild->owner_id, 'roles' => $guild->roles, 'joined_at' => $guild->joined_at, 'afk_channel_id' => $guild->afk_channel_id, 'afk_timeout' => $guild->afk_timeout, 'large' => $guild->large, 'features' => $guild->features, 'splash' => $guild->splash, 'emojis' => $guild->emojis], true);
                     $channels = new Collection();
                     foreach ($guild->channels as $channel) {
                         $channelPart = new Channel(['id' => $channel->id, 'name' => $channel->name, 'type' => $channel->type, 'topic' => $channel->topic, 'guild_id' => $guild->id, 'position' => $channel->position, 'last_message_id' => $channel->last_message_id, 'permission_overwrites' => $channel->permission_overwrites], true);
                         $channels->push($channelPart);
                     }
                     $guildPart->setCache('channels', $channels);
                     // preload
                     $guildPart->getBansAttribute();
                     // guild members
                     $members = new Collection();
                     foreach ($guild->members as $member) {
                         $memberPart = new Member(['user' => $member->user, 'roles' => $member->roles, 'mute' => $member->mute, 'deaf' => $member->deaf, 'joined_at' => $member->joined_at, 'guild_id' => $guild->id, 'status' => 'offline', 'game' => null], true);
                         // check for presences
                         foreach ($guild->presences as $presence) {
                             if ($presence->user->id == $member->user->id) {
                                 $memberPart->status = $presence->status;
                                 $memberPart->game = $presence->game;
                             }
                         }
                         $members->push($memberPart);
                     }
                     $guildPart->setCache('members', $members);
                     $guilds->push($guildPart);
                 }
                 $this->discord->setCache('guilds', $guilds);
                 // after we do everything, emit ready
                 $this->emit('ready', [$this->discord]);
             }
         });
         $ws->on('close', function ($ws) {
             $this->emit('close', [$ws, $this->discord]);
         });
         $ws->on('error', function ($error, $ws) {
             $this->emit('error', [$error, $ws, $this->discord]);
         });
         if (!$this->sentLoginFrame) {
             $this->sendLoginFrame();
             $this->sentLoginFrame = true;
             $this->emit('sent-login-frame', [$ws, $this->discord]);
         }
     }, function ($e) {
         $this->emit('connectfail', [$e]);
         $this->loop->stop();
     });
     $this->loop->run();
 }