class Caldera::Player

Constants

LOGGER

@visibility private

Attributes

client[R]

@return [Client]

guild_id[R]

@return [String]

node[R]

@return [Node] The node that owns this player.

now_playing[R]

@return [Track]

paused[R]

@return [true, false]

position[R]

@return [Integer]

time[R]

@return [Time]

track[R]

@return [Track]

volume[R]

@return [Integer]

Public Class Methods

new(guild_id, node, client) click to toggle source
# File lib/caldera/player.rb, line 40
def initialize(guild_id, node, client)
  @guild_id = guild_id
  @node = node
  @client = client
  @volume = 100
  @paused = false
  @position = 0
  @time = 0

  register_node_handlers
end

Public Instance Methods

destroy() click to toggle source

Destroy this player

# File lib/caldera/player.rb, line 110
def destroy
  send_packet(:destroy)
end
equalizer(**bands) click to toggle source

Adjust the gain of bands. @example

player.equalizer(1 => 0.25, 5 => -0.25, 10 => 0.0)
# File lib/caldera/player.rb, line 101
def equalizer(**bands)
  send_packet(:equalizer, {
    bands: bands.collect do |band,gain| 
      { band: band.to_i, gain: gain.to_f }
    end
  })
end
load_tracks(...) click to toggle source

See Node#load_tracks

# File lib/caldera/player.rb, line 121
def load_tracks(...)
  @node.load_tracks(...)
end
pause() click to toggle source

Pause playback.

# File lib/caldera/player.rb, line 69
def pause
  send_packet(:pause, {
    pause: true
  })
end
play(track, start_time: 0, end_time: 0) click to toggle source

Play a track. @param [String, Track] Either a base64 encoded track, or a {Track} object. @param [Integer] start_time The time in milliseconds to begin playback at. @param [Integer] end_time The time in milliseconds to end at.

# File lib/caldera/player.rb, line 56
def play(track, start_time: 0, end_time: 0)      
  @paused = false
  @track = track
  
  send_packet(:play, {
     track: track.is_a?(Model::Track) ? track.track_data : track,
     startTime: start_time,
     endTime: end_time,
     noReplace: false
  })
end
seek(position) click to toggle source

Seek to a position in the track. @param [Integer] position The position to seek to, in milliseconds.

# File lib/caldera/player.rb, line 84
def seek(position)
  send_packet(:seek, {
    position: position
  })
end
state=(new_state) click to toggle source

@visibility private

# File lib/caldera/player.rb, line 115
def state=(new_state)
  @time = Time.at(new_state['time'])
  @position = new_state['position']
end
unpause() click to toggle source

Resume the player.

# File lib/caldera/player.rb, line 76
def unpause
  send_packet(:pause, {
    pause: false
  })
end

Private Instance Methods

handle_track_end(data) click to toggle source
# File lib/caldera/player.rb, line 146
def handle_track_end(data)
  LOGGER.debug { "Track ended for #{@guild_id}" }

  @track = nil
  emit(:track_end, Events::TrackEnd.new(data, self))
end
handle_track_exception(data) click to toggle source
# File lib/caldera/player.rb, line 153
def handle_track_exception(data)
  LOGGER.debug { "Track exception for #{@guild_id}" }
  @track = nil
  emit(:track_exception, Events::TrackException.new(data, self))
end
handle_track_start(data) click to toggle source
# File lib/caldera/player.rb, line 141
def handle_track_start(data)
  LOGGER.debug { "Track started for #{@guild_id}" }
  emit(:track_start, Events::TrackStart.new(data, self))
end
handle_track_stuck(data) click to toggle source
# File lib/caldera/player.rb, line 159
def handle_track_stuck(data)
  LOGGER.debug { "Track stuck for #{@guild_id}" }
  @track = nil
  emit(:track_stuck, Events::TrackStuck.new(data, self))
end
handle_websocket_closed(data) click to toggle source
# File lib/caldera/player.rb, line 165
def handle_websocket_closed(data)
  LOGGER.warn { "WebSocket closed for #{@guild_id}" }
  @track = nil
  emit(:websocket_closed, Events::WebSocketClosed.new(data, self))
end
register_node_handlers() click to toggle source
# File lib/caldera/player.rb, line 133
def register_node_handlers
  node.on(:track_start, &method(:handle_track_start))
  node.on(:track_end, &method(:handle_track_end))
  node.on(:track_exception, &method(:handle_track_exception))
  node.on(:track_stuck, &method(:handle_track_stuck))
  node.on(:websocket_closed, &method(:handle_websocket_closed))
end
send_packet(op, data={}) click to toggle source
# File lib/caldera/player.rb, line 127
def send_packet(op, data={})
packet = { op: op, guildId: guild_id }.merge(data)
LOGGER.debug { "Sending packet to node: #{packet}"}
  @node.send_json(packet)
end