rmcafee / discord_ex

Discord Elixir Library
MIT License
48 stars 13 forks source link

[Solved, pending PR] not working - websockets errors with :badarg #7

Closed Marqin closed 8 years ago

Marqin commented 8 years ago

When someone writes to channel/on DM:

13:54:58.871 [error] ** Websocket client DiscordEx.Client terminating in :websocket_handle/3
   for the reason :error::badarg
** Websocket message was {:binary,
 <<REMOVED_SECRET_DATA>>}
** Handler state was %{agent_seq_num: #PID<0.185.0>, client_pid: #PID<0.184.0>,
  guild_id: REMOVED_SECRET_DATA, handler: "REMOVED_SECRET_DATA", rest_client: #PID<0.177.0>,
  session_id: "REMOVED_SECRET_DATA",
  token: "REMOVED_SECRET_DATA",
  voice_states: []}
** Stacktrace: [{:erlang, :apply, ["REMOVED_SECRET_DATA", :handle_event, []], []},
 {DiscordEx.Client, :websocket_handle, 3,
  [file: 'lib/discord_ex/client/client.ex', line: 105]},
 {:websocket_client, :handle_websocket_frame, 2,
  [file: 'REMOVED_SECRET_DATA/deps/websocket_client/src/websocket_client.erl',
   line: 448]}, {:gen_fsm, :handle_msg, 7, [file: 'gen_fsm.erl', line: 451]},
 {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]

13:54:58.907 [error] ** State machine #PID<0.184.0> terminating
** Last message in was {:ssl,
 {:sslsocket, {:gen_tcp, #Port<0.6699>, :tls_connection, :undefined},
  #PID<0.186.0>},
 <<REMOVED_SECRET_DATA>>}
** When State == :connected
**      Data  == {:context,
 {:websocket_req, :wss, 'gateway.discord.gg', 443, '/?v=4&encoding=etf',
  :infinity, :undefined, 1,
  {:sslsocket, {:gen_tcp, #Port<0.6699>, :tls_connection, :undefined},
   #PID<0.186.0>},
  {:transport, :ssl, :ssl, :ssl_closed, :ssl_error,
   [mode: :binary, active: true, verify: :verify_none, packet: 0]},
  "REMOVED_SECRET_DATA", :undefined, 1, :undefined, :undefined,
  :undefined},
 {:transport, :ssl, :ssl, :ssl_closed, :ssl_error,
  [mode: :binary, active: true, verify: :verify_none, packet: 0]}, [],
 {:wss, 'gateway.discord.gg', 443, '/?v=4&encoding=etf'},
 {DiscordEx.Client,
  %{agent_seq_num: #PID<0.185.0>, client_pid: #PID<0.184.0>,
    guild_id: REMOVED_SECRET_DATA, handler: "REMOVED_SECRET_DATA",
    rest_client: #PID<0.177.0>, session_id: "REMOVED_SECRET_DATA",
    token: "REMOVED_SECRET_DATA",
    voice_states: []}}, "", false, 0}
** Reason for termination =
** :badarg

Bot code (part under supervisor):

defmodule REMOVED_SECRET_DATA.Worker do
  require Logger
  require DiscordEx

  def start_link(opts \\ []) do
    DiscordEx.Client.start_link(opts)
  end

  def handle_event({event, _payload}, state) do
    Logger.info "Received Event: #{event}"
    {:ok, state}
  end

  def handle_event({:message_create, payload}, state) do
    # Your stuff happens here #
    {:ok, state}
  end
end

I've changed sensitive data to REMOVED_SECRET_DATA.

Marqin commented 8 years ago

When there is no/bad token provided I also get this huge error (just there is no session_id), so it seems it's not only for normal messages, but for all events

Marqin commented 8 years ago

I have injected IO.puts before https://github.com/rmcafee/discord_ex/blob/master/lib/discord_ex/client/client.ex#L105 and event, payload, state seem to be ok.

inspect(state[:handler]) gives me handler"BotName" - is it ok?

Marqin commented 8 years ago

Ok, I found. It's bug in README.

handler given to bot should not be it's name. It should be module name of module that implements event handling. Gonna make some PR.