This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbox.go
154 lines (128 loc) · 3.33 KB
/
chatbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package switchcraftgo
import (
"encoding/json"
"net/url"
"github.com/gorilla/websocket"
)
const (
ChatboxFormattingMarkdown = iota
ChatboxFormattingFormat
)
type ChatboxIngameUser struct {
Type string `json:"type"`
Name string `json:"name"`
Uuid string `json:"uuid"`
DisplayName string `json:"displayName"`
Group string `json:"group"`
Pronouns string `json:"pronouns"`
World string `json:"world"`
Afk bool `json:"afk"`
Alt bool `json:"alt"`
Bot bool `json:"bot"`
Supporter uint8 `json:"supporter"`
LinkedUser *ChatboxDiscordUser `json:"linkedUser"`
}
type ChatboxDiscordUser struct {
Type string `json:"type"`
Id uint64 `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Discriminator uint16 `json:"discriminator"`
Avatar string `json:"avatar"`
Roles []*ChatboxDiscordRole `json:"roles"`
LinkedUser *ChatboxIngameUser `json:"linkedUser"`
}
type ChatboxDiscordRole struct {
Id uint64 `json:"id"`
Name string `json:"name"`
Colour uint32 `json:"colour"`
}
type ChatboxTellPacket struct {
Type string `json:"type"`
User string `json:"user"`
Text string `json:"text"`
Name string `json:"name"`
Mode string `json:"mode"`
}
type ChatboxCommandPacket struct {
Event string `json:"event"`
User ChatboxIngameUser `json:"user"`
Command string `json:"command"`
Args []string `json:"args"`
OwnerOnly bool `json:"ownerOnly"`
}
type ChatboxGenericEventPacket struct {
Event string `json:"event"`
}
type Chatbox struct {
Conn *websocket.Conn
scUrl url.URL
OnRaw func(int, []byte)
OnCommand func(ChatboxCommandPacket)
}
type NewChatboxOptions struct {
Token string
Base url.URL
}
func GetDefaultBase() url.URL {
return url.URL{
Scheme: "wss",
Host: "chat.sc3.io",
Path: "/v2/",
}
}
func NewChatbox(opts NewChatboxOptions) *Chatbox {
scUrl := opts.Base
if scUrl.Host == "" {
scUrl = GetDefaultBase()
}
if opts.Token == "" {
opts.Token = "guest"
}
scUrl.Path += opts.Token
sc := &Chatbox{
scUrl: scUrl,
OnRaw: func(_ int, _ []byte) {},
OnCommand: func(_ ChatboxCommandPacket) {},
}
return sc
}
func (sc *Chatbox) Connect() error {
conn, _, err := websocket.DefaultDialer.Dial(sc.scUrl.String(), nil)
if err != nil {
return err
}
sc.Conn = conn
return nil
}
func (sc *Chatbox) Listen() {
for {
messageType, message, err := sc.Conn.ReadMessage()
if err != nil {
break
}
sc.OnRaw(messageType, message)
var parsed ChatboxGenericEventPacket
json.Unmarshal(message, &parsed)
switch parsed.Event {
case "command":
var command ChatboxCommandPacket
json.Unmarshal(message, &command)
sc.OnCommand(command)
}
}
}
func (sc *Chatbox) Tell(user, message, name string, mode int) {
formattingMode := "markdown"
if mode == ChatboxFormattingFormat {
formattingMode = "format"
}
packet := &ChatboxTellPacket{
Type: "tell",
User: user,
Text: message,
Name: name,
Mode: formattingMode,
}
sc.Conn.WriteJSON(packet)
}