Module: OnyxCord::Bot::VoiceControl

Included in:
OnyxCord::Bot
Defined in:
lib/onyxcord/core/bot/voice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#voicesHash<Integer => Client> (readonly)

Returns the voice connections this bot currently has, by the server ID to which they are connected.

Returns:

  • (Hash<Integer => Client>)

    the voice connections this bot currently has, by the server ID to which they are connected.



7
8
9
# File 'lib/onyxcord/core/bot/voice.rb', line 7

def voices
  @voices
end

Instance Method Details

#voice(thing) ⇒ Voice::Client?

Gets the voice bot for a particular server or channel. You can connect to a new channel using the #voice_connect method.

Parameters:

  • thing (Channel, Server, Integer)

    the server or channel you want to get the voice bot for, or its ID.

Returns:

  • (Voice::Client, nil)

    the Client for the thing you specified, or nil if there is no connection yet



13
14
15
16
17
18
19
20
21
22
# File 'lib/onyxcord/core/bot/voice.rb', line 13

def voice(thing)
  id = thing.resolve_id
  return @voices[id] if @voices[id]

  channel = channel(id)
  return nil unless channel

  server_id = channel.server.id
  return @voices[server_id] if @voices[server_id]
end

#voice_connect(chan, encrypted = true) ⇒ Voice::Client

Connects to a voice channel, initializes network connections and returns the Voice::Client over which audio data can then be sent. After connecting, the bot can also be accessed using #voice. If the bot is already connected to voice, the existing connection will be terminated - you don't have to call Voice::Client#destroy before calling this method.

Parameters:

  • chan (Channel, String, Integer)

    The voice channel, or its ID, to connect to.

  • encrypted (true, false) (defaults to: true)

    Whether voice communication should be encrypted using (uses an XSalsa20 stream cipher for encryption and Poly1305 for authentication)

Returns:

  • (Voice::Client)

    the initialized bot over which audio data can then be sent.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/onyxcord/core/bot/voice.rb', line 32

def voice_connect(chan, encrypted = true)
  raise ArgumentError, 'Unencrypted voice connections are no longer supported.' unless encrypted

  chan = channel(chan.resolve_id)
  server_id = chan.server.id

  if @voices[chan.id]
    debug('Voice bot exists already! Destroying it')
    @voices[chan.id].destroy
    @voices.delete(chan.id)
  end

  debug("Got voice channel: #{chan}")

  @should_connect_to_voice[server_id] = chan
  @gateway.send_voice_state_update(server_id.to_s, chan.id.to_s, false, false)

  debug('Voice channel init packet sent! Now waiting.')

  Internal::AsyncRuntime.sleep(0.05) until @voices[server_id]
  debug('Voice connect succeeded!')
  @voices[server_id]
end

#voice_destroy(server, destroy_vws = true) ⇒ Object

Disconnects the client from a specific voice connection given the server ID. Usually it's more convenient to use Voice::Client#destroy rather than this.

Parameters:

  • server (Server, String, Integer)

    The server, or server ID, the voice connection is on.

  • destroy_vws (true, false) (defaults to: true)

    Whether or not the VWS should also be destroyed. If you're calling this method directly, you should leave it as true.



61
62
63
64
65
66
# File 'lib/onyxcord/core/bot/voice.rb', line 61

def voice_destroy(server, destroy_vws = true)
  server = server.resolve_id
  @gateway.send_voice_state_update(server.to_s, nil, false, false)
  @voices[server].destroy if @voices[server] && destroy_vws
  @voices.delete(server)
end