Allyedge

Allyedge

How to send messages using WebSockex

So, I’m trying to build a Discord library in Elixir and I have to connect to the Discord gateway for that. The connection part works, but when I try to send a message as shown here: Gateway - Documentation - Discord

I get no response back - it also says timeout when I do it manually in iex

defmodule Chemie.Discord.Gateway do
  @moduledoc false

  use WebSockex

  import Chemie.Constants, only: [gateway_url: 0, bot_token: 0]
  require Logger

  @url gateway_url()

  def start_link(state \\ []) do
    WebSockex.start_link(@url, __MODULE__, state, name: __MODULE__)
  end

  def handle_frame({_type, message}, state) do
    json = Jason.decode!(message)

    IO.inspect(json)

    Map.new(~w[d op s t]a, &{&1, json[to_string(&1)]})
    |> handle_discord_message()

    {:ok, state}
  end

  defp handle_discord_message(%{op: 10}) do
    data =
      Jason.encode!(%{
        "op" => 2,
        "d" => %{
          "token" => bot_token(),
          "intents" => 513,
          "properties" => %{
            "$os" => "linux",
            "$browser" => "chemie",
            "$device" => "chemie"
          }
        }
      })

    # This is where I send the message
    WebSockex.send_frame(__MODULE__, {:text, data})
  end

  defp handle_discord_message(data) do
    IO.inspect(data)
  end
end

Am I sending the message in a wrong way?

Thanks!

Most Liked

Graborg

Graborg

Hi!

Did you ever manage to solve this?

If not:
It seems you are using MODULE instead of a pid when calling

WebSockex.send_frame(MODULE, {:text, data})

Using the pid of the Chemie.Discord.Gateway process should do the trick!

I saw now that you correctly name your WebSockex with __MODULE__ as well.. so that should’nt be a problem.

Looks correct on the Elixir side to me!

If not, you can also try using gun. Which seems a little more stable.
Good luck!

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement