diff --git a/src/Discord/Cache/CacheInterface.php b/src/Discord/Cache/CacheInterface.php index f1b4e00eb..1c5913d5f 100644 --- a/src/Discord/Cache/CacheInterface.php +++ b/src/Discord/Cache/CacheInterface.php @@ -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); diff --git a/src/Discord/Helpers/Guzzle.php b/src/Discord/Helpers/Guzzle.php index 2c8d034ed..29fa8e4a7 100644 --- a/src/Discord/Helpers/Guzzle.php +++ b/src/Discord/Helpers/Guzzle.php @@ -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 { diff --git a/src/Discord/Parts/Channel/Channel.php b/src/Discord/Parts/Channel/Channel.php index 639bb4e1d..6f06764d3 100644 --- a/src/Discord/Parts/Channel/Channel.php +++ b/src/Discord/Parts/Channel/Channel.php @@ -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. @@ -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. * diff --git a/src/Discord/Parts/User/Client.php b/src/Discord/Parts/User/Client.php index 67ea380d4..52e2f4187 100644 --- a/src/Discord/Parts/User/Client.php +++ b/src/Discord/Parts/User/Client.php @@ -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]+)/'); } /** @@ -179,7 +179,7 @@ public function getChannelsAttribute() */ public function getUsersAttribute() { - return Cache::getAll("/user.([0-9]+)/"); + return Cache::getAll('/user.([0-9]+)/'); } /**