This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
command.go
188 lines (169 loc) · 3.83 KB
/
command.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
package main
import (
"fmt"
"strings"
)
var (
senderRegex = strings.NewReplacer()
cmdHistory = make([]string, 0, 256)
)
// Commands contains multiple commands
type Commands []Command
// Command contains a command's info
type Command struct {
Command string
Function func([]string)
Description string
}
var commands = Commands{
Command{
Command: "/goto",
Function: gotoChannel,
Description: "[channel name] - jumps to a channel",
},
Command{
Command: "/editor",
Function: commandEditor,
Description: "Pop up $EDITOR to send a message <C-e>",
},
Command{
Command: "/mentions",
Function: commandMentions,
Description: "shows the last mentions",
},
Command{
Command: "/nick",
Function: changeSelfNick,
Description: "[nickname] - changes nickname for the current guild",
},
Command{
Command: "/status",
Function: setStatus,
Description: "[online|busy|away|invisible] - sets your status",
},
Command{
Command: "/edit",
Function: editMessage,
Description: "[n:int optional] - edits the latest n message of yours",
},
Command{
Command: "/delete",
Function: deleteMessage,
Description: "[messageID:int] - deletes the message",
},
Command{
Command: "/presence",
Function: setGame,
Description: "[string] - sets your \"Playing\" or \"Listening to\" presence, empty to reset",
},
Command{
Command: "/react",
Function: reactMessage,
Description: "[messageID:int] [emoji:string] - toggle reaction on a message",
},
Command{
Command: "/upload",
Function: uploadFile,
Description: "[file path] - uploads file",
},
Command{
Command: "/heated",
Function: cmdHeated,
Description: "warns you when a message is sent, regardless of settings",
},
Command{
Command: "/copy",
Function: matchCopyMessage,
Description: "[n:int] - copies the entire last n message",
},
Command{
Command: "/highlight",
Function: highlightMessage,
Description: "[ID:int64] - highlights the message ID if possible",
},
Command{
Command: "/dm",
Function: makeDirectMessage,
Description: "[@mention] - starts a new direct message",
},
Command{
Command: "/block",
Function: blockUser,
Description: "[@mention] - blocks someone",
},
Command{
Command: "/unblock",
Function: unblockUser,
Description: "[@mention] - unblocks someone",
},
Command{
Command: "/copytoken",
Function: commandCopyToken,
Description: "prints your token",
},
Command{
Command: "/debug",
Function: commandDebug,
Description: "prints extra debug info",
},
Command{
Command: "/quit",
Function: commandExit,
Description: "quits <C-c>",
},
}
func commandExit(text []string) {
app.Stop()
}
// CommandHandler .
func CommandHandler() {
text := input.GetText()
if text == "" {
return
}
defer input.SetText("")
if len(cmdHistory) >= 256 {
cmdHistory = cmdHistory[255:]
}
cmdHistory = append(
cmdHistory, text,
)
switch {
case strings.HasPrefix(text, "s/"):
go editMessageRegex(text)
case strings.HasPrefix(text, "/"):
f := strings.Fields(text)
if len(f) < 0 {
return
}
for _, cmd := range commands {
if f[0] == cmd.Command && cmd.Function != nil {
go func() {
defer func() {
if r := recover(); r != nil {
Warn(fmt.Sprintf("%v", r))
}
}()
cmd.Function(f)
}()
return
}
}
fallthrough
default:
// Trim literal backslash, in case "\/actual message"
text = strings.TrimPrefix(text, `\`)
if Channel == nil {
Message("You're not in a channel!")
return
}
go func(text string) {
_, err := d.ChannelMessageSend(Channel.ID, processString(text))
if err != nil {
Warn("Failed to send message:\n" + text + "\nError: " + err.Error())
}
messagesView.ScrollToEnd()
app.Draw()
}(text)
}
}