#discord_gleam, a discord bot library for gleam (duh)

1 messages Β· Page 1 of 1 (latest)

wintry jay
neat jasper
#

awesome :0

wintry jay
#

Update: can recive packets from discord, and maintan a connection with the gateway

wintry jay
#

code for the test:

#
import discord_gleam
import discord_gleam/event_handler
import logging

pub fn main(token: String) {
  logging.configure()
  logging.set_level(logging.Info)

  discord_gleam.run(token, [event_handler])
}

fn event_handler(packet: event_handler.Packet) {
  case packet {
    event_handler.ReadyPacket(ready) -> {
      logging.log(logging.Info, "Logged in as " <> ready.d.user.username)
    }
    event_handler.MessagePacket(message) -> {
      logging.log(
        logging.Info,
        "Received message: '"
          <> message.d.content
          <> "' from "
          <> message.d.author.username,
      )
    }
    _ -> Nil
  }
}
#

A discord bot library im working on (status: can read events)

#

Only the following events tho: READY, MESSAGE_CREATE

wintry jay
#

Update: Can send messages

#

Which means basic commands

#

A discord bot library im working on (status: basic commands)

dull yew
#

That's a pretty cool project!

wintry jay
#

embeds

wintry jay
wintry jay
# wintry jay

a little snippet: ```swift
let embed1 =
message.Embed(
title: "Embed Title",
description: "Embed Description",
color: 0x00FF00,
)

      discord_gleam.send_message(bot, message.d.channel_id, "Embed!", [
        embed1,
      ])
wintry jay
wind mason
#

WOOOOOO

#

W CYTEON

#

WWWWWW

#

MY MAN IS COOKING

neat jasper
#

a lil check list on the main readme for what is done n what is coming would be neat :) /suggestion

wintry jay
#

user kicking

#

(failed cause role was below the target first time)

wintry jay
#

I injured my arm, so development might be stale for a week

fallen pulsar
#

ouch, get well soon tho!

wintry jay
#

thanks :D

dull yew
#

Wish you a quick recovery!

warm canopy
wintry jay
wintry jay
#

examples includes ban/kick/embed/pingpong

warm canopy
wintry jay
#

me rn

warm canopy
#

πŸ™‚

#

If I find time, I could look into it lucy

wintry jay
#

ill prob try tho

#

it will be interesting

wintry jay
#

Update v0.0.3:

#

Added message delete event to packets

#

Added message deleting + example for it

#

Fixed some on examples

upbeat magnet
#

My only request in any discord bot library in a typed language: typed snowflakes

upbeat magnet
#

mhm

#

message ids, user ids, guid ids etc

#

they are called snowflakes

wintry jay
#

ah

#

i do that somewhere

#

actually

upbeat magnet
#

a large amount of libraries just have one "snowflake" type

#

so it's easy to mess them up and pass them incorrectly

wintry jay
#

hm

#

hm

#

imma try change em

upbeat magnet
#

they're always integers

wintry jay
#

wonder how much this breaks

#

nothing broke

#

interesting

#

Pushed commit that changes all ids to snowflakes

warm canopy
upbeat magnet
#

+1 but discord uses it's own thing

warm canopy
wintry jay
#

Possibly adding slash cmds rn :)

wintry jay
#

yay

#

need to add handling tho

wintry jay
#

options dosent work in "interaction_create" event

#

so those cant be used

#

this is very bare bones and needs lots of work too

upbeat magnet
wintry jay
#

pushing to git now

wintry jay
#

Update v0.0.4:

#

Slash commands

wintry jay
#

A discord bot library im working on (status: basic commands + bare bones slash cmds)

noble trout
noble trout
#

The client ID was hard-coded in.

wintry jay
#

nvm

#

ye

#

10sec lemme test then merge

noble trout
#

Alright! Thank you.

#

Would you mind if I work on VOICE_STATE_UPDATE event as well?

wintry jay
#

sure

noble trout
#

great!

wintry jay
#

if it works, ill merge

#

@noble trout Merged your pr

noble trout
#

Thank you!

wintry jay
#

i will add a function to delete slash cmds

#

so u can delete those u dont use anymore

#

or, a function to remove all actually

#

so can u just call that before registering cmds

noble trout
#

nice!!

wintry jay
wintry jay
#

@dusky relic i requested some changes on your PR

wintry jay
#

another pr merged πŸ’ͺ

#

no tests failed this time πŸ”₯

wintry jay
#

Pushed v0.0.5 to hex

wintry jay
#

Adding in a cache now

#

So you can get deleted messages and edited ones

#

also to speed it up

wintry jay
#

gleam feels so gleam after not using it for a while

wintry jay
#

this code feels bad

#

but it works ig

#

need to find some of my silly issues tho

#

πŸ‘€

#

working on intents now

#

Intents appear to be working

#

current only intents message_content and guild_messages

#

as any other would be useless atm

tame jolt
#

big fan of this library so far. does it support sending direct messages to users? i can’t tell from the docs.

wintry jay
#

Sorry i didnt notice your msg before now πŸ˜“

wintry jay
#

Working on it

tame jolt
#

it was cool i had the tools to do it myself

wintry jay
#

btw, if you are using default_intents(), i renamed the function to default() in v0.2.2

tame jolt
#

works like a charm

wintry jay
#

Yayyy

tame jolt
#

FYI, my websocket connection is disconneted every 30 minutes - 2 hours

#

i believe it's because the heartbeat code is incorrect

#

i think the right approach is to use process.send_after rather than repeatedly

#

but i'll need to poke around at it a bit

wintry jay
wintry jay
#

I fixed sequence being 0, not sure if that will fix disconnecting tho

#

ill keep it running for a while to see

wintry jay
#

btw, up to v0.1.0 i wasnt following semver correctly, my brain was prob lagging

#

but from then (we are currently at v0.2.4) i have been doing it correctly πŸ™

foggy jay
#

v0's are exempt from semver, thats why you have to type the notice to publish them

wintry jay
foggy jay
#

we encourage folks to publish v1's when they want to publish a package, theres no problem hasving major bumps!

upbeat magnet
#

i'm trying to get my friends less scared to do major version bumps as they have a habit of doing breaking changes on patch versions πŸ™„

wintry jay
tame jolt
#
INFO Last restart at: 2025-05-22T19:06:44.238Z
INFO Running...
EROR oh nyo, discord closed websocket :c
INFO websocket go bye bye
runtime error: let assert
Pattern match failed, no pattern matched the value.
unmatched value:
  Error(ErlangError("badarg"))
stacktrace:
  discord_gleam/ws/event_loop.main src/discord_gleam/ws/event_loop.gleam:32
upbeat magnet
#

possibly creating an ETS table that already exists with the name "State"?

#

I see Last restart at: 2025-05-22T19:06:44.238Z - are you restarting your bot programatically?

#

There doesn't seem to a uset.delete at the end of the main function in the lib

upbeat magnet
#

Then what I said seems likely fme

wintry jay
#

or is it still crashing randomly :/

wintry jay
#

im going to fix some of my wack code then publish 1.0.0

fallen pulsar
#

OO

wintry jay
wintry jay
#

implementing rn

wintry jay
wintry jay
#

having some issues determing the disconnect code when disconnected from the websocket

#

should i reconnect every time, unless it was really short since last reconnect? or just only reconnect on reconnect packet

wintry jay
#

(imo i should publish v1, then add more intents in future versions)

wicked compass
#

Imo with you having the "Yeah, you can make a basic and usable bot with this" stuff in, it wouldn't hurt to go ahead and do v1, then expand on it from there.

wintry jay
#

pushing v1 now

wintry jay
#

time to just implement a lot of gateway events πŸ”₯

wintry jay
#

added CHANNEL_CREATE, CHANNEL_DELETE and CHANNEL_UPDATE

kindred sundial
#

Did anyone encounter this at startup (one of the examples):

EROR Disconnected after too short time, not reconnecting
EROR websocket go bye bye

is this maybe a permissions issue? or something else?

fallen pulsar
#

😭 does it really say 'go bye bye'

fallen pulsar
#

lool

wintry jay
kindred sundial
#

Ahh didn't see that hah

DEBG Requesting gateway
DEBG Creating builder
DEBG Calling user initializer
DEBG Builder init
DEBG Attempting handshake to https://gateway.discord.gg/?v=10&encoding=json
DEBG Making request to gateway.discord.gg at 443
DEBG Sent upgrade request, waiting 5000
DEBG Handshake successful
DEBG WebSocket process ready to start receiving
DEBG Gateway text msg: {"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["[\"gateway-prd-us-east1-b-0b66\",{\"micros\":0.0}]"]}}
DEBG Gateway text msg: {"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["[\"gateway-prd-us-east1-b-0b66\",{\"micros\":0.0}]"]}}
DEBG WebSocket closing: "Disallowed intent(s)."
DEBG The webhook was closed
EROR Disconnected after too short time, not reconnecting
EROR websocket go bye bye

so i have the incorrect permissions for the bot?

wintry jay
#

Its under "Privileged Gateway Intents"

kindred sundial
#

it works, thanks! πŸ™‚

wintry jay
#

Epic

kindred sundial
wintry jay
#

would it be possible to have bravo just use < 1.0.0 gleam_erlang instead?

kindred sundial
#

let me check

#

i seem to have it almost working now, just this:

error: Type mismatch
    β”Œβ”€ /Users/dvic/Development/discord_gleam/src/discord_gleam/ws/event_loop.gleam:71:13
    β”‚
 71 β”‚         loop: fn(state, msg, conn) {
    β”‚ ╭─────────────^
 72 β”‚ β”‚         case msg {
 73 β”‚ β”‚           stratus.Text(msg) -> {
 74 β”‚ β”‚             logging.log(logging.Debug, "Gateway text msg: " <> msg)
    Β· β”‚
191 β”‚ β”‚         }
192 β”‚ β”‚       },
    β”‚ ╰───────^

Expected type:

    fn(State, stratus.Message(String), stratus.Connection) -> stratus.Next(State, String)

Found type:

    fn(State, stratus.Message(String), stratus.Connection) -> actor.Next(State, a)

and this


  todo
  // process.new_selector()
  // |> process.selecting_process_down(process.monitor(pid), function.identity)
  // |> process.select_forever

#

(with all the deps bumped)

wintry jay
#

epic

kindred sundial
wintry jay
#

fair

kindred sundial
#

i've updated the description πŸ˜‚

wintry jay
#

is everything outdated

kindred sundial
#

yeah, these are the PRs of the deps that need to be merged

upbeat magnet
#

max and rawhat shouldn't take too long, but bravo hasn't been updated in like a year

kindred sundial
wintry jay
#

me when i am adding ban event but end up fixing how errors are done in the event handler (the channel update event isnt updated to new yet)

#

now it will be way easier to debug and stuff

#

ban and unban events work (ignore the empty messages, those are embed-only messages from other bots)

wintry jay
#

😭

wintry jay
#

published v1.2.0

wintry jay
#

discord_gleam, a discord bot library for gleam (duh)

#

btw im still working on this, but a little slower as i have a job for the next two weeks, and for the past week

wintry jay
#

Added the GUILD_ROLE_CREATE, GUILD_ROLE_UPDATE and GUILD_ROLE_DELETE gateway events

wintry jay
#

just implementing a bunch of gateway stuff

molten marlin
#

hello

#

I shall be using your package πŸ™‚

#

It does break my LSP though

molten marlin
#

Is there a specific version of gleam this needs to run on?

upbeat magnet
#

they need to update to gleam_erlang 1.0.0

molten marlin
#

oh i see

upbeat magnet
#

discord_gleam is using 0.34.0

molten marlin
#

yeah im on gleam 1.11.1

upbeat magnet
#

gleam version doesn't matter

molten marlin
#

oh

upbeat magnet
#

gleam_erlang is a package

molten marlin
#

oh

#

gotcha

upbeat magnet
#

you can pin it in your gleam.toml for now

[dependencies]
gleam_erlang = "0.34.0"
molten marlin
#

that seems really outdated

upbeat magnet
#

it's not that outdated, it's the version prior to 1.0.0

#

Feb 2nd 2025

molten marlin
#

oh

#

well i updated

#

same error...

#

i just ran "gleam deps download"

#

is that correct?

#

Sorry, im new

upbeat magnet
#

gleam deps update after pinning it

molten marlin
#

oh

upbeat magnet
molten marlin
#

now i need rebar3

upbeat magnet
#

yeah

molten marlin
#

does gleam not install dependencies of dependencies?

#

i have to add them all myself?

upbeat magnet
#

rebar3 is the erlang build tool

molten marlin
#

oh

#

im guessing this then

upbeat magnet
#

yeah, i think i've seen people use the non-minimal one for nix as well

molten marlin
#

yeah it didn't like this version

upbeat magnet
#

while you're there make sure you have erlang installed too

molten marlin
#

yeah

#

erlang_28

#

oh

#

maybe it's because i used a erlang_27 package

upbeat magnet
#

maybe

#

are you on unstable?

molten marlin
#

yes

#

yeah

#

the 28 package is on unstable

upbeat magnet
#

i think it's borked on unstable rn

molten marlin
#

nah, i was just looking at nix 25.05

upbeat magnet
#

#1395043705269784698 message

molten marlin
#

ah

#

it runs on stable!

#

yay!

#

Thank you

upbeat magnet
#

np ^.^

#

gleam is great, have fun using it <3

wintry jay
#

if u have any questions feel free to ping me and you will prob get a question instantly, or whitin 12 hours (if im sleeping)

#

i should check this thread more often tho, so i see messages where im not pinged

wintry jay
#

@kindred sundial one of your prs got closed

#

But the library was updated

#

So ig make the next draft pr use that version and mark ready for review?

kindred sundial
#

saw it, will try to get to it this weekend!

wintry jay
kindred sundial
foggy jay
#

appreciate that this might be a priority for you but pinging me at 08:30am is probably not the play

rustic trout
#

Then ignore it. I don't understand why this is such a big issue.

#

I just wanted to make sure you were aware of the situation.

kindred sundial
#

nice!

kindred sundial
rustic trout
upbeat magnet
#

i think either switching library or writing your own FFI to ETS is the best course of action atp

wintry jay
#

I'll take a look at booklet once I'm home from my trip tmrw

wintry jay
wintry jay
#

fixing the gh workflow then releasing 1.5.0

#

released :D

kindred sundial
#

nice!!!!

wintry jay
#

cause school has started for me development will be slow, but i will still try to find time for it

#

i hope to implement more stuff this weekend

rancid mist
#

Oh hey, this seems very handy. Gleam discord bot sounds fun.

#

Have a gh lucy

wintry jay
wintry jay
#

what do yall feel like its most important for me to add the soonest?

rancid mist
#

tbh looking at your feature set, you've done so much since I last checked it out.

wintry jay
#

yay

#

i think imma first focus on implementing more gateway events

wintry jay
#

implemented close codes, makes it way easier to debug if you are disconnected

upbeat magnet
#

nit: Discord should be capitalised in the err msg 😝

really good job with everything here! I haven't had a chance to use it for a while but it seems like you're making amazing progress

fallen pulsar
deep cargo
#

Hi!

#

I'm tryig to use this

#

and I'm not sure if the GuildMemberAddPacket works

#

with intents set to all on both the developer portal and the code

#
fn event_handler(bot, packet: event_handler.Packet) {
  case packet {
    event_handler.GuildMemberUpdatePacket(member) -> {
      echo "Guild member add packet received"
      Nil
    }
  }
}
#

this has no output

#

Nvm i fixed it

#

awesome library though!

main nymph
#

@deep cargo I've noticed a pattern of you asking a question and then immediately figuring it out. Have you ever considering getting a desk rubber duck? Rubber duck debugging is quite useful πŸ˜„

deep cargo
#

I have a rubber duck, but its noisy, and I don't like making noise

foggy jay
#

i think its good πŸ™‚ if someone has a similar problem in the future they can see you figured it out and ask you

warm canopy
#

Even better with a quick summary of the solution :)

deep cargo
#

it was me not using intents when I configured the bot

#

i was using the code from the docs first page

main nymph
deep cargo
#

Just one note, ```
case interaction.d.data.name {
"ping" -> {
case interaction.d.data.options {
option.Some(options) -> {
case list.first(options) {
Ok(option) -> {
let value = case option.value {
interaction_create.StringValue(value) -> value
_ -> "unexpected value type"
}

seems like a lot of cases...
#

for slash commands

main nymph
#

there are ways to refactor your code to make it less nested

#

you can extract things out to their own functions

#

you can make use of use

#

you can make use of pipelines

#

without the full context it'd be hard to show you a possible refactor

heady iron
#

instead of list.first, pattern match on the list items in the option.Some(...)

keen canopy
#

I'd pull each command into a function, so ping would be its own thing
Also, you can match on the option like Some([option, ..]) ->
But this is getting more into general gleam advice, so let's not further hijack the thread

deep cargo
#

Ahh these suggestions are useful

#

ty

deep cargo
fallen pulsar
#

the clanker

deep cargo
#

yes

upbeat magnet
#

made a few small PRs to discord_gleam πŸ’œ

wintry jay
molten marlin
#

hi

molten marlin
#

does the new version of this work?

#

the readme is very outdated

#

but i think i did it right based on the tests

molten marlin
#

maybe im using the wrong token or clientid?

molten marlin
#

hmmm

#

I can add commands and everything

#

that works

#

but the bot never goes online

molten marlin
#

@wintry jay hello πŸ™‚

#

I was wondering if you'd be able to help diagnose this warning i keep getting

wintry jay
wintry jay
wintry jay
molten marlin
#

I'll try getting that tonight

molten marlin
# wintry jay can you send the full warning?

WARN #(charlist.from_string("Actor discarding unexpected message: ~s"), [charlist.from_string("Ssl(Sslsocket(//erl(#Port<0.6>), //erl(<0.136.0>), //erl(<0.135.0>), GenTcp, TlsGenConnection, //erl(#Ref<0.3990994764.2383806473.93114>), Undefined), <<129, 126, 0, 128, 123, 34, 116, 34, 58, 110, 117, 108, 108, 44, 34, 115, 34, 58, 110, 117, 108, 108, 44, 34, 111, 112, 34, 58, 49, 48, 44, 34, 100, 34, 58, 123, 34, 104, 101, 97, 114, 116, 98, 101, 97, 116, 95, 105, 110, 116, 101, 114, 118, 97, 108, 34, 58, 52, 49, 50, 53, 48, 44, 34, 95, 116, 114, 97, 99, 101, 34, 58, 91, 34, 91, 92, 34, 103, 97, 116, 101, 119, 97, 121, 45, 112, 114, 100, 45, 97, 114, 109, 45, 117, 115, 45, 101, 97, 115, 116, 49, 45, 100, 45, 108, 107, 116, 56, 92, 34, 44, 123, 92, 34, 109, 105, 99, 114, 111, 115, 92, 34, 58, 48, 46, 48, 125, 93, 34, 93, 125, 125>>)")])

heady iron
#

This is those bytes as a string btw

{
  "t": null,
  "s": null,
  "op": 10,
  "d": {
    "heartbeat_interval": 41250,
    "_trace": [
      [
        "gateway-prd-arm-us-east1-d-lkt8",
        { "micros": 0.0 }
      ]
    ]
  }
}
wintry jay
molten marlin
#

Hm rip

wintry jay
#

are you using the latest version?

molten marlin
#

Yeah

wintry jay
#

hm

molten marlin
#

It's able to add slash commands and everything

#

It just doesn't go online or respond to anything

#

And there is no ready message