Class: Discordrb::Poll::Answer

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/data/poll.rb

Overview

A selectable poll answer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idInteger (readonly)

Returns the ID of the poll answer.

Returns:

  • (Integer)

    the ID of the poll answer.



142
143
144
# File 'lib/discordrb/data/poll.rb', line 142

def id
  @id
end

#message_idInteger (readonly)

Returns the ID of the message the answer is from.

Returns:

  • (Integer)

    the ID of the message the answer is from.



149
150
151
# File 'lib/discordrb/data/poll.rb', line 149

def message_id
  @message_id
end

#votesInteger (readonly) Also known as: vote_count

Returns the number of votes the answer has.

Returns:

  • (Integer)

    the number of votes the answer has.



145
146
147
# File 'lib/discordrb/data/poll.rb', line 145

def votes
  @votes
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check if two poll answers are equivalent.

Parameters:

  • other (Object)

    The object to compare the poll answer against.

Returns:

  • (true, false)

    Whether or not the poll answers are equivalent.



210
211
212
213
214
# File 'lib/discordrb/data/poll.rb', line 210

def ==(other)
  return false unless other.is_a?(Answer)

  @message_id == other.message_id && @id == other.id
end

#emojiEmoji?

Get the emoji of the poll answer.

Returns:

  • (Emoji, nil)

    The emoji of the poll answer.



170
171
172
# File 'lib/discordrb/data/poll.rb', line 170

def emoji
  @media.emoji
end

#textString

Get the text of the poll answer.

Returns:

  • (String)

    The text of the poll answer.



164
165
166
# File 'lib/discordrb/data/poll.rb', line 164

def text
  @media.text
end

#voters(after: nil, limit: 100) ⇒ Array<User>

Get the users who voted for the poll answer.

Parameters:

  • after (Time, #resolve_id, nil) (defaults to: nil)

    The ID or timestamp to start fetching voters from.

  • limit (Integer, nil) (defaults to: 100)

    The maximum number of voters to fetch, or nil to fetch all the voters.

Returns:

  • (Array<User>)

    The users who voted for the poll answer; ordered by user ID in ascending order.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/discordrb/data/poll.rb', line 184

def voters(after: nil, limit: 100)
  stable = @poll.nil? || @poll.finalised?
  channel_id = @channel_id || @poll.message.channel.id
  after_time = after.is_a?(Time) ? IDObject.synthesise(after) : after&.resolve_id

  get_users = lambda do |limit, after|
    data = API::Channel.get_poll_voters(@bot.token, channel_id, @message_id, @id, after:, limit:)
    JSON.parse(data)['users'].collect { |poll_voter_data| @bot.ensure_user(poll_voter_data) }
  end

  return get_users.call(limit, after_time) if limit && limit <= 100

  paginator = Paginator.new(limit, :down) do |last_page|
    if stable && last_page && last_page.count < 100
      []
    else
      get_users.call(100, last_page&.last&.id || after_time)
    end
  end

  paginator.to_a
end

#winner?true, false

Check if the poll answer won the poll.

Returns:

  • (true, false)

    Whether or not the poll answer is the winner.



176
177
178
# File 'lib/discordrb/data/poll.rb', line 176

def winner?
  @poll.nil? || @poll.winner&.id == @id
end