Skip to content

Commit

Permalink
Better Message History (#99)
Browse files Browse the repository at this point in the history
* Removing space

* Adding a few more magic properties

* Applied fixes from StyleCI

* Adding new properties, and discriminator to member

* Applied fixes from StyleCI

* Adding better message history

* Applied fixes from StyleCI

* Applied fixes from StyleCI
  • Loading branch information
cryptiklemur authored and David committed Apr 15, 2016
1 parent 8d5ae80 commit 88c27b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Discord/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function get($key);
* Gets all the attributes in the cache.
*
* @param string $query A query string to run on the cache key(s).
*
*
* @return Collection A collection of cache objects.
*/
public function getAll($query = null);
Expand Down
5 changes: 5 additions & 0 deletions src/Discord/Helpers/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

/**
* Provides an easy wrapper for the Guzzle HTTP client.
*
* @method static object get(...$params)
* @method static object post(...$params)
* @method static object put(...$params)
* @method static object delete(...$params)
*/
class Guzzle
{
Expand Down
49 changes: 49 additions & 0 deletions src/Discord/Parts/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Discord\Parts\User\User;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* A Channel can be either a text or voice channel on a Discord guild.
Expand Down Expand Up @@ -243,6 +244,54 @@ public function getMessagesAttribute()
return Cache::get("channel.{$this->id}.messages");
}

/**
* Fetches message history.
*
* @param array $options
*
* @return array|Collection
* @throws \Exception
*/
public function getMessageHistory(array $options)
{
$resolver = new OptionsResolver();
$resolver->setDefaults(['limit' => 100]);
$resolver->setDefined(['before', 'after']);
$resolver->setAllowedValues('limit', range(1, 100));

$options = $resolver->resolve($options);
if (isset($options['before'], $options['after'])) {
throw new \Exception('Can only specify before, or after, not both.');
}

$url = "channels/{$this->id}/messages?limit={$options['limit']}";
if (isset($options['before'])) {
if ($options['before'] instanceof Message) {
throw new \Exception('before must be an instance of '.Message::class);
}
$url .= '&before='.$options['before']->id;
}
if (isset($options['after'])) {
if ($options['after'] instanceof Message) {
throw new \Exception('after must be an instance of '.Message::class);
}
$url .= '&after='.$options['after']->id;
}

$request = Guzzle::get($url);
$messages = [];

foreach ($request as $index => $message) {
$message = new Message((array) $message, true);
Cache::set("message.{$message->id}", $message);
$messages[$index] = $message;
}

$messages = new Collection($messages);

return $messages;
}

/**
* Returns the message history attribute.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Discord/Parts/User/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function updatePresence($ws, $gamename, $idle)
*/
public function getChannelsAttribute()
{
return Cache::getAll("/channel.([0-9]+)/");
return Cache::getAll('/channel.([0-9]+)/');
}

/**
Expand All @@ -179,7 +179,7 @@ public function getChannelsAttribute()
*/
public function getUsersAttribute()
{
return Cache::getAll("/user.([0-9]+)/");
return Cache::getAll('/user.([0-9]+)/');
}

/**
Expand Down

0 comments on commit 88c27b1

Please sign in to comment.