Class: Discordrb::Poll
- Inherits:
-
Object
- Object
- Discordrb::Poll
- Defined in:
- lib/discordrb/data/poll.rb
Overview
A surface with selectable answers.
Defined Under Namespace
Classes: Answer, Builder, Media, Result
Constant Summary collapse
- LAYOUTS =
Mapping of layout types for polls.
{ default: 1 }.freeze
Instance Attribute Summary collapse
-
#answers ⇒ Array<Answer>
readonly
The answers of the poll.
-
#closes_at ⇒ Time?
readonly
The time at when the poll will expire.
-
#finalised ⇒ true, false
(also: #finalised?, #finalized?)
readonly
Whether or not Discord has precisely counted the votes yet.
-
#layout ⇒ Integer
readonly
The layout type of the poll.
-
#message ⇒ Message
readonly
The message linked to the poll.
-
#multiselect ⇒ true, false
(also: #multiselect?)
readonly
Whether or not users are allowed to vote for multiple answers.
-
#question ⇒ Media
readonly
The display-data for the poll question.
-
#unknown_results ⇒ true, false
(also: #unknown_results?)
readonly
Whether or not Discord did not fetch the results for the poll.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check if two poll objects are equivalent.
-
#answer(answer_id) ⇒ Answer?
Get a single answer for the poll by its ID.
-
#close ⇒ Message
Prematurely end the poll, only functional for polls created by the current bot.
-
#closed? ⇒ true, false
Check if the poll has closed.
-
#default_layout? ⇒ true, false
Whether or not the poll is using the default layout.
-
#total_votes ⇒ Integer
Get the total amount of votes cast on the poll.
-
#winner ⇒ Answer?
Get the poll answer that has the most amount of votes.
Instance Attribute Details
#answers ⇒ Array<Answer> (readonly)
Returns the answers of the poll.
15 16 17 |
# File 'lib/discordrb/data/poll.rb', line 15 def answers @answers end |
#closes_at ⇒ Time? (readonly)
Returns the time at when the poll will expire.
24 25 26 |
# File 'lib/discordrb/data/poll.rb', line 24 def closes_at @closes_at end |
#finalised ⇒ true, false (readonly) Also known as: finalised?, finalized?
Returns whether or not Discord has precisely counted the votes yet.
27 28 29 |
# File 'lib/discordrb/data/poll.rb', line 27 def finalised @finalised end |
#layout ⇒ Integer (readonly)
Returns the layout type of the poll.
12 13 14 |
# File 'lib/discordrb/data/poll.rb', line 12 def layout @layout end |
#message ⇒ Message (readonly)
Returns the message linked to the poll.
18 19 20 |
# File 'lib/discordrb/data/poll.rb', line 18 def @message end |
#multiselect ⇒ true, false (readonly) Also known as: multiselect?
Returns whether or not users are allowed to vote for multiple answers.
32 33 34 |
# File 'lib/discordrb/data/poll.rb', line 32 def multiselect @multiselect end |
#question ⇒ Media (readonly)
Returns the display-data for the poll question.
21 22 23 |
# File 'lib/discordrb/data/poll.rb', line 21 def question @question end |
#unknown_results ⇒ true, false (readonly) Also known as: unknown_results?
Returns whether or not Discord did not fetch the results for the poll.
When this is true, the #finalised? method will always return a value of false,
and Discordrb::Poll::Answer#votes will always return a value of 0.
38 39 40 |
# File 'lib/discordrb/data/poll.rb', line 38 def unknown_results @unknown_results end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Check if two poll objects are equivalent.
100 101 102 |
# File 'lib/discordrb/data/poll.rb', line 100 def ==(other) other.is_a?(Poll) ? @message == other. : false end |
#answer(answer_id) ⇒ Answer?
Get a single answer for the poll by its ID.
64 65 66 67 68 69 70 71 72 |
# File 'lib/discordrb/data/poll.rb', line 64 def answer(answer_id) answer_id = if answer_id.is_a?(Answer) answer_id.id else answer_id.resolve_id end @answers.find { |answer| answer.id == answer_id } end |
#close ⇒ Message
Prematurely end the poll, only functional for polls created by the current bot.
91 92 93 94 95 |
# File 'lib/discordrb/data/poll.rb', line 91 def close raise Discordrb::Errors::NoPermission, 'Cannot close the poll' if !@message.from_bot? || closed? Message.new(JSON.parse(API::Channel.end_poll(@bot.token, @message.channel.id, @message.id)), @bot) end |
#closed? ⇒ true, false
Check if the poll has closed.
76 77 78 |
# File 'lib/discordrb/data/poll.rb', line 76 def closed? @finalised || (!@closes_at.nil? && Time.now > @closes_at) end |
#default_layout? ⇒ true, false
Returns whether or not the poll is using the default layout.
108 109 110 |
# File 'lib/discordrb/data/poll.rb', line 108 LAYOUTS.each do |name, value| define_method("#{name}_layout?") { @layout == value } end |
#total_votes ⇒ Integer
Get the total amount of votes cast on the poll.
57 58 59 |
# File 'lib/discordrb/data/poll.rb', line 57 def total_votes @answers.sum(&:votes) end |
#winner ⇒ Answer?
Get the poll answer that has the most amount of votes.
82 83 84 85 86 |
# File 'lib/discordrb/data/poll.rb', line 82 def winner return unless (max = @answers.max_by(&:votes))&.votes&.nonzero? @answers.one? { |answer| answer.votes == max&.votes } ? max : nil end |