/** * Handles the message. * * @param Message $message * @param array $params * @param Discord $discord * @param Config $config * @return void */ public static function handleMessage($message, $params, $discord, $config) { $id = isset($params[1]) ? $params[1] : $message->author->id; if (preg_match('/<@(.+)>/', $id, $matches)) { $id = $matches[1]; } $user = new User((array) Guzzle::get("users/{$id}"), true); $str = "**{$user->username}:**\r\n"; $str .= "**ID:** {$user->id}\r\n"; $str .= "**Avatar URL:** {$user->avatar}\r\n"; $str .= "**Discriminator:** {$user->discriminator}\r\n"; $str .= "**Mention:** `{$user}`\r\n"; $guildcount = 0; $servers = ''; foreach ($discord->guilds as $guild) { foreach ($guild->members as $member) { if ($member->id == $user->id) { $guildcount++; $servers .= $guild->name . ", "; } } } $servers = rtrim($servers, ', '); $str .= "**Shared Servers:** {$guildcount} _({$servers})_\r\n"; $level = isset($config['perms']['perms'][$user->id]) ? $config['perms']['perms'][$user->id] : $config['perms']['default']; $level = $config['perms']['levels'][$level]; $str .= "**User Level:** {$level}\r\n"; $message->channel->sendMessage($str); }
/** * Broadcasts that you are typing to the channel. Lasts for 5 seconds. * * @return boolean */ public function broadcastTyping() { if ($this->type != self::TYPE_TEXT) { return false; } Guzzle::post("channels/{$this->id}/typing"); return true; }
/** * Accepts a Discord channel invite. * * @param $code * * @return Invite * @throws InviteInvalidException */ public function acceptInvite($code) { try { $request = Guzzle::post("invite/{$code}"); } catch (\Exception $e) { throw new InviteInvalidException('The invite is invalid or has expired.'); } return new Invite(['code' => $request->code, 'guild' => $request->guild, 'xkcdpass' => $request->xkcdpass, 'channel' => $request->channel], true); }
public function __construct(LoopInterface $loop, Resolver $resolver = null) { $this->defaultHeaders['User-Agent'] = Guzzle::getUserAgent(); if (null === $resolver) { $factory = new DnsFactory(); $resolver = $factory->create('8.8.8.8', $loop); } $this->_loop = $loop; $this->_connector = new Connector($loop, $resolver); $this->_secureConnector = new SecureConnector($this->_connector, $loop); }
/** * Broadcasts that you are typing to the channel. Lasts for 5 seconds. * * @return boolean */ public function broadcastTyping() { if (isset($this->attributes_cache['channel_id'])) { $channel_id = $this->attributes_cache['channel_id']; } else { $channel = Guzzle::post('users/@me/channels', ['recipient_id' => $this->id]); $channel_id = $channel->id; $this->attributes_cache['channel_id'] = $channel->id; } Guzzle::post("channels/{$channel_id}/typing"); return true; }
/** * Returns an array of Guilds. * * @return array */ public function getGuildsAttribute() { if (isset($this->attributes_cache['guilds'])) { return $this->attributes_cache['guilds']; } $guilds = []; $request = Guzzle::get("users/@me/guilds"); foreach ($request as $index => $guild) { $guilds[$guild->id] = 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, 'embed_enabled' => $guild->embed_enabled, 'embed_channel_id' => $guild->embed_channel_id], true); } $this->attributes_cache['guilds'] = $guilds; return $guilds; }
/** * Returns the roles attribute. * * @return array */ public function getRolesAttribute() { if (isset($this->attributes_cache['roles'])) { return $this->attributes_cache['roles']; } $roles = []; $request = Guzzle::get($this->replaceWithVariables('guilds/:guild_id/roles')); foreach ($request as $key => $role) { if (in_array($role->id, $this->attributes['roles'])) { $roles[] = new Role(['id' => $role->id, 'name' => $role->name, 'color' => $role->color, 'managed' => $role->managed, 'hoist' => $role->hoist, 'position' => $role->position, 'permissions' => new Permission(['perms' => $role->permissions]), 'guild_id' => $this->guild_id], true); } } $this->attributes_cache['roles'] = $roles; return $roles; }
/** * Handles the message. * * @param Message $message * @param array $params * @param Discord $discord * @param Config $config * @return void */ public static function handleMessage($message, $params, $discord, $config) { $id = isset($params[0]) ? $params[0] : $message->author->id; if (preg_match('/<@(.+)>/', $id, $matches)) { $id = $matches[1]; } $user = new User((array) Guzzle::get("users/{$id}"), true); $str = "**Information for {$user->username}#{$user->discriminator}:**\r\n"; $str .= "**ID:** {$user->id}\r\n"; $str .= "**Avatar URL:** {$user->avatar}\r\n"; $str .= "**Discriminator:** {$user->discriminator}\r\n"; $str .= "**Mention:** `{$user}`\r\n"; $guildcount = 0; $servers = ''; foreach ($discord->guilds as $guild) { foreach ($guild->members as $member) { if ($member->id == $user->id) { $guildcount++; $servers .= $guild->name . ", "; } } } $servers = rtrim($servers, ', '); $str .= "**Shared Servers:** {$guildcount} _({$servers})_\r\n"; $level = isset($config['perms']['perms'][$user->id]) ? $config['perms']['perms'][$user->id] : $config['perms']['default']; $level = $config['perms']['levels'][$level]; $str .= "**User Level:** {$level}\r\n\r\n"; $roles = ''; try { foreach ($message->full_channel->guild->members->get('id', $id)->roles as $role) { $roles .= str_replace('@everyone', '@ everyone', $role->name) . ', '; } } catch (\Exception $e) { $roles = 'Could not get roles.'; } $roles = rtrim($roles, ', '); $str .= "**User Roles:** _{$roles}_\r\n"; $joinedDiscord = Discord::getTimestamp($message->author->id); $str .= "**Joined Discord:** {$joinedDiscord}\r\n"; $joinedGuild = $message->full_channel->guild->members->get('id', $id)->joined_at; $str .= "**Joined Guild:** {$joinedGuild}\r\n"; $message->channel->sendMessage($str); }
/** * Deletes the part on the Discord servers. * * @return boolean */ public function delete() { if (!$this->deletable) { return false; } try { $request = Guzzle::delete($this->replaceWithVariables($this->uris['delete'])); $this->created = false; $this->deleted = true; } catch (\Exception $e) { throw new PartRequestFailedException($e->getMessage()); } return true; }
/** * Returns the guilds bans. * * @return Collection */ public function getBansAttribute() { if (isset($this->attributes_cache['bans'])) { return $this->attributes_cache['bans']; } $bans = []; try { $request = Guzzle::get($this->replaceWithVariables('guilds/:id/bans')); } catch (DiscordRequestFailedException $e) { return false; } foreach ($request as $index => $ban) { $bans[$index] = new Ban(['user' => $ban->user, 'guild' => $this], true); } $bans = new Collection($bans); $this->attributes_cache['bans'] = $bans; return $bans; }
} }); // Handle close event (Not exactly gracefully, but consider it handled... /** @var \Discord\WebSockets\WebSocket $websocket */ /** @var \Sluggard\SluggardApp $app */ $websocket->on("close", function ($websocket, $reason, $discord) use($app) { $app->log->err("Connection was closed: " . $reason); die; }); // Handle close event (Not exactly gracefully, but consider it handled... /** @var \Discord\WebSockets\WebSocket $websocket */ /** @var \Sluggard\SluggardApp $app */ $websocket->on("error", function ($error, $websocket) use($app) { $app->log->err("Error: {$error}"); }); // Handle reconnect event /** @var \Sluggard\SluggardApp $app */ $websocket->on("reconnect", function () use($app) { $app->log->info("Reconnecting to Discord"); }); // Handle reconnected event /** @var \Sluggard\SluggardApp $app */ $websocket->on("reconnected", function () use($app) { $app->log->info("Reconnected to Discord"); }); // Setup the cache (Only works aslong as the bot is running) \Discord\Cache\Cache::setCache(new \Discord\Cache\Drivers\ArrayCacheDriver()); // Add some config options to Guzzle.. \Discord\Helpers\Guzzle::addGuzzleOptions(array("http_errors" => false, "allow_redirects" => true)); // Start the bot $websocket->run();
/** * Gets the WebSocket gateway. * * @return string */ public function getGateway() { return Guzzle::get('gateway')->url; }
/** * Returns the guilds bans. * * @return array */ public function getBansAttribute() { if (isset($this->attributes_cache['bans'])) { return $this->attributes_cache['bans']; } $bans = []; $request = Guzzle::get($this->replaceWithVariables('guilds/:id/bans')); foreach ($request as $index => $ban) { $bans[$index] = new Ban(['user' => $ban->user, 'guild' => $this], true); } $this->attributes_cache['bans'] = $bans; return $bans; }