Class: Discordrb::Poll

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#answersArray<Answer> (readonly)

Returns the answers of the poll.

Returns:

  • (Array<Answer>)

    the answers of the poll.



15
16
17
# File 'lib/discordrb/data/poll.rb', line 15

def answers
  @answers
end

#closes_atTime? (readonly)

Returns the time at when the poll will expire.

Returns:

  • (Time, nil)

    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

#finalisedtrue, false (readonly) Also known as: finalised?, finalized?

Returns whether or not Discord has precisely counted the votes yet.

Returns:

  • (true, false)

    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

#layoutInteger (readonly)

Returns the layout type of the poll.

Returns:

  • (Integer)

    the layout type of the poll.



12
13
14
# File 'lib/discordrb/data/poll.rb', line 12

def layout
  @layout
end

#messageMessage (readonly)

Returns the message linked to the poll.

Returns:

  • (Message)

    the message linked to the poll.



18
19
20
# File 'lib/discordrb/data/poll.rb', line 18

def message
  @message
end

#multiselecttrue, false (readonly) Also known as: multiselect?

Returns whether or not users are allowed to vote for multiple answers.

Returns:

  • (true, false)

    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

#questionMedia (readonly)

Returns the display-data for the poll question.

Returns:

  • (Media)

    the display-data for the poll question.



21
22
23
# File 'lib/discordrb/data/poll.rb', line 21

def question
  @question
end

#unknown_resultstrue, 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.

Returns:

  • (true, false)

    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.

Parameters:

  • other (Object)

    The object to compare the poll object against.

Returns:

  • (true, false)

    Whether or not the poll objects are equivalent.



100
101
102
# File 'lib/discordrb/data/poll.rb', line 100

def ==(other)
  other.is_a?(Poll) ? @message == other.message : false
end

#answer(answer_id) ⇒ Answer?

Get a single answer for the poll by its ID.

Parameters:

Returns:

  • (Answer, nil)

    The poll answer, or nil if it couldn't be found.



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

#closeMessage

Prematurely end the poll, only functional for polls created by the current bot.

Returns:

  • (Message)

    The resulting message. Will fail if the poll was not sent by the current bot.

Raises:



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.

Returns:

  • (true, false)

    Whether or not 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.

Returns:

  • (true, false)

    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_votesInteger

Get the total amount of votes cast on the poll.

Returns:

  • (Integer)

    The total amount of votes that the poll received.



57
58
59
# File 'lib/discordrb/data/poll.rb', line 57

def total_votes
  @answers.sum(&:votes)
end

#winnerAnswer?

Get the poll answer that has the most amount of votes.

Returns:

  • (Answer, nil)

    The winning poll answer, or nil for no winner.



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