-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Poll permissions, events, and other related changes (#1226)
* ✨ Implement the Poll object `Parts` * 🧑💻 Add a `poll` attribute to the `Message` part * 🧑💻 Add poll support to the `MessageBuilder` * ✨ Add event support to polls * 🧑💻 Allow passing an array to `Poll::addAnswer()` * 🎨 Clean up classes * 🎨 Update the poll expire endpoint constant * 🎨 Improve docblock wording * 🚚 Move the `Poll` object part to the `Channel` namespace * 🚚 Move the `Poll` create object art to the `Poll` namespace * 🎨 Improve `Poll::addAnswer` when passed an array * 🎨 Fix total answer count in `Poll:addAnswer` * 🎨 Push answers into a collection * ♻ Move `Poll::getAnswerVoters()` to `PollAnswer::getVoters()` * 🎨 Improve docblock wording * ⏪ Revert version change * 🩹 Fix return types * 🩹 Remove unused prop docblock * 🎨 Update `PollAnswer::getVoters` to be uniform with `Reaction::getUsers` * 🚧 Utilize caching in the `MESSAGE_POLL_VOTE_ADD` and `MESSAGE_POLL_VOTE_REMOVE` events * 🔧 Update the endpoint for `PollAnswer::getVoters` * 🚧 Create a repository for poll answers * 🎨 Add docblock for event fillable attributes * 🎨 Improve return types * 🎨 Rename `Poll::end` to `Poll::expire` for parity with the Discord API * 🎨 Remove unnecessary linebreak * 🔧 Update the `Poll::expire` endpoint * 🎨 Handle setting the `answers` attribute --------- Co-authored-by: Brandon <[email protected]>
- Loading branch information
1 parent
af54bcf
commit e584be6
Showing
17 changed files
with
910 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <[email protected]> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Parts\Channel; | ||
|
||
use Carbon\Carbon; | ||
use Discord\Helpers\Collection; | ||
use Discord\Http\Endpoint; | ||
use Discord\Parts\Channel\Poll\PollAnswer; | ||
use Discord\Parts\Channel\Poll\PollMedia; | ||
use Discord\Parts\Channel\Poll\PollResults; | ||
use Discord\Parts\Part; | ||
use Discord\Repository\Channel\PollAnswerRepository; | ||
use React\Promise\ExtendedPromiseInterface; | ||
|
||
/** | ||
* A message poll. | ||
* | ||
* @link https://discord.com/developers/docs/resources/poll#poll-object | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @property PollMedia $question The question of the poll. Only text is supported. | ||
* @property PollAnswerRepository $answers Each of the answers available in the poll. | ||
* @property Carbon $expiry The time when the poll ends. | ||
* @property bool $allow_multiselect Whether a user can select multiple answers. | ||
* @property int $layout_type The layout type of the poll. | ||
* @property PollResults|null $results The results of the poll. | ||
* | ||
* @property string $channel_id The ID of the channel the poll is in. | ||
* @property string $message_id The ID of the message the poll is in. | ||
*/ | ||
class Poll extends Part | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $fillable = [ | ||
'question', | ||
'expiry', | ||
'allow_multiselect', | ||
'layout_type', | ||
'results', | ||
|
||
// events | ||
'channel_id', | ||
'message_id', | ||
|
||
// repositories | ||
'answers', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $repositories = [ | ||
'answers' => PollAnswerRepository::class, | ||
]; | ||
|
||
/** | ||
* Sets the answers attribute. | ||
* | ||
* @param array $answers | ||
*/ | ||
protected function setAnswersAttribute(array $answers): void | ||
{ | ||
foreach ($answers as $answer) { | ||
/** @var ?PollAnswer */ | ||
if ($part = $this->answers->offsetGet($answer->answer_id)) { | ||
$part->fill($answer); | ||
} else { | ||
/** @var PollAnswer */ | ||
$part = $this->answers->create($answer); | ||
} | ||
|
||
$this->answers->pushItem($part); | ||
} | ||
|
||
$this->attributes['answers'] = $answers; | ||
} | ||
|
||
/** | ||
* Returns the question attribute. | ||
* | ||
* @return PollMedia | ||
*/ | ||
protected function getQuestionAttribute(): PollMedia | ||
{ | ||
return $this->factory->part(PollMedia::class, (array) $this->attributes['question'], true); | ||
} | ||
|
||
/** | ||
* Return the expiry attribute. | ||
* | ||
* @return Carbon | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function getExpiryAttribute(): Carbon | ||
{ | ||
return Carbon::parse($this->attributes['expiry']); | ||
} | ||
|
||
/** | ||
* Returns the results attribute. | ||
* | ||
* @return PollResults|null | ||
*/ | ||
protected function getResultsAttribute(): ?PollResults | ||
{ | ||
if (! isset($this->attributes['results'])) { | ||
return null; | ||
} | ||
|
||
return $this->factory->part(PollResults::class, (array) $this->attributes['results'], true); | ||
} | ||
|
||
/** | ||
* Expire the poll. | ||
* | ||
* @link https://discord.com/developers/docs/resources/poll#end-poll | ||
* | ||
* @return ExtendedPromiseInterface<Message> | ||
*/ | ||
public function expire(): ExtendedPromiseInterface | ||
{ | ||
return $this->http->post(Endpoint::bind(Endpoint::MESSAGE_POLL_EXPIRE, $this->channel_id, $this->message_id)) | ||
->then(function ($response) { | ||
return $this->factory->create(Message::class, (array) $response, true); | ||
}); | ||
} | ||
} |
Oops, something went wrong.