rmcafee / discord_ex

Discord Elixir Library
MIT License
48 stars 13 forks source link

Unable to start the bot #25

Closed rekyuu closed 7 years ago

rekyuu commented 7 years ago

Hiya, I'm pretty sure I'm just screwed up somewhere but I wanted a second pair of eyes to see why this isn't starting. I don't have anything crazy in the bot, just having it start with a supervisor and then have it parse events.

My repo is at https://github.com/KumaKaiNi/discord-kuma. The error I'm getting is as follows:

== Compilation error on file lib/discord_kuma/bot.ex ==
** (exit) exited in: :gen_server.call(:hackney_manager, {:new_request, #PID<0.172.0>, #Reference<0.0.1.773>, {:client, :undefined, {:metrics_ng, :metrics_dummy}, :hackney_ssl_transport, 'discordapp.com', 443, "discordapp.com", [], nil, nil, nil, true, :hackney_pool, 5000, false, 5, false, 5, nil, nil, nil, :undefined, :start, nil, :normal, false, false, false, :undefined, false, nil, :waiting, nil, 4096, "", [], :undefined, nil, nil, nil, nil, :undefined, nil}}, :infinity)
    ** (EXIT) no process

01:06:52.059 [error] GenServer #PID<0.172.0> terminating
** (stop) exited in: :gen_server.call(:hackney_manager, {:new_request, #PID<0.172.0>, #Reference<0.0.1.773>, {:client, :undefined, {:metrics_ng, :metrics_dummy}, :hackney_ssl_transport, 'discordapp.com', 443, "discordapp.com", [], nil, nil, nil, true, :hackney_pool, 5000, false, 5, false, 5, nil, nil, nil, :undefined, :start, nil, :normal, false, false, false, :undefined, false, nil, :waiting, nil, 4096, "", [], :undefined, nil, nil, nil, nil, :undefined, nil}}, :infinity)
    ** (EXIT) no process
    (stdlib) gen_server.erl:212: :gen_server.call/3
    c:/Users/bowan/dev/elixir/discord_kuma/deps/hackney/src/hackney_manager.erl:66: :hackney_manager.init_request/1
    c:/Users/bowan/dev/elixir/discord_kuma/deps/hackney/src/hackney_manager.erl:56: :hackney_manager.new_request/1
    c:/Users/bowan/dev/elixir/discord_kuma/deps/hackney/src/hackney_connect.erl:184: :hackney_connect.socket_from_pool/4
    c:/Users/bowan/dev/elixir/discord_kuma/deps/hackney/src/hackney_connect.erl:36: :hackney_connect.connect/5
    c:/Users/bowan/dev/elixir/discord_kuma/deps/hackney/src/hackney.erl:328: :hackney.request/5
    lib/httpoison/base.ex:402: HTTPoison.Base.request/9
    lib/discord_ex/rest_client/rest.ex:5: DiscordEx.Connections.REST.request!/5
Last message: {:resource, :get, "users/@me", nil}
State: %{token: "nnnnnooooooolmao"}

The code I'm using to start the bot:

defmodule DiscordKuma.Bot do
  {:ok, _bot_client} = DiscordEx.Client.start_link(%{
    token: Application.get_env(:discord_kuma, :discord_token),
    handler: DiscordKuma.Module
  })
end

Thanks for the help.

rekyuu commented 7 years ago

I also tried working backwards with the Rest API and got

iex(6)> {:ok, rest} = DiscordEx.RestClient.start_link(%{token: token})
{:ok, #PID<0.272.0>}
iex(7)> DiscordEx.RestClient.Resources.User.current(rest)
%{"code" => 0, "message" => "401: Unauthorized"}
rekyuu commented 7 years ago

Looking through the most recent commit it seems there is code that was not installed with Hex, likely a version bump and Hex update issue. Regardless I tried the fix of adding Bot to the beginning of token and I still get the same error as I did in the first comment. It does however work for the Rest client when I do the new token with the same code as I did in post 2. If I remove the following and start iex and run Client.start_link manually it also works, so it seems to be an issue with genserver and hackney while it's compiling. While running it manually I sent my bot a message and got the following:

03:11:46.803 [error] ** Websocket client DiscordEx.Client terminating in :websocket_handle/3
   for the reason :error:%Protocol.UndefinedError{description: "", protocol: String.Chars,
 value: %{data: %{"attachments" => [],
     "author" => %{"avatar" => "b5290cf391d4482364d1c7b1f0050031",
       "discriminator" => "4390", "id" => 107977662680571904,
       "username" => "rekyuu"}, "channel_id" => 234550087345373184,
     "content" => "test", "edited_timestamp" => nil, "embeds" => [],
     "id" => 234603808578076673, "mention_everyone" => false,
     "mention_roles" => [], "mentions" => [], "nonce" => "234603808586465280",
     "pinned" => false, "timestamp" => "2016-10-09T09:11:46.693000+00:00",
     "tts" => false, "type" => 0}, event_name: :MESSAGE_CREATE, op: :dispatch,
   seq_num: 4}}
** Websocket message was {:binary,
 <<131, 116, 0, 0, 0, 4, 100, 0, 1, 100, 116, 0, 0, 0, 15, 109, 0, 0, 0, 11, 97,
   116, 116, 97, 99, 104, 109, 101, 110, 116, 115, 106, 109, 0, 0, 0, 6, 97,
   117, 116, 104, 111, 114, 116, 0, 0, 0, 4, 109, ...>>}
** Handler state was %{agent_seq_num: #PID<0.252.0>, client_id: 234548552053817345,
  client_pid: #PID<0.251.0>,
  guilds: [%{guild_id: 214268737887404042, voice_states: []}],
  handler: DiscordKuma.Module, rest_client: #PID<0.244.0>,
  session_id: "a46e7dd2563c69570bf076ffbce9d6e7",
  token: "Bot nopelol"}
** Stacktrace: [{String.Chars, :impl_for!, 1, [file: 'lib/string/chars.ex', line: 3]},
 {String.Chars, :to_string, 1, [file: 'lib/string/chars.ex', line: 17]},
 {DiscordKuma.Module, :handle_event, 2,
  [file: 'lib/discord_kuma/module.ex', line: 3]},
 {:websocket_client, :handle_websocket_frame, 2,
  [file: 'c:/Users/bowan/dev/elixir/discord_kuma/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]}]

iex(4)> ** (EXIT from #PID<0.241.0>) %Protocol.UndefinedError{description: "", protocol: String.Chars, value: %{data: %{"attachments" => [], "author" => %{"avatar" => "b5290cf391d4482364d1c7b1f0050031", "discriminator" => "4390", "id" => 107977662680571904, "username" => "rekyuu"}, "channel_id" => 234550087345373184, "content" => "test", "edited_timestamp" => nil, "embeds" => [], "id" => 234603808578076673, "mention_everyone" => false, "mention_roles" => [], "mentions" => [], "nonce" => "234603808586465280", "pinned" => false, "timestamp" => "2016-10-09T09:11:46.693000+00:00", "tts" => false, "type" => 0}, event_name: :MESSAGE_CREATE, op: :dispatch, seq_num: 4}}
iex(4)>
iex(4)>
03:11:46.825 [error] ** State machine #PID<0.251.0> terminating
** Last message in was {:ssl,
 {:sslsocket, {:gen_tcp, #Port<0.11321>, :tls_connection, :undefined},
  #PID<0.254.0>},
 <<130, 126, 1, 239, 131, 116, 0, 0, 0, 4, 100, 0, 1, 100, 116, 0, 0, 0, 15,
   109, 0, 0, 0, 11, 97, 116, 116, 97, 99, 104, 109, 101, 110, 116, 115, 106,
   109, 0, 0, 0, 6, 97, 117, 116, 104, 111, 114, 116, ...>>}
** When State == :connected
**      Data  == {:context,
 {:websocket_req, :wss, 'gateway.discord.gg', 443, '/?v=4&encoding=etf',
  :infinity, :undefined, 1,
  {:sslsocket, {:gen_tcp, #Port<0.11321>, :tls_connection, :undefined},
   #PID<0.254.0>},
  {:transport, :ssl, :ssl, :ssl_closed, :ssl_error,
   [mode: :binary, active: true, verify: :verify_none, packet: 0]},
  "yNmspc/KzpHBNGidgxSuwg==", :undefined, 1, 2, :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.252.0>, client_id: 234548552053817345,
    client_pid: #PID<0.251.0>,
    guilds: [%{guild_id: 214268737887404042, voice_states: []}],
    handler: DiscordKuma.Module, rest_client: #PID<0.244.0>,
    session_id: "a46e7dd2563c69570bf076ffbce9d6e7",
    token: "Bot nopelol"}},
 "", false, 0}
** Reason for termination =
** %Protocol.UndefinedError{description: "", protocol: String.Chars,
 value: %{data: %{"attachments" => [],
     "author" => %{"avatar" => "b5290cf391d4482364d1c7b1f0050031",
       "discriminator" => "4390", "id" => 107977662680571904,
       "username" => "rekyuu"}, "channel_id" => 234550087345373184,
     "content" => "test", "edited_timestamp" => nil, "embeds" => [],
     "id" => 234603808578076673, "mention_everyone" => false,
     "mention_roles" => [], "mentions" => [], "nonce" => "234603808586465280",
     "pinned" => false, "timestamp" => "2016-10-09T09:11:46.693000+00:00",
     "tts" => false, "type" => 0}, event_name: :MESSAGE_CREATE, op: :dispatch,
   seq_num: 4}}

Dunno how I managed to completely destroy this library but I hope I've given enough info to see what's going on. lol

rekyuu commented 7 years ago

Got it working, turns out HTTPoison wasn't starting in application for whatever reason so I manually invoked it:

defmodule DiscordKuma do
  require Logger

  def start(_type, _args) do
    Logger.debug "Starting HTTPoison..."
    HTTPoison.start

    Logger.debug "Starting bot..."
    DiscordEx.Client.start_link(%{
      token: Application.get_env(:discord_kuma, :discord_token),
      handler: DiscordKuma.Module
    })
  end
end

Thanks!