-
Notifications
You must be signed in to change notification settings - Fork 1
/
discord.go
286 lines (232 loc) · 8.31 KB
/
discord.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"fmt"
"strings"
"github.com/TheDiscordian/onebot/libs/discord"
"github.com/TheDiscordian/onebot/onelib"
"github.com/bwmarrin/discordgo"
)
const (
// NAME is same as filename, minus extension
NAME = "discord"
// LONGNAME is what's presented to the user
LONGNAME = "Discord"
// VERSION of the script
VERSION = "v0.0.0"
)
var (
// discordAuthUser FIXME set this somewhere so we can filter out messages from ourselves (see: recv)
discordAuthUser string
// discordAuthToken if blank, falls back onto pass
discordAuthToken string
// discordAdminId is the UUID of the admin of the bot. This should probably be an array
discordAdminId string
// our userID
discordId onelib.UUID
)
func loadConfig() {
discordAuthToken = onelib.GetTextConfig(NAME, "auth_token")
discord.DiscordAdminId = onelib.UUID(onelib.GetTextConfig(NAME, "admin_id"))
}
// Load connects to Discord, and sets up listeners. It's required for OneBot.
func Load() onelib.Protocol {
loadConfig()
if discordAuthToken == "" {
onelib.Error.Panicln("discordAuthToken can't be blank.")
}
client, err := discordgo.New("Bot " + discordAuthToken) // FIXME maybe make this a global of a "discord lib" (wrap discordgo client in our own struct, extending as needed)
if err != nil {
onelib.Error.Panicln(err)
}
discordSession := &Discord{client: &discord.DiscordClient{Session: client}, prefix: onelib.DefaultPrefix, nickname: onelib.DefaultNickname}
client.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { // OnMessageCreate...
if m.Type == discordgo.MessageTypeDefault {
var (
displayName string
sender *discordSender
user *discordgo.User
)
msg := &discordMessage{text: m.Content, id: onelib.UUID(m.ID)}
dc := &discord.DiscordClient{Session: client}
dl := &discord.DiscordLocation{Client: dc, Uuid: onelib.UUID(m.ChannelID), GuildID: onelib.UUID(m.GuildID)}
// Check if we were mentioned
if strings.Contains(m.Content, fmt.Sprintf("<@%s>", string(discordId))) {
msg.mentioned = true
} else if m.MessageReference != nil { // Check if we were replied to
originalMessage, err := s.ChannelMessage(m.ChannelID, m.MessageReference.MessageID) // TODO: Check if this is an API call each time, if so, maybe we should cache msgs
if err == nil && originalMessage.Author.ID == string(discordId) {
msg.mentioned = true
}
}
if m.Member != nil && m.Member.Nick != "" {
displayName = m.Member.Nick
} else if m.Author != nil {
displayName = m.Author.Username
}
if m.Author != nil {
user = m.Author
} else if m.Member != nil && m.Member.User != nil {
user = m.Member.User
}
if user != nil {
sender = &discordSender{uuid: onelib.UUID(user.ID), username: user.Username + "#" + user.Discriminator, displayName: displayName, location: dl}
} else {
onelib.Error.Println("Error processing message, contains no UUID:", m)
return
}
discordSession.recv(onelib.Message(msg), onelib.Sender(sender))
onelib.Debug.Printf("%s: %s\n", displayName, msg.Text())
} else {
onelib.Debug.Printf("Message (type: %v): %v\n", m.Type, m)
}
})
client.AddHandler(func(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
if m.Emoji.ID == "" {
m.Emoji.ID = m.Emoji.Name
}
msg := &discordMessage{id: onelib.UUID(m.MessageID), emoji: &onelib.Emoji{Added: true, ID: onelib.UUID(m.Emoji.ID), Name: m.Emoji.Name}}
dc := &discord.DiscordClient{Session: client}
dl := &discord.DiscordLocation{Client: dc, Uuid: onelib.UUID(m.ChannelID), GuildID: onelib.UUID(m.GuildID)}
sender := &discordSender{uuid: onelib.UUID(m.UserID), location: dl}
discordSession.update(msg, sender)
})
client.AddHandler(func(s *discordgo.Session, m *discordgo.MessageReactionRemove) {
if m.Emoji.ID == "" {
m.Emoji.ID = m.Emoji.Name
}
msg := &discordMessage{id: onelib.UUID(m.MessageID), emoji: &onelib.Emoji{Added: false, ID: onelib.UUID(m.Emoji.ID), Name: m.Emoji.Name}}
dc := &discord.DiscordClient{Session: client}
dl := &discord.DiscordLocation{Client: dc, Uuid: onelib.UUID(m.ChannelID), GuildID: onelib.UUID(m.GuildID)}
sender := &discordSender{uuid: onelib.UUID(m.UserID), location: dl}
discordSession.update(msg, sender)
})
// Add a handler for the Ready event
client.AddHandlerOnce(func(s *discordgo.Session, r *discordgo.Ready) {
// Retrieve the user ID
discordId = onelib.UUID(r.User.ID)
})
err = discordSession.client.Open()
if err != nil {
onelib.Error.Panicln(err)
return nil
}
return onelib.Protocol(discordSession)
}
type discordMessage struct {
id onelib.UUID
formattedText, text string
emoji *onelib.Emoji
mentioned bool
}
func (mm *discordMessage) Mentioned() bool {
return mm.mentioned
}
func (mm *discordMessage) UUID() onelib.UUID {
return mm.id
}
func (mm *discordMessage) Reaction() *onelib.Emoji {
return mm.emoji
}
func (mm *discordMessage) Text() string {
return mm.text
}
func (mm *discordMessage) FormattedText() string {
return mm.formattedText
}
func (mm *discordMessage) StripPrefix(prefix string) onelib.Message {
if len(mm.text) > len(prefix) {
prefix = prefix + " "
}
return onelib.Message(&discordMessage{text: strings.Replace(mm.text, prefix, "", 1), formattedText: strings.Replace(mm.formattedText, prefix, "", 1)})
}
func (mm *discordMessage) Raw() []byte {
return []byte(mm.text)
}
type discordSender struct {
displayName, username string
location *discord.DiscordLocation
uuid onelib.UUID
}
func (ms *discordSender) Self() bool {
return ms.uuid == discordId
}
func (ms *discordSender) DisplayName() string {
return ms.displayName
}
func (ms *discordSender) Username() string {
return ms.username
}
func (ms *discordSender) UUID() onelib.UUID {
return ms.uuid
}
func (ms *discordSender) Location() onelib.Location {
return ms.location
}
func (ms *discordSender) Protocol() string {
return NAME
}
func (ms *discordSender) Send(msg onelib.Message) {
ms.location.Client.Send(ms.uuid, msg)
}
func (ms *discordSender) SendText(text string) {
ms.location.Client.SendText(ms.uuid, text)
}
func (ms *discordSender) SendFormattedText(text, formattedText string) {
ms.location.Client.SendFormattedText(ms.uuid, text, formattedText)
}
// Discord is the Protocol object used for handling anything Discord related.
type Discord struct {
/*
Store useful data here such as connected rooms, admins, nickname, accepted prefixes, etc
*/
prefix string
nickname string
client *discord.DiscordClient
}
// Name returns the name of the plugin, usually the filename.
func (dis *Discord) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (dis *Discord) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (dis *Discord) Version() string {
return VERSION
}
// NewMessage should generate a message object from something
func (dis *Discord) NewMessage(raw []byte) onelib.Message {
return nil
}
// Send sends a Message object to a location specified by to (usually a location or sender UUID).
func (dis *Discord) Send(to onelib.UUID, msg onelib.Message) {
dis.client.Send(to, msg)
}
// SendText sends text to a location specified by to (usually a location or sender UUID).
func (dis *Discord) SendText(to onelib.UUID, text string) {
dis.client.SendText(to, text)
}
// SendFormattedText sends formatted text to a location specified by to (usually a location or sender UUID).
func (dis *Discord) SendFormattedText(to onelib.UUID, text, formattedText string) {
dis.client.SendFormattedText(to, text, formattedText)
}
// GetUserDisplayName returns a user's display name from a UUID
//func (dis *Discord) GetUserDisplayName(uuid onelib.UUID) string
// recv should be called after you've recieved data and built a Message object
func (dis *Discord) recv(msg onelib.Message, sender onelib.Sender) {
if string(sender.UUID()) != discordAuthUser {
onelib.ProcessMessage([]string{dis.prefix}, msg, sender)
}
}
// update should be called after you've recieved an edit or reaction
func (dis *Discord) update(msg onelib.Message, sender onelib.Sender) {
if string(sender.UUID()) != discordAuthUser {
onelib.ProcessUpdate(msg, sender)
}
}
// Remove
func (dis *Discord) Remove() {
dis.client.Session.Close()
}