Skip to content

Commit

Permalink
Add documentation for Poll helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed Apr 14, 2024
1 parent 22ee041 commit 50a7faf
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/nostrum/struct/message/poll.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Nostrum.Struct.Message.Poll do

alias Nostrum.Util

alias Nostrum.Snowflake
alias Nostrum.Struct.Message.Poll.{Answer, MediaObject, Results}

@derive Jason.Encoder
Expand Down Expand Up @@ -76,6 +77,27 @@ defmodule Nostrum.Struct.Message.Poll do
struct(__MODULE__, new)
end

@doc ~S"""
Helper method to create a new poll.
Accepts a `question_text` parameter which is the string to use as the poll title.
Keyword arguments:
- duration: duration (in hours) the poll should be open for
- allow_multiselect: whether users should be able to select multiple answers
You can also pass an `answers` key with answers, though `put_answer/2` and `put_answer/3` are advised.
## Examples
```elixir
poll = Poll.create_poll("Do you enjoy pineapple on pizza?", duration: 2, allow_multiselect: false)
|> Poll.put_answer("Yes!", default_emoji: "\u2705") # check mark emoji
|> Poll.put_answer("No!", default_emoji: "\u274C") # cross emoji
Api.create_message(channel_id, poll: poll)
```
"""
@spec create_poll(String.t(), duration: duration, allow_multiselect: allow_multiselect) :: t()
def create_poll(question_text, duration: duration, allow_multiselect: allow_multiselect) do
%__MODULE__{
Expand Down Expand Up @@ -108,6 +130,14 @@ defmodule Nostrum.Struct.Message.Poll do
Map.update(poll, :answers, [answer], fn answers -> answers ++ [answer] end)
end

@doc ~S"""
Add an answer to the provided poll, see `create_poll/2` for a code sample of this function.
Takes a required "answer" text field, as well as either of the optional arguments:
- `custom_emoji`: An integer representing the snowflake of an emoji to display with the option
- `default_emoji`: A default platform emoji represented as a unicode character
"""
@spec put_answer(Poll.t(), String.t()) :: Poll.t()
def put_answer(poll, answer) do
new_answer = %Answer{
poll_media: %MediaObject{
Expand All @@ -118,6 +148,7 @@ defmodule Nostrum.Struct.Message.Poll do
add_answer(poll, new_answer)
end

@spec put_answer(Poll.t(), String.t(), custom_emoji: Snowflake.t()) :: Poll.t()
def put_answer(poll, answer, custom_emoji: custom_emoji) do
new_answer = %Answer{
poll_media: %MediaObject{
Expand All @@ -131,6 +162,7 @@ defmodule Nostrum.Struct.Message.Poll do
add_answer(poll, new_answer)
end

@spec put_answer(Poll.t(), String.t(), default_emoji: String.t()) :: Poll.t()
def put_answer(poll, answer, default_emoji: default_emoji) do
new_answer = %Answer{
poll_media: %MediaObject{
Expand Down

0 comments on commit 50a7faf

Please # to comment.