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: Discord Developer Portal

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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement