Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Cole committed Dec 28, 2016
2 parents f9eb2d3 + 258ef59 commit b44c4be
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 21 deletions.
22 changes: 21 additions & 1 deletion src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Discord
*
* @var string Version.
*/
const VERSION = 'v4.0.2';
const VERSION = 'v4.0.3';

/**
* The logger.
Expand Down Expand Up @@ -482,6 +482,7 @@ protected function handleGuildMembersChunk($data)

$memberPart = $this->factory->create(Member::class, $member, true);
$guild->members->push($memberPart);
$this->users->push($memberPart->user);
++$count;
}

Expand Down Expand Up @@ -1338,4 +1339,23 @@ public function __call($name, $params)

return call_user_func_array([$this->client, $name], $params);
}

/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array
*/
public function __debugInfo()
{
$secrets = [
'token' => '*****',
];
$replace = array_intersect_key($secrets, $this->options);
$config = $replace + $this->options;

unset($config['loop'], $config['cachePool'], $config['logger']);

return $config;
}
}
3 changes: 2 additions & 1 deletion src/Discord/Http/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function runRequest($method, $url, $headers, $body, array $options = [])
$method,
$url,
$headers,
$body
$body,
'1.0'
);
$count = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Discord/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function runRequest($method, $url, $content, $extraHeaders, $cache, $blo
}

$headers = [
'User-Agent' => $this->getUserAgent(),
'User-Agent' => $this->getUserAgent(),
];

$headers['authorization'] = $this->token;
Expand Down
23 changes: 15 additions & 8 deletions src/Discord/Parts/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
* @property int $bitrate The bitrate of the channel. Only for voice channels.
* @property \Discord\Parts\User\User $recipient The first recipient of the channel. Only for DM or group channels.
* @property Collection[User] $recipients A collection of all the recipients in the channel. Only for DM or group channels.
*
* @property \Discord\Repository\Channel\VoiceMemberRepository $members
* @property \Discord\Repository\Channel\MessageRepository $messages
* @property \Discord\Repository\Channel\OverwriteRepository $overwrites
Expand Down Expand Up @@ -99,7 +98,7 @@ public function afterConstruct()
*/
public function getIsPrivateAttribute()
{
return (array_search($this->type, [self::TYPE_DM, self::TYPE_GROUP]) !== false);
return array_search($this->type, [self::TYPE_DM, self::TYPE_GROUP]) !== false;
}

/**
Expand Down Expand Up @@ -338,14 +337,17 @@ public function getMessageHistory(array $options)

$resolver = new OptionsResolver();
$resolver->setDefaults(['limit' => 100, 'cache' => true]);
$resolver->setDefined(['before', 'after']);
$resolver->setDefined(['before', 'after', 'around']);
$resolver->setAllowedTypes('before', [Message::class, 'string']);
$resolver->setAllowedTypes('after', [Message::class, 'string']);
$resolver->setAllowedTypes('around', [Message::class, 'string']);
$resolver->setAllowedValues('limit', range(1, 100));

$options = $resolver->resolve($options);
if (isset($options['before'], $options['after'])) {
$deferred->reject(new \Exception('Can only specify before, or after, not both.'));
if (isset($options['before'], $options['after']) ||
isset($options['before'], $options['around']) ||
isset($options['around'], $options['around'])) {
$deferred->reject(new \Exception('Can only specify one of before, after and around.'));

return $deferred->promise();
}
Expand All @@ -357,6 +359,9 @@ public function getMessageHistory(array $options)
if (isset($options['after'])) {
$url .= '&after='.($options['after'] instanceof Message ? $options['after']->id : $options['after']);
}
if (isset($options['around'])) {
$url .= '&around='.($options['around'] instanceof Message ? $options['around']->id : $options['around']);
}

$this->http->get($url, null, [], $options['cache'] ? null : 0)->then(
function ($response) use ($deferred) {
Expand Down Expand Up @@ -509,12 +514,13 @@ public function setPermissionOverwritesAttribute($overwrites)
/**
* Sends a message to the channel if it is a text channel.
*
* @param string $text The text to send in the message.
* @param bool $tts Whether the message should be sent with text to speech enabled.
* @param string $text The text to send in the message.
* @param bool $tts Whether the message should be sent with text to speech enabled.
* @param Embed $embed An embed to send.
*
* @return \React\Promise\Promise
*/
public function sendMessage($text, $tts = false)
public function sendMessage($text, $tts = false, $embed = null)
{
$deferred = new Deferred();

Expand All @@ -529,6 +535,7 @@ public function sendMessage($text, $tts = false)
[
'content' => $text,
'tts' => $tts,
'embed' => $embed,
]
)->then(
function ($response) use ($deferred) {
Expand Down
22 changes: 19 additions & 3 deletions src/Discord/Parts/Channel/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @property Carbon|null $edited_timestamp A timestamp of when the message was edited, or null.
* @property bool $tts Whether the message was sent as a text-to-speech message.
* @property array $attachments An array of attachment objects.
* @property array $embeds An array of embed objects.
* @property Collection[Embed] $embeds A collection of embed objects.
* @property string|null $nonce A randomly generated string that provides verification for the client. Not required.
* @property Collection[Role] $mention_roles A collection of roles that were mentioned in the message.
* @property bool $pinned Whether the message is pinned to the channel.
Expand Down Expand Up @@ -97,8 +97,8 @@ public function getChannelAttribute()
}

return $this->factory->create(Channel::class, [
'id' => $this->channel_id,
'type' => Channel::TYPE_DM,
'id' => $this->channel_id,
'type' => Channel::TYPE_DM,
], true);
}

Expand Down Expand Up @@ -150,6 +150,22 @@ public function getAuthorAttribute()
return $this->channel->guild->members->get('id', $this->attributes['author']->id);
}

/**
* Returns the embed attribute.
*
* @return Collection A collection of embeds.
*/
public function getEmbedsAttribute()
{
$embeds = new Collection();

foreach ($this->attributes['embeds'] as $embed) {
$embeds->push($this->factory->create(Embed::class, $embed, true));
}

return $embeds;
}

/**
* Returns the timestamp attribute.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Discord/Parts/Embed/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Discord\Parts\Embed;

use Discord\Parts\Part;

/**
* The author of an embed object.
*
* @property string $name The name of the author.
* @property string $url The URL to the author.
* @property string $icon_url The source of the author icon. Must be https.
* @property string $proxy_icon_url A proxied version of the icon url.
*/
class Author extends Part
{
/**
* {@inheritdoc}
*/
protected $fillable = ['name', 'url', 'icon_url', 'proxy_icon_url'];
}
132 changes: 132 additions & 0 deletions src/Discord/Parts/Embed/Embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Discord\Parts\Embed;

use Carbon\Carbon;
use Discord\Helpers\Collection;
use Discord\Parts\Part;

/**
* An embed object to be sent with a message.
*
* @property string $title The title of the embed.
* @property string $description A description of the embed.
* @property string $url The URL of the embed.
* @property Carbon|string $timestamp A timestamp of the embed.
* @property int $color The color of the embed.
* @property Footer $footer The footer of the embed.
* @property Image $image The image of the embed.
* @property Image $thumbnail The thumbnail of the embed.
* @property Video $video The video of the embed.
* @property array $provider The provider of the embed.
* @property Author $author The author of the embed.
* @property Collection[Field] $fields A collection of embed fields.
*/
class Embed extends Part
{
/**
* {@inheritdoc}
*/
protected $fillable = ['title', 'type', 'description', 'url', 'timestamp', 'color', 'footer', 'image', 'thumbnail', 'video', 'provider', 'author', 'fields'];

/**
* Gets the timestamp attribute.
*
* @return Carbon The timestamp attribute.
*/
public function getTimestampAttribute()
{
if (! array_key_exists('timestamp', $this->attributes)) {
return Carbon::now();
}

return Carbon::parse($this->attributes['timestamp']);
}

/**
* Gets the footer attribute.
*
* @return Footer The footer attribute.
*/
public function getFooterAttribute()
{
return $this->attributeHelper('footer', Footer::class);
}

/**
* Gets the image attribute.
*
* @return Image The image attribute.
*/
public function getImageAttribute()
{
return $this->attributeHelper('image', Image::class);
}

/**
* Gets the thumbnail attribute.
*
* @return Thumbnail The thumbnail attribute.
*/
public function getThumbnailAttribute()
{
return $this->attributeHelper('thumbnail', Image::class);
}

/**
* Gets the video attribute.
*
* @return Video The video attribute.
*/
public function getVideoAttribute()
{
return $this->attributeHelepr('video', Video::class);
}

/**
* Gets the author attribute.
*
* @return Author The author attribute.
*/
public function getAuthorAttribute()
{
return $this->attributeHelper('author', Author::class);
}

/**
* Gets the fields attribute.
*
* @return Collection[Field] The fields attribute.
*/
public function getFieldsAttribute()
{
$fields = new Collection();

foreach ($this->attributes['fields'] as $field) {
if (! ($field instanceof Field)) {
$field = $this->discord->factory(Field::class, $field, true);
}

$fields->push($field);
}

return $fields;
}

/**
* Helps with getting embed attributes.
*
* @param string $key The attribute key.
* @param string $class The attribute class.
*
* @return mixed
*/
protected function attributeHelper($key, $class)
{
if ($this->attributes[$key] instanceof $class) {
return $this->attributes[$key];
}

return $this->discord->factory($class, $this->attributes[$key], true);
}
}
34 changes: 34 additions & 0 deletions src/Discord/Parts/Embed/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Discord\Parts\Embed;

use Discord\Parts\Part;

/**
* A field of an embed object.
*
* @property string $name The name of the field.
* @property string $value The value of the field.
* @property bool $inline Whether the field should be displayed in-line.
*/
class Field extends Part
{
/**
* {@inheritdoc}
*/
protected $fillable = ['name', 'value', 'inline'];

/**
* Gets the inline attribute.
*
* @return bool The inline attribute.
*/
public function getInlineAttribute()
{
if (! array_key_exists('inline', $this->attributes)) {
return false;
}

return $this->attributes['inline'];
}
}
20 changes: 20 additions & 0 deletions src/Discord/Parts/Embed/Footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Discord\Parts\Embed;

use Discord\Parts\Part;

/**
* The footer section of an embed.
*
* @property string $text Footer text.
* @property string $icon_url URL of an icon for the footer. Must be https.
* @property string $proxy_icon_url Proxied version of the icon URL.
*/
class Footer extends Part
{
/**
* {@inheritdoc}
*/
protected $fillable = ['text', 'icon_url', 'proxy_icon_url'];
}

0 comments on commit b44c4be

Please sign in to comment.