Skip to content

Commit

Permalink
✨ Add support for polls
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jul 18, 2024
1 parent fc11623 commit 6ad0a07
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
12 changes: 12 additions & 0 deletions resources/data/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@
"oldMessage": "?Discord\\Parts\\Channel\\Message"
}
},
{
"MessagePollVoteAdd": {
"answer": "Discord\\Parts\\Channel\\Poll\\PollAnswer",
"discord": "Discord\\Discord"
}
},
{
"MessagePollVoteRemove": {
"answer": "Discord\\Parts\\Channel\\Poll\\PollAnswer",
"discord": "Discord\\Discord"
}
},
{
"PresenceUpdate": {
"presence": "Discord\\Parts\\WebSockets\\PresenceUpdate",
Expand Down
42 changes: 42 additions & 0 deletions src/Discord/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Discord\Http\Exceptions\NoPermissionsException;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Message as ChannelMessage;
use Discord\Parts\Channel\Poll\Request\Poll;
use Discord\Parts\Channel\Webhook;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;
Expand Down Expand Up @@ -145,6 +146,11 @@ class Message
*/
protected array $files = [];

/**
* The message poll.
*/
protected ?Poll $poll = null;

/**
* The message webhook.
*/
Expand Down Expand Up @@ -217,6 +223,10 @@ public function build(): MessageBuilder
$message->addComponent($this->getButtons());
}

if ($this->hasPoll()) {
$message->setPoll($this->poll);
}

if ($this->hasFiles()) {
foreach ($this->files as $file) {
$message->addFileFromContent($file['filename'], $file['content']);
Expand Down Expand Up @@ -1037,6 +1047,38 @@ public function hasButtons(): bool
return ! empty($this->buttons);
}

/**
* Add a poll to the message.
*/
public function poll(string $question, array $answers, int $duration = 24, bool $multiselect = false): self
{
$this->poll = (new Poll($this->bot->discord()))
->setQuestion($question)
->setAnswers($answers)
->setDuration($duration)
->setAllowMultiselect($multiselect);

return $this;
}

/**
* Clear the poll from the message.
*/
public function clearPoll(): self
{
$this->poll = null;

return $this;
}

/**
* Determine if the message has a poll.
*/
public function hasPoll(): bool
{
return ! is_null($this->poll);
}

/**
* Set the interaction route prefix.
*/
Expand Down

0 comments on commit 6ad0a07

Please sign in to comment.