/
app.go
197 lines (170 loc) · 5.33 KB
/
app.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
package ui
import (
"sort"
"strings"
"github.com/ayntgl/astatine"
"github.com/craftxbox/discordo/config"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type App struct {
*tview.Application
MainFlex *tview.Flex
GuildsList *GuildsList
ChannelsTreeView *ChannelsTreeView
MessagesTextView *MessagesTextView
MessageInputField *MessageInputField
Session *astatine.Session
SelectedChannel *astatine.Channel
Config *config.Config
SelectedMessage int
}
func NewApp(token string, c *config.Config) *App {
app := &App{
MainFlex: tview.NewFlex(),
Session: astatine.New(token),
Config: c,
SelectedMessage: -1,
}
app.Session.State.MaxMessageCount = 5000
app.Session.State.TrackMembers = true
app.Session.State.TrackChannels = true
app.Session.State.TrackThreads = true
app.Session.State.TrackEmojis = true
app.Session.State.TrackMembers = true
app.Session.State.TrackThreadMembers = true
app.Session.State.TrackRoles = true
app.Session.State.TrackVoice = true
app.Session.State.TrackPresences = true
app.GuildsList = NewGuildsList(app)
app.ChannelsTreeView = NewChannelsTreeView(app)
app.MessagesTextView = NewMessagesTextView(app)
app.MessageInputField = NewMessageInputField(app)
app.Application = tview.NewApplication()
app.EnableMouse(app.Config.General.Mouse)
app.SetInputCapture(app.onInputCapture)
InitCommands()
return app
}
func (app *App) Connect() error {
// For user accounts, all of the guilds, the user is in, are dispatched in the READY gateway event.
// Whereas, for bot accounts, the guilds are dispatched discretely in the GUILD_CREATE gateway events.
if !strings.HasPrefix(app.Session.Identify.Token, "Bot") {
app.Session.UserAgent = app.Config.General.UserAgent
app.Session.Identify.Compress = false
app.Session.Identify.LargeThreshold = 0
app.Session.Identify.Intents = 0
app.Session.Identify.Properties = astatine.IdentifyProperties{
OS: app.Config.General.Identify.Os,
Browser: app.Config.General.Identify.Browser,
}
app.Session.AddHandlerOnce(app.onSessionReady)
}
app.Session.AddHandler(app.onSessionGuildCreate)
app.Session.AddHandler(app.onSessionGuildDelete)
app.Session.AddHandler(app.onSessionMessageCreate)
app.Session.AddHandler(app.onSessionMessageEdit)
app.Session.AddHandler(app.onSessionMessageDelete)
return app.Session.Open()
}
func (app *App) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
if app.MessageInputField.HasFocus() {
return e
}
if app.MainFlex.GetItemCount() != 0 {
switch e.Name() {
case app.Config.Keys.ToggleGuildsList:
app.SetFocus(app.GuildsList)
return nil
case app.Config.Keys.ToggleChannelsTreeView:
app.SetFocus(app.ChannelsTreeView)
return nil
case app.Config.Keys.ToggleMessagesTextView:
app.SetFocus(app.MessagesTextView)
return nil
case app.Config.Keys.ToggleMessageInputField:
app.SetFocus(app.MessageInputField)
return nil
}
}
return e
}
func (app *App) DrawMainFlex() {
leftFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(app.GuildsList, 10, 1, false).
AddItem(app.ChannelsTreeView, 0, 1, false)
rightFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(app.MessagesTextView, 0, 1, false).
AddItem(app.MessageInputField, 3, 1, false)
app.MainFlex.
AddItem(leftFlex, 0, 1, false).
AddItem(rightFlex, 0, 4, false)
app.SetRoot(app.MainFlex, true)
}
func (app *App) onSessionReady(_ *astatine.Session, r *astatine.Ready) {
sort.Slice(r.Guilds, func(a, b int) bool {
found := false
for _, guildID := range r.Settings.GuildPositions {
if found && guildID == r.Guilds[b].ID {
return true
}
if !found && guildID == r.Guilds[a].ID {
found = true
}
}
return false
})
for _, g := range r.Guilds {
app.GuildsList.AddItem(g.Name, "", 0, nil)
}
}
func (app *App) onSessionGuildCreate(_ *astatine.Session, g *astatine.GuildCreate) {
app.GuildsList.AddItem(g.Name, "", 0, nil)
app.Draw()
}
func (app *App) onSessionGuildDelete(_ *astatine.Session, g *astatine.GuildDelete) {
items := app.GuildsList.FindItems(g.BeforeDelete.Name, "", false, false)
if len(items) != 0 {
app.GuildsList.RemoveItem(items[0])
}
app.Draw()
}
func (app *App) onSessionMessageCreate(_ *astatine.Session, m *astatine.MessageCreate) {
if app.SelectedChannel != nil && app.SelectedChannel.ID == m.ChannelID {
app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, m.Message)
_, err := app.MessagesTextView.Write(buildMessage(app, m.Message))
if err != nil {
return
}
if len(app.MessagesTextView.GetHighlights()) == 0 {
app.MessagesTextView.ScrollToEnd()
}
}
}
func (app *App) onSessionMessageDelete(_ *astatine.Session, m *astatine.MessageDelete) {
if app.SelectedChannel != nil && app.SelectedChannel.ID == m.ChannelID {
_, err := app.MessagesTextView.Write(buildDelete(app, m))
if err != nil {
return
}
if len(app.MessagesTextView.GetHighlights()) == 0 {
app.MessagesTextView.ScrollToEnd()
}
}
}
func (app *App) onSessionMessageEdit(_ *astatine.Session, m *astatine.MessageUpdate) {
if len(m.Content) < 1 {
return
}
if app.SelectedChannel != nil && app.SelectedChannel.ID == m.ChannelID {
_, err := app.MessagesTextView.Write(buildEdit(app, m))
if err != nil {
return
}
if len(app.MessagesTextView.GetHighlights()) == 0 {
app.MessagesTextView.ScrollToEnd()
}
}
}