aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bot/bot.go7
-rw-r--r--pkg/discord/discord.go22
-rw-r--r--pkg/discord/entities/entities.go (renamed from pkg/discord/entities.go)2
-rw-r--r--pkg/discord/event_handler.go36
-rw-r--r--pkg/discord/events/event_handler.go29
-rw-r--r--pkg/discord/events/events.go14
-rw-r--r--pkg/discord/gateway/gateway.go (renamed from pkg/discord/gateway.go)51
-rw-r--r--pkg/discord/gateway/heartbeat.go (renamed from pkg/discord/heartbeat.go)2
-rw-r--r--pkg/discord/gateway/payloads.go54
-rw-r--r--pkg/discord/payloads.go50
-rw-r--r--pkg/discord/rest/rest.go (renamed from pkg/discord/rest.go)9
11 files changed, 147 insertions, 129 deletions
diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go
index daad0ec..eb385c2 100644
--- a/pkg/bot/bot.go
+++ b/pkg/bot/bot.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"jinx/pkg/discord"
+ "jinx/pkg/discord/events"
"github.com/rs/zerolog"
)
@@ -33,12 +34,12 @@ func (b *Bot) Start() error {
}
}()
- b.client.On(discord.DISCORD_EVENT_READY, func(_ any) {
+ b.client.On(events.READY, func(_ any) {
b.logger.Info().Msg("ready!")
})
- b.client.On(discord.DISCORD_EVENT_MESSAGE, func(m any) {
- msg := m.(discord.GatewayMessageCreateEvent)
+ b.client.On(events.MESSAGE, func(m any) {
+ msg := m.(events.Message)
b.logger.Debug().Msgf("message: %s", msg.Content)
if msg.Content == "ping" {
diff --git a/pkg/discord/discord.go b/pkg/discord/discord.go
index a75e8e7..0030971 100644
--- a/pkg/discord/discord.go
+++ b/pkg/discord/discord.go
@@ -2,6 +2,10 @@ package discord
import (
"context"
+ "jinx/pkg/discord/entities"
+ "jinx/pkg/discord/events"
+ "jinx/pkg/discord/gateway"
+ "jinx/pkg/discord/rest"
"github.com/rs/zerolog"
)
@@ -9,17 +13,17 @@ import (
type Discord struct {
token string
logger *zerolog.Logger
- gateway Gateway
- eventHandler EventHandler
- rest REST
+ gateway gateway.Gateway
+ eventHandler events.EventHandler
+ rest rest.REST
}
func NewClient(token string, logger *zerolog.Logger) *Discord {
token = "Bot " + token
- eventHandler := NewEventHandler()
- rest := NewREST(token)
- gateway := NewGateway(logger, eventHandler)
+ eventHandler := events.NewEventHandler()
+ rest := rest.New(token)
+ gateway := gateway.New(logger, eventHandler)
return &Discord{
token: token,
@@ -42,7 +46,7 @@ func (d *Discord) Connect(ctx context.Context) error {
}
// We are ready!
- d.eventHandler.Fire(DISCORD_EVENT_READY, nil)
+ d.eventHandler.Fire(events.READY, nil)
return nil
}
@@ -51,10 +55,10 @@ func (d *Discord) Disconnect() error {
return d.gateway.Close()
}
-func (d *Discord) SendMessage(channelID Snowflake, content string) error {
+func (d *Discord) SendMessage(channelID entities.Snowflake, content string) error {
return d.rest.SendMessage(channelID, content)
}
-func (d *Discord) On(eventName DiscordEvent, handler func(payload any)) {
+func (d *Discord) On(eventName events.Event, handler func(payload any)) {
d.eventHandler.Add(eventName, handler)
}
diff --git a/pkg/discord/entities.go b/pkg/discord/entities/entities.go
index 8a62fc6..268a74c 100644
--- a/pkg/discord/entities.go
+++ b/pkg/discord/entities/entities.go
@@ -1,4 +1,4 @@
-package discord
+package entities
type Snowflake string
diff --git a/pkg/discord/event_handler.go b/pkg/discord/event_handler.go
deleted file mode 100644
index 6f3ded5..0000000
--- a/pkg/discord/event_handler.go
+++ /dev/null
@@ -1,36 +0,0 @@
-package discord
-
-type DiscordEvent uint8
-
-const (
- DISCORD_EVENT_READY DiscordEvent = iota
- DISCORD_EVENT_MESSAGE
-)
-
-type EventHandler interface {
- Add(event DiscordEvent, handler func(payload any))
-
- Fire(event DiscordEvent, payload any)
-}
-
-var _ EventHandler = &EventHandlerImpl{}
-
-type EventHandlerImpl struct {
- handlers map[DiscordEvent]func(payload any)
-}
-
-func NewEventHandler() *EventHandlerImpl {
- return &EventHandlerImpl{
- handlers: make(map[DiscordEvent]func(payload any)),
- }
-}
-
-func (h *EventHandlerImpl) Add(event DiscordEvent, handler func(payload any)) {
- h.handlers[event] = handler
-}
-
-func (h *EventHandlerImpl) Fire(event DiscordEvent, payload any) {
- if handler, ok := h.handlers[event]; ok {
- handler(payload)
- }
-}
diff --git a/pkg/discord/events/event_handler.go b/pkg/discord/events/event_handler.go
new file mode 100644
index 0000000..5a1a118
--- /dev/null
+++ b/pkg/discord/events/event_handler.go
@@ -0,0 +1,29 @@
+package events
+
+type EventHandler interface {
+ Add(event Event, handler func(payload any))
+
+ Fire(event Event, payload any)
+}
+
+var _ EventHandler = &EventHandlerImpl{}
+
+type EventHandlerImpl struct {
+ handlers map[Event]func(payload any)
+}
+
+func NewEventHandler() *EventHandlerImpl {
+ return &EventHandlerImpl{
+ handlers: make(map[Event]func(payload any)),
+ }
+}
+
+func (h *EventHandlerImpl) Add(event Event, handler func(payload any)) {
+ h.handlers[event] = handler
+}
+
+func (h *EventHandlerImpl) Fire(event Event, payload any) {
+ if handler, ok := h.handlers[event]; ok {
+ handler(payload)
+ }
+}
diff --git a/pkg/discord/events/events.go b/pkg/discord/events/events.go
new file mode 100644
index 0000000..b1730f0
--- /dev/null
+++ b/pkg/discord/events/events.go
@@ -0,0 +1,14 @@
+package events
+
+import "jinx/pkg/discord/entities"
+
+type Event uint8
+
+const (
+ READY Event = iota
+ MESSAGE
+)
+
+type Ready struct{}
+
+type Message entities.Message
diff --git a/pkg/discord/gateway.go b/pkg/discord/gateway/gateway.go
index 32a1e99..18cf708 100644
--- a/pkg/discord/gateway.go
+++ b/pkg/discord/gateway/gateway.go
@@ -1,9 +1,10 @@
-package discord
+package gateway
import (
"context"
"encoding/json"
"fmt"
+ "jinx/pkg/discord/events"
"jinx/pkg/libs/cancellablewebsocket"
"net/http"
@@ -25,11 +26,11 @@ type GatewayImpl struct {
logger *zerolog.Logger
conn *cancellablewebsocket.CancellableWebSocket
heartbeat Heartbeat
- eventHandler EventHandler
+ eventHandler events.EventHandler
lastSeq uint64
}
-func NewGateway(logger *zerolog.Logger, eventHandler EventHandler) *GatewayImpl {
+func New(logger *zerolog.Logger, eventHandler events.EventHandler) *GatewayImpl {
gateway := &GatewayImpl{
ctx: nil,
logger: logger,
@@ -77,34 +78,34 @@ func (g *GatewayImpl) Close() error {
}
func (g *GatewayImpl) Heartbeat() error {
- msg := GatewayPayload[uint64]{
- Op: GATEWAY_OP_HEARTBEAT,
+ msg := Payload[uint64]{
+ Op: OP_HEARTBEAT,
Data: g.lastSeq,
}
return g.send(msg)
}
-func (g *GatewayImpl) hello() (GatewayHelloEvent, error) {
- var msg GatewayPayload[GatewayHelloEvent]
+func (g *GatewayImpl) hello() (HelloEvent, error) {
+ var msg Payload[HelloEvent]
if err := receive(g, &msg); err != nil {
- return GatewayHelloEvent{}, err
+ return HelloEvent{}, err
}
- if msg.Op != GATEWAY_OP_HELLO {
- return GatewayHelloEvent{}, fmt.Errorf("expected opcode %d, got %d", GATEWAY_OP_HELLO, msg.Op)
+ if msg.Op != OP_HELLO {
+ return HelloEvent{}, fmt.Errorf("expected opcode %d, got %d", OP_HELLO, msg.Op)
}
return msg.Data, nil
}
func (g *GatewayImpl) identify(token string) error {
- msg := GatewayPayload[GatewayIdentifyMsg]{
- Op: GATEWAY_OP_IDENTIFY,
- Data: GatewayIdentifyMsg{
+ msg := Payload[IdentifyCmd]{
+ Op: OP_IDENTIFY,
+ Data: IdentifyCmd{
Token: token,
Intents: 13991,
- Properties: GatewayIdentifyProperties{
+ Properties: IdentifyProperties{
OS: "linux",
Browser: "jinx",
Device: "jinx",
@@ -117,15 +118,15 @@ func (g *GatewayImpl) identify(token string) error {
return err
}
- var res GatewayPayload[GatewayReadyEvent]
+ var res Payload[ReadyEvent]
if err := receive(g, &res); err != nil {
return err
}
g.logger.Debug().Msgf("identify response payload: %+v", res)
- if res.Op != GATEWAY_OP_DISPATCH {
- return fmt.Errorf("expected opcode %d, got %d", GATEWAY_OP_DISPATCH, res.Op)
+ if res.Op != OP_DISPATCH {
+ return fmt.Errorf("expected opcode %d, got %d", OP_DISPATCH, res.Op)
}
if res.EventName != "READY" {
@@ -137,7 +138,7 @@ func (g *GatewayImpl) identify(token string) error {
func (g *GatewayImpl) listen() {
for {
- var msg GatewayPayload[json.RawMessage]
+ var msg Payload[json.RawMessage]
if err := receive(g, &msg); err != nil {
g.logger.Error().Err(err).Msgf("error reading message")
continue
@@ -152,13 +153,13 @@ func (g *GatewayImpl) listen() {
}
}
-func (g *GatewayImpl) onEvent(msg GatewayPayload[json.RawMessage]) error {
+func (g *GatewayImpl) onEvent(msg Payload[json.RawMessage]) error {
switch msg.Op {
- case GATEWAY_OP_HEARTBEAT_ACK:
+ case OP_HEARTBEAT_ACK:
g.heartbeat.Ack()
- case GATEWAY_OP_HEARTBEAT:
+ case OP_HEARTBEAT:
g.heartbeat.ForceHeartbeat()
- case GATEWAY_OP_DISPATCH:
+ case OP_DISPATCH:
return g.onDispatch(msg.EventName, msg.Data)
default:
g.logger.Warn().Msgf("received unknown opcode: %d", msg.Op)
@@ -170,12 +171,12 @@ func (g *GatewayImpl) onEvent(msg GatewayPayload[json.RawMessage]) error {
func (g *GatewayImpl) onDispatch(eventName string, body json.RawMessage) error {
switch eventName {
case "MESSAGE_CREATE":
- var payload GatewayMessageCreateEvent
+ var payload MessageCreateEvent
if err := json.Unmarshal(body, &payload); err != nil {
return err
}
- g.eventHandler.Fire(DISCORD_EVENT_MESSAGE, payload)
+ g.eventHandler.Fire(events.MESSAGE, payload)
default:
g.logger.Warn().Msgf("received unknown event: %s", eventName)
}
@@ -187,7 +188,7 @@ func (g *GatewayImpl) send(payload any) error {
return g.conn.WriteJSON(payload)
}
-func receive[D any](g *GatewayImpl, res *GatewayPayload[D]) error {
+func receive[D any](g *GatewayImpl, res *Payload[D]) error {
err := g.conn.ReadJSON(&res)
if err != nil {
return err
diff --git a/pkg/discord/heartbeat.go b/pkg/discord/gateway/heartbeat.go
index 5c4a955..6cefb21 100644
--- a/pkg/discord/heartbeat.go
+++ b/pkg/discord/gateway/heartbeat.go
@@ -1,4 +1,4 @@
-package discord
+package gateway
import (
"context"
diff --git a/pkg/discord/gateway/payloads.go b/pkg/discord/gateway/payloads.go
new file mode 100644
index 0000000..8da894f
--- /dev/null
+++ b/pkg/discord/gateway/payloads.go
@@ -0,0 +1,54 @@
+package gateway
+
+import (
+ "jinx/pkg/discord/entities"
+)
+
+type GatewayOp uint8
+
+const (
+ OP_DISPATCH GatewayOp = 0
+ OP_HEARTBEAT GatewayOp = 1
+ OP_IDENTIFY GatewayOp = 2
+ OP_PRESENCE_UPDATE GatewayOp = 3
+ OP_VOICE_STATE_UPDATE GatewayOp = 4
+ OP_RESUME GatewayOp = 6
+ OP_RECONNECT GatewayOp = 7
+ OP_REQUEST_GUILD_MEMBERS GatewayOp = 8
+ OP_INVALID_SESSION GatewayOp = 9
+ OP_HELLO GatewayOp = 10
+ OP_HEARTBEAT_ACK GatewayOp = 11
+)
+
+type Payload[D any] struct {
+ Op GatewayOp `json:"op"`
+ Data D `json:"d,omitempty"`
+ Sequence uint64 `json:"s,omitempty"`
+ EventName string `json:"t,omitempty"`
+}
+
+type IdentifyCmd struct {
+ Token string `json:"token"`
+ Intents uint64 `json:"intents"`
+ Properties IdentifyProperties `json:"properties"`
+}
+
+type HelloEvent struct {
+ HeartbeatInterval uint64 `json:"heartbeat_interval"`
+}
+
+type ReadyEvent struct {
+ Version uint64 `json:"v"`
+ User entities.User `json:"user"`
+ Guilds []entities.Guild `json:"guilds"`
+ SessionID string `json:"session_id"`
+ Shard []int `json:"shard"`
+}
+
+type MessageCreateEvent entities.Message
+
+type IdentifyProperties struct {
+ OS string `json:"$os"`
+ Browser string `json:"$browser"`
+ Device string `json:"$device"`
+}
diff --git a/pkg/discord/payloads.go b/pkg/discord/payloads.go
deleted file mode 100644
index 81c6c68..0000000
--- a/pkg/discord/payloads.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package discord
-
-type GatewayOp uint8
-
-const (
- GATEWAY_OP_DISPATCH GatewayOp = 0
- GATEWAY_OP_HEARTBEAT GatewayOp = 1
- GATEWAY_OP_IDENTIFY GatewayOp = 2
- GATEWAY_OP_PRESENCE_UPDATE GatewayOp = 3
- GATEWAY_OP_VOICE_STATE_UPDATE GatewayOp = 4
- GATEWAY_OP_RESUME GatewayOp = 6
- GATEWAY_OP_RECONNECT GatewayOp = 7
- GATEWAY_OP_REQUEST_GUILD_MEMBERS GatewayOp = 8
- GATEWAY_OP_INVALID_SESSION GatewayOp = 9
- GATEWAY_OP_HELLO GatewayOp = 10
- GATEWAY_OP_HEARTBEAT_ACK GatewayOp = 11
-)
-
-type GatewayPayload[D any] struct {
- Op GatewayOp `json:"op"`
- Data D `json:"d,omitempty"`
- Sequence uint64 `json:"s,omitempty"`
- EventName string `json:"t,omitempty"`
-}
-
-type GatewayIdentifyMsg struct {
- Token string `json:"token"`
- Intents uint64 `json:"intents"`
- Properties GatewayIdentifyProperties `json:"properties"`
-}
-
-type GatewayHelloEvent struct {
- HeartbeatInterval uint64 `json:"heartbeat_interval"`
-}
-
-type GatewayReadyEvent struct {
- Version uint64 `json:"v"`
- User User `json:"user"`
- Guilds []Guild `json:"guilds"`
- SessionID string `json:"session_id"`
- Shard []int `json:"shard"`
-}
-
-type GatewayMessageCreateEvent Message
-
-type GatewayIdentifyProperties struct {
- OS string `json:"$os"`
- Browser string `json:"$browser"`
- Device string `json:"$device"`
-}
diff --git a/pkg/discord/rest.go b/pkg/discord/rest/rest.go
index 438d8d1..5f5bb78 100644
--- a/pkg/discord/rest.go
+++ b/pkg/discord/rest/rest.go
@@ -1,9 +1,10 @@
-package discord
+package rest
import (
"bytes"
"encoding/json"
"errors"
+ "jinx/pkg/discord/entities"
"net/http"
"time"
)
@@ -14,7 +15,7 @@ const USER_AGENT = "DiscordBot (https://jinx.rnrd.eu/, v0.0.0) Jinx"
type REST interface {
Gateway() (string, error)
- SendMessage(channelID Snowflake, content string) error
+ SendMessage(channelID entities.Snowflake, content string) error
}
var _ REST = &RESTImpl{}
@@ -24,7 +25,7 @@ type RESTImpl struct {
client *http.Client
}
-func NewREST(token string) *RESTImpl {
+func New(token string) *RESTImpl {
return &RESTImpl{
token: token,
client: &http.Client{
@@ -46,7 +47,7 @@ func (r *RESTImpl) Gateway() (string, error) {
return res.URL + "?v=9&encoding=json", nil
}
-func (r *RESTImpl) SendMessage(channelID Snowflake, content string) error {
+func (r *RESTImpl) SendMessage(channelID entities.Snowflake, content string) error {
msg := struct {
Content string `json:"content"`
}{