Kraigie / nostrum

Elixir Discord Library
https://kraigie.github.io/nostrum/
MIT License
616 stars 129 forks source link

Unable to reproduce AudioPlayer example from docs #337

Closed on3dd closed 2 years ago

on3dd commented 3 years ago

Trying to reproduce the example with voice channels from the documentation, but after starting the audio playback, the bot immediately stops speaking.

Logs:

17:39:47.649 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: true}

17:39:51.591 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: false}

Source code: https://github.com/on3dd/berezovskiy

What could be the problem? Thank you in advance.

BrandtHill commented 3 years ago

Within the example what command are running to generate audio?

on3dd commented 3 years ago

If I understood you correctly:

BrandtHill commented 3 years ago

I suspect it may be a networking issue with youtube-dl. Replace @soundcloud_url with a path to a local audio file you have on your computer and replace with :ytdl with :url

on3dd commented 3 years ago

Sorry for late response, I tried your suggestion, but it didn't solve the problem. With the following code:

defmodule Berezovskiy.Consumer.MessageCreate do
  ...

  @local_mp3_url "~/code/berezovskiy/Kalimba.mp3"

  ...

  def handle(msg) do
    ...

    "!play song" ->
        if Voice.ready?(msg.guild_id) do
          Voice.play(msg.guild_id, @local_mp3_url, :url)
        else
          do_not_ready_msg(msg)
        end

    ...
  end
end

I still get the following logs:

20:21:50.510 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: true}

20:21:50.718 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: false}

and the bot still doesn't speak 😞

BrandtHill commented 3 years ago

It looks like you’re using the latest nostrum release from hex. Try using the master branch from git; the pending 0.5 release has a ton of fixes/new features for voice. Additionally, once you’ve switched, you may also try passing the options realtime: false as the 4th argument to the play/4 function.

on3dd commented 3 years ago

Tried to change release to master branch from git, also tried to set realtime: false, but both of them gave me the same results: they only added new field to voice logs.

Logs now:

20:10:35.367 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: true, timed_out: false}

20:10:35.570 [debug] VOICE SPEAKING UPDATE %Nostrum.Struct.Event.SpeakingUpdate{channel_id: 580931507712032792, guild_id: 580931507288539177, speaking: false, timed_out: true}
BrandtHill commented 3 years ago

Something doesn't seem right that the time between those logs is 200ms. Please completely nuke your dependencies and re-pull all of them and recompile them. Additionally, make sure your ffmpeg is binary is up to date.

tionis commented 2 years ago

Can confirm, I had the same or rather a similar issue but using the master branch and realtime: false fixed it for me.

chanphiromsok commented 2 years ago

I had this issued too After play music from youtube it sound about 2sec then speaking turn to false

jchristgit commented 2 years ago

I'm closing this for now pending response, feel free to re-open if you're still encountering the issue with realtime: false on the most recent Nostrum version 0.5.0-rc2.

sylvesterroos commented 8 months ago

I'd like to re-open this issue because I'm having the same issue. I make my bot join my voice channel with /summon, and then I use /play song to play the soundcloud url, but it prints in chat that it has to be in a voice channel before being able to play audio. This happens with both the normal Voice.play and when I've added realtime: false as an option: [%{name: "song"}] -> Voice.play(guild_id, @soundcloud_url, :ytdl, realtime: false)

BrandtHill commented 8 months ago

This will occur when your bot joins a voice channel without guild_voice_states gateway intents enabled. https://kraigie.github.io/nostrum/gateway_intents.html

sylvesterroos commented 8 months ago

Well, I'm not sure what is going on, but it's not working for me. Can you confirm that the following code should work? audio_player_bot.zip

In this project I run iex -S mix, AudioPlayerSupervisor.start_link([]), and AudioPlayerConsumer.create_guild_commands(12345) # guild id here. Then I have the bot join with /summon and I test it with /play song, after which it says in chat that it needs to be in a voice channel.

I noticed that I'm getting the following errors in the terminal. I'm assuming it's related.

logs terminating ** (FunctionClauseError) no function clause matching in Nostrum.Voice.Session.terminate/2 (nostrum 0.8.0) lib/nostrum/voice/session.ex:205: Nostrum.Voice.Session.terminate({%Nostrum.Error.CacheError{message: "ERROR: No match for 1168655087745380406 found in Elixir.Nostrum.Cache.ChannelCache"}, [{Nostrum.Util, :bangify_find, 3, [file: ~c"lib/nostrum/util.ex", line: 100]}, {Nostrum.Voice.Session, :handle_continue, 2, [file: ~c"lib/nostrum/voice/session.ex", line: 33]}, {:gen_server, :try_handle_continue, 3, [file: ~c"gen_server.erl", line: 1085]}, {:gen_server, :loop, 7, [file: ~c"gen_server.erl", line: 995]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, nil) (stdlib 5.2) gen_server.erl:1143: :gen_server.try_terminate/3 (stdlib 5.2) gen_server.erl:1339: :gen_server.terminate/10 (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 Last message: {:continue, %Nostrum.Struct.VoiceState{guild_id: 1168655086751338506, channel_id: 1168655087745380406, self_mute: false, self_deaf: false, gateway: "rotterdam3894.discord.media:443", session: "3a71e626bcc59c4853cf4d87df71ac83", token: "965e0b054e038e9d", secret_key: nil, session_pid: nil, ssrc: nil, speaking: nil, ip: nil, port: nil, udp_socket: nil, rtp_sequence: nil, rtp_timestamp: nil, ffmpeg_proc: nil, raw_audio: nil, raw_stateful: nil, current_url: nil, player_pid: nil, persist_source: nil, persist_playback: nil}} State: nil 10:41:30.278 [error] GenServer #PID<0.328.0> terminating ** (FunctionClauseError) no function clause matching in Nostrum.Voice.Session.terminate/2 (nostrum 0.8.0) lib/nostrum/voice/session.ex:205: Nostrum.Voice.Session.terminate({%Nostrum.Error.CacheError{message: "ERROR: No match for 1168655087745380406 found in Elixir.Nostrum.Cache.ChannelCache"}, [{Nostrum.Util, :bangify_find, 3, [file: ~c"lib/nostrum/util.ex", line: 100]}, {Nostrum.Voice.Session, :handle_continue, 2, [file: ~c"lib/nostrum/voice/session.ex", line: 33]}, {:gen_server, :try_handle_continue, 3, [file: ~c"gen_server.erl", line: 1085]}, {:gen_server, :loop, 7, [file: ~c"gen_server.erl", line: 995]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, nil) (stdlib 5.2) gen_server.erl:1143: :gen_server.try_terminate/3 (stdlib 5.2) gen_server.erl:1339: :gen_server.terminate/10 (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 Last message: {:continue, %Nostrum.Struct.VoiceState{guild_id: 1168655086751338506, channel_id: 1168655087745380406, self_mute: false, self_deaf: false, gateway: "rotterdam3894.discord.media:443", session: "3a71e626bcc59c4853cf4d87df71ac83", token: "965e0b054e038e9d", secret_key: nil, session_pid: nil, ssrc: nil, speaking: nil, ip: nil, port: nil, udp_socket: nil, rtp_sequence: nil, rtp_timestamp: nil, ffmpeg_proc: nil, raw_audio: nil, raw_stateful: nil, current_url: nil, player_pid: nil, persist_source: nil, persist_playback: nil}} State: nil 10:41:30.278 [error] GenServer #PID<0.329.0> terminating ** (FunctionClauseError) no function clause matching in Nostrum.Voice.Session.terminate/2 (nostrum 0.8.0) lib/nostrum/voice/session.ex:205: Nostrum.Voice.Session.terminate({%Nostrum.Error.CacheError{message: "ERROR: No match for 1168655087745380406 found in Elixir.Nostrum.Cache.ChannelCache"}, [{Nostrum.Util, :bangify_find, 3, [file: ~c"lib/nostrum/util.ex", line: 100]}, {Nostrum.Voice.Session, :handle_continue, 2, [file: ~c"lib/nostrum/voice/session.ex", line: 33]}, {:gen_server, :try_handle_continue, 3, [file: ~c"gen_server.erl", line: 1085]}, {:gen_server, :loop, 7, [file: ~c"gen_server.erl", line: 995]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, nil) (stdlib 5.2) gen_server.erl:1143: :gen_server.try_terminate/3 (stdlib 5.2) gen_server.erl:1339: :gen_server.terminate/10 (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 Last message: {:continue, %Nostrum.Struct.VoiceState{guild_id: 1168655086751338506, channel_id: 1168655087745380406, self_mute: false, self_deaf: false, gateway: "rotterdam3894.discord.media:443", session: "3a71e626bcc59c4853cf4d87df71ac83", token: "965e0b054e038e9d", secret_key: nil, session_pid: nil, ssrc: nil, speaking: nil, ip: nil, port: nil, udp_socket: nil, rtp_sequence: nil, rtp_timestamp: nil, ffmpeg_proc: nil, raw_audio: nil, raw_stateful: nil, current_url: nil, player_pid: nil, persist_source: nil, persist_playback: nil}} State: nil 10:41:30.278 [error] GenServer #PID<0.330.0> terminating ** (FunctionClauseError) no function clause matching in Nostrum.Voice.Session.terminate/2 (nostrum 0.8.0) lib/nostrum/voice/session.ex:205: Nostrum.Voice.Session.terminate({%Nostrum.Error.CacheError{message: "ERROR: No match for 1168655087745380406 found in Elixir.Nostrum.Cache.ChannelCache"}, [{Nostrum.Util, :bangify_find, 3, [file: ~c"lib/nostrum/util.ex", line: 100]}, {Nostrum.Voice.Session, :handle_continue, 2, [file: ~c"lib/nostrum/voice/session.ex", line: 33]}, {:gen_server, :try_handle_continue, 3, [file: ~c"gen_server.erl", line: 1085]}, {:gen_server, :loop, 7, [file: ~c"gen_server.erl", line: 995]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 241]}]}, nil) (stdlib 5.2) gen_server.erl:1143: :gen_server.try_terminate/3 (stdlib 5.2) gen_server.erl:1339: :gen_server.terminate/10 (stdlib 5.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 Last message: {:continue, %Nostrum.Struct.VoiceState{guild_id: 1168655086751338506, channel_id: 1168655087745380406, self_mute: false, self_deaf: false, gateway: "rotterdam3894.discord.media:443", session: "3a71e626bcc59c4853cf4d87df71ac83", token: "965e0b054e038e9d", secret_key: nil, session_pid: nil, ssrc: nil, speaking: nil, ip: nil, port: nil, udp_socket: nil, rtp_sequence: nil, rtp_timestamp: nil, ffmpeg_proc: nil, raw_audio: nil, raw_stateful: nil, current_url: nil, player_pid: nil, persist_source: nil, persist_playback: nil}} State: nil ```
BrandtHill commented 8 months ago

Could you please try using master? I recall that the hex release may have broken some things related to channel caching.

sylvesterroos commented 8 months ago

Ok, it's working now! Thanks for your time