-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
384 lines (315 loc) · 8.09 KB
/
main.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/c-bata/go-prompt"
"github.com/diamondburned/arikawa/api"
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/gateway"
"github.com/pkg/errors"
)
var (
token string
)
func init() {
flag.StringVar(&token, "t", "", "Discord token to use")
}
func main() {
flag.Parse()
if token == "" {
log.Fatalln("Missing token; declare with -t $TOKEN.")
}
s, err := connect(token)
if err != nil {
log.Fatalln("failed to connect:", err)
}
p := NewPrompt(s)
p.Run()
}
type Prompt struct {
writeMu sync.Mutex
writer prompt.ConsoleWriter
prompt *prompt.Prompt
state *State
lastTyped time.Time
}
func NewPrompt(s *State) *Prompt {
p := Prompt{
writer: prompt.NewStdoutWriter(),
state: s,
}
p.prompt = prompt.New(
p.execute, WrapAutoCompleter(p.state),
prompt.OptionTitle("protocord"),
prompt.OptionPrefix("> "),
prompt.OptionLivePrefix(p.prefix),
prompt.OptionWriter(p.writer),
prompt.OptionSetExitCheckerOnInput(p.onChange),
)
s.AddHandler(func(msg *gateway.MessageCreateEvent) {
if p.state.ChannelID() != msg.ChannelID {
return
}
var name = msg.Author.Username
if msg.Member != nil && msg.Member.Nick != "" {
name = msg.Member.Nick
}
p.Writelnf(
"[%s] %s: %s",
msg.Timestamp.Time().Local().Format(time.Kitchen),
name, msg.Content,
)
})
s.AddHandler(func(msg *gateway.MessageUpdateEvent) {
if p.state.ChannelID() != msg.ChannelID {
return
}
var name = msg.Author.Username
if msg.Member != nil && msg.Member.Nick != "" {
name = msg.Member.Nick
}
p.Writelnf(
"[%s] %s: %s (edited)",
msg.EditedTimestamp.Time().Local().Format(time.Kitchen),
name, msg.Content,
)
})
s.AddHandler(func(t *gateway.TypingStartEvent) {
if p.state.ChannelID() != t.ChannelID {
return
}
// Lazy mode.
if t.Member != nil {
var name = t.Member.Nick
if name == "" {
name = t.Member.User.Username
}
p.WriteColorlnf(
prompt.LightGray,
"*%s is typing.*", name,
)
}
})
return &p
}
func (p *Prompt) Run() { p.prompt.Run() }
var commandSuggestions = []prompt.Suggest{
{Text: "/help", Description: "Print the help message."},
{Text: "/quit", Description: "Quit."},
{Text: "/list", Description: "List all servers."},
{Text: "/join", Description: "Join a channel."},
{Text: "/join-invite", Description: "Join a guild using an invite code."},
{Text: "/create-invite", Description: "Create an invite code to the current channel."},
{Text: "/create-guild", Description: "Create a new guild"},
{Text: "/create-channel", Description: "Create a new channel"},
}
func (p *Prompt) execute(line string) {
if !strings.HasPrefix(line, "/") {
p.sendMessage(line)
return
}
parts := strings.SplitN(line, " ", 2)
k, v := strings.TrimPrefix(parts[0], "/"), ""
if len(parts) == 2 {
v = parts[1]
}
switch k {
case "help":
p.WriteLine("To send a message, type in directly.")
p.WriteLine("Available commands:")
p.WriteLine(" /list")
p.WriteLine(" /quit")
p.WriteLine(" /join <channelID>")
p.WriteLine(" /join-invite <inviteCode>")
p.WriteLine(" /create-invite [channelID] [json:createInviteData]")
p.WriteLine(" /create-guild")
p.WriteLine(" /create-channel")
case "list":
p.list()
case "quit":
// refer to shouldExit().
case "join":
p.join(v)
case "join-invite":
p.joinInvite(v)
case "create-invite":
p.createInvite(v)
case "create-guild":
p.createGuild(v)
case "create-channel":
p.createChannel(v)
}
}
func (p *Prompt) prefix() (prefix string, use bool) {
name := p.state.ChannelName()
return fmt.Sprintf("[#%s] ", name), name != ""
}
const typeFrequency = 8 * time.Second
func (p *Prompt) onChange(in string, broke bool) (quit bool) {
// go-prompt is an awful library.
if broke {
return in == "/quit"
}
if id := p.state.ChannelID(); id.IsValid() {
now := time.Now()
if p.lastTyped.Add(typeFrequency).Before(now) {
p.lastTyped = now
go p.state.Typing(id)
}
}
return false
}
func (p *Prompt) WriteError(err error) {
p.writeMu.Lock()
defer p.writeMu.Unlock()
p.writer.SetColor(prompt.DarkRed, prompt.DefaultColor, false)
p.writer.WriteStr("Error: " + err.Error() + "\n")
p.writer.SetColor(prompt.DefaultColor, prompt.DefaultColor, false)
}
func (p *Prompt) WriteLine(line string) {
p.writeMu.Lock()
defer p.writeMu.Unlock()
p.writer.WriteStr(line + "\n")
}
func (p *Prompt) Writelnf(f string, v ...interface{}) {
p.writeMu.Lock()
defer p.writeMu.Unlock()
p.writer.WriteStr(fmt.Sprintf(f+"\n", v...))
}
func (p *Prompt) WriteColorlnf(color prompt.Color, f string, v ...interface{}) {
p.writeMu.Lock()
defer p.writeMu.Unlock()
p.writer.SetColor(color, prompt.DefaultColor, false)
p.writer.WriteStr(fmt.Sprintf(f+"\n", v...))
p.writer.SetColor(prompt.DefaultColor, prompt.DefaultColor, false)
}
func (p *Prompt) sendMessage(body string) {
channelID := p.state.ChannelID()
if !channelID.IsValid() {
p.WriteError(errors.New("not in any channel"))
return
}
_, err := p.state.SendText(channelID, body)
if err != nil {
p.WriteError(err)
}
}
func (p *Prompt) list() {
guilds, err := p.state.Guilds()
if err != nil {
p.WriteError(errors.Wrap(err, "failed to list all guilds"))
return
}
for _, guild := range guilds {
channels, err := p.state.Channels(guild.ID)
if err != nil {
p.WriteError(errors.Wrap(err, "failed to get channels"))
continue
}
p.Writelnf("Guild %d: %q:", guild.ID, guild.Name)
for _, ch := range channels {
p.Writelnf(" - %d: %q", ch.ID, ch.Name)
}
}
}
func (p *Prompt) join(body string) {
id, err := discord.ParseSnowflake(body)
if err != nil {
p.WriteError(errors.Wrap(err, "failed to parse channel ID"))
return
}
ch, err := p.state.Channel(discord.ChannelID(id))
if err != nil {
p.WriteError(errors.Wrap(err, "invalid channel"))
return
}
msgs, err := p.state.Messages(ch.ID)
if err != nil {
p.WriteError(errors.Wrap(err, "failed to get messages"))
return
}
for i := len(msgs) - 1; i >= 0; i-- {
msg := msgs[i]
p.Writelnf(
"[%s] %s: %s",
msg.Timestamp.Time().Local().Format(time.Kitchen),
msg.Author.Username, msg.Content,
)
}
p.state.mutex.Lock()
p.state.channelName = ch.Name
p.state.channelID = ch.ID
p.state.guildID = ch.GuildID
p.state.mutex.Unlock()
if ch.GuildID.IsValid() {
p.state.MemberState.Subscribe(ch.GuildID)
}
}
func (p *Prompt) joinInvite(body string) {
joined, err := p.state.JoinInvite(body)
if err != nil {
p.WriteError(errors.Wrap(err, "failed to join invite"))
return
}
if joined.Channel.ID.IsValid() {
p.state.mutex.Lock()
p.state.channelName = joined.Channel.Name
p.state.channelID = joined.Channel.ID
p.state.guildID = joined.Guild.ID
p.state.mutex.Unlock()
}
p.Writelnf(
"Joined guild %q (%d) into channel %q (%d).",
joined.Guild.Name, joined.Guild.ID, joined.Channel.Name, joined.Channel.ID,
)
}
func (p *Prompt) createInvite(body string) {
parts := strings.SplitN(body, " ", 2)
inviteChannel := p.state.ChannelID()
var inviteData api.CreateInviteData
if parts[0] != "" {
id, err := discord.ParseSnowflake(parts[0])
if err != nil {
p.WriteError(errors.Wrap(err, "failed to parse channelID"))
return
}
inviteChannel = discord.ChannelID(id)
}
if len(parts) == 2 {
if err := json.Unmarshal([]byte(parts[1]), &inviteData); err != nil {
p.WriteError(errors.Wrap(err, "failed to parse invite data JSON"))
return
}
}
inv, err := p.state.CreateInvite(inviteChannel, inviteData)
if err != nil {
p.WriteError(errors.Wrap(err, "failed to create invite"))
return
}
p.Writelnf("Invite created: %q", inv.Code)
}
func (p *Prompt) createGuild(body string) {
g, err := p.state.CreateGuild(api.CreateGuildData{
Name: body,
})
if err != nil {
p.WriteError(errors.Wrap(err, "failed to create guild"))
return
}
p.Writelnf("Guild with ID %d created", g.ID)
}
func (p *Prompt) createChannel(body string) {
ch, err := p.state.CreateChannel(p.state.GuildID(), api.CreateChannelData{
Name: body,
})
if err != nil {
p.WriteError(errors.Wrap(err, "failed to create channel"))
return
}
p.Writelnf("Channel with ID %d created", ch.ID)
}