-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommands_query.go
323 lines (293 loc) · 8.56 KB
/
commands_query.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
/*
DiscordConsole is a software aiming to give you full control over accounts, bots and webhooks!
Copyright (C) 2020 Mnpn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"io"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/jD91mZM2/stdutil"
)
func commandsQuery(session *discordgo.Session, cmd string, args []string, nargs int, w io.Writer) (returnVal string) {
switch cmd {
case "read":
if nargs < 1 {
stdutil.PrintErr("read <message id> [property]", nil)
return
}
if loc.channel == nil {
stdutil.PrintErr(tl("invalid.channel"), nil)
return
}
msgID := args[0]
var msg *discordgo.Message
var err error
if strings.EqualFold(msgID, "cache") {
if cacheRead == nil {
stdutil.PrintErr(tl("invalid.cache"), nil)
return
}
msg = cacheRead
} else {
msg, err = getMessage(session, loc.channel.ID, msgID)
if err != nil {
stdutil.PrintErr(tl("failed.msg.query"), err)
return
}
cacheRead = msg
}
property := ""
if len(args) >= 2 {
property = strings.ToLower(args[1])
}
switch property {
case "":
printMessage(session, msg, false, loc.guild, loc.channel, w)
case "text":
returnVal = msg.Content
case "channel":
returnVal = msg.ChannelID
case "timestamp":
t, err := timestamp(msg)
if err != nil {
stdutil.PrintErr(tl("failed.timestamp"), err)
return
}
returnVal = t
case "author":
returnVal = msg.Author.ID
case "author_email":
returnVal = msg.Author.Email
case "author_name":
returnVal = msg.Author.Username
case "author_avatar":
returnVal = msg.Author.Avatar
case "author_bot":
returnVal = strconv.FormatBool(msg.Author.Bot)
case "embed":
embed := "{}"
if len(msg.Embeds) >= 1 {
embedBytes, err := json.MarshalIndent(msg.Embeds[0], "", "\t")
if err != nil {
stdutil.PrintErr("Failed to make embed into JSON", err)
return
}
embed = string(embedBytes)
}
returnVal = embed
default:
stdutil.PrintErr(tl("invalid.value"), nil)
}
lastUsedMsg = msg.ID
if returnVal != "" {
writeln(w, returnVal)
}
case "info":
if nargs < 1 {
stdutil.PrintErr("info <user/guild/channel/settings> (for user: <id/@me>) [property] (or info u/g/c/s)", nil)
return
}
switch strings.ToLower(args[0]) {
case "channel", "c":
if loc.channel == nil {
stdutil.PrintErr(tl("invalid.channel"), nil)
return
}
values := chan2array(loc.channel)
if nargs < 2 {
for _, keyval := range values {
writeln(w, keyval.String())
}
} else {
var ok bool
returnVal, ok = findValByKey(values, args[1])
if !ok {
stdutil.PrintErr(tl("invalid.value"), nil)
return
}
writeln(w, returnVal)
}
case "guild", "g":
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
values := guild2array(loc.guild)
if nargs < 2 {
for _, keyval := range values {
writeln(w, keyval.String())
}
} else {
var ok bool
returnVal, ok = findValByKey(values, args[1])
if !ok {
stdutil.PrintErr(tl("invalid.value"), nil)
return
}
writeln(w, returnVal)
}
case "user", "u":
if nargs < 2 {
stdutil.PrintErr("info user/u <id/@me> [property]", nil)
return
}
id := args[1]
var keyvals []*keyval
if strings.EqualFold(id, "cache") {
if cacheUser == nil {
stdutil.PrintErr(tl("invalid.cache"), nil)
return
}
keyvals = cacheUser
} else {
user, err := session.User(id)
if err != nil {
stdutil.PrintErr(tl("failed.user"), err)
return
}
keyvals = user2array(user)
cacheUser = keyvals
}
if nargs < 3 {
for _, keyval := range keyvals {
writeln(w, keyval.String())
}
} else {
var ok bool
returnVal, ok = findValByKey(keyvals, args[2])
if !ok {
stdutil.PrintErr(tl("invalid.value"), nil)
return
}
writeln(w, returnVal)
}
case "settings", "s":
if userType == typeBot {
stdutil.PrintErr(tl("invalid.onlyfor.users"), nil)
return
}
settings, err := session.UserSettings()
if err != nil {
stdutil.PrintErr(tl("failed.settings"), err)
return
}
values := settings2array(settings)
if nargs < 2 {
for _, keyval := range values {
writeln(w, keyval.String())
}
} else {
var ok bool
returnVal, ok = findValByKey(values, args[1])
if !ok {
stdutil.PrintErr(tl("invalid.value"), nil)
return
}
writeln(w, returnVal)
}
default:
stdutil.PrintErr("info <user/guild/channel/settings> (for user: <id/@me>) [property] (or info u/g/c/s)", nil)
}
}
return
}
func guild2array(guild *discordgo.Guild) []*keyval {
return []*keyval{
&keyval{"ID", guild.ID},
&keyval{"Name", guild.Name},
&keyval{"Icon", guild.Icon},
&keyval{"Region", guild.Region},
&keyval{"Owner", guild.OwnerID},
&keyval{"Join messages", guild.SystemChannelID},
&keyval{"Widget channel", guild.WidgetChannelID},
&keyval{"AFK channel", guild.AfkChannelID},
&keyval{"AFK timeout", strconv.Itoa(guild.AfkTimeout)},
&keyval{"Members", strconv.Itoa(guild.MemberCount)},
&keyval{"Verification", typeVerifications[guild.VerificationLevel]},
&keyval{"Admin MFA", typeMfa[guild.MfaLevel]},
&keyval{"Explicit Content Filter", typeContentFilter[guild.ExplicitContentFilter]},
&keyval{"Unavailable", strconv.FormatBool(guild.Unavailable)},
}
}
func chan2array(channel *discordgo.Channel) []*keyval {
return []*keyval{
&keyval{"ID", channel.ID},
&keyval{"Guild", channel.GuildID},
&keyval{"Name", channel.Name},
&keyval{"Topic", channel.Topic},
&keyval{"Type", typeChannel[channel.Type]},
&keyval{"NSFW", strconv.FormatBool(channel.NSFW)},
&keyval{"Parent category", channel.ParentID},
&keyval{"Last message", channel.LastMessageID},
&keyval{"Bitrate", strconv.Itoa(channel.Bitrate)},
&keyval{"User limit", strconv.Itoa(channel.UserLimit)},
}
}
func user2array(user *discordgo.User) []*keyval {
return []*keyval{
&keyval{"ID", user.ID},
&keyval{"Email", user.Email},
&keyval{"Name", user.Username},
&keyval{"Discrim", user.Discriminator},
&keyval{"Avatar", user.Avatar},
&keyval{"Avatar URL", user.AvatarURL("1024")},
&keyval{"Verified", strconv.FormatBool(user.Verified)},
&keyval{"MFA Enabled", strconv.FormatBool(user.MFAEnabled)},
&keyval{"Bot", strconv.FormatBool(user.Bot)},
}
}
func settings2array(settings *discordgo.Settings) []*keyval {
return []*keyval{
&keyval{"Theme", settings.Theme},
&keyval{"Compact", strconv.FormatBool(settings.MessageDisplayCompact)},
&keyval{"Locale", settings.Locale},
&keyval{"TTS", strconv.FormatBool(settings.EnableTtsCommand)},
&keyval{"Convert emotes", strconv.FormatBool(settings.ConvertEmoticons)},
&keyval{"Attachments", strconv.FormatBool(settings.InlineAttachmentMedia)},
&keyval{"Media embeds", strconv.FormatBool(settings.InlineEmbedMedia)},
&keyval{"Show embeds", strconv.FormatBool(settings.RenderEmbeds)},
&keyval{"Show current game", strconv.FormatBool(settings.ShowCurrentGame)},
&keyval{"Dev mode", strconv.FormatBool(settings.DeveloperMode)},
&keyval{"Platform accounts", strconv.FormatBool(settings.DetectPlatformAccounts)},
}
}
func invite2array(invite *discordgo.Invite) []*keyval {
values := make([]*keyval, 0)
if invite.Inviter != nil {
for _, keyval := range user2array(invite.Inviter) {
keyval.Key = "Inviter_" + keyval.Key
values = append(values, keyval)
}
}
for _, keyval := range guild2array(invite.Guild) {
keyval.Key = "Guild_" + keyval.Key
values = append(values, keyval)
}
for _, keyval := range chan2array(invite.Channel) {
keyval.Key = "Channel_" + keyval.Key
values = append(values, keyval)
}
values = append(values,
&keyval{"Created_at", string(invite.CreatedAt)},
&keyval{"Max_age", (time.Duration(invite.MaxAge) * time.Second).String()},
&keyval{"Max_uses", strconv.Itoa(invite.MaxUses)},
&keyval{"Uses", strconv.Itoa(invite.Uses)},
&keyval{"Revoked", strconv.FormatBool(invite.Revoked)},
&keyval{"Temporary", strconv.FormatBool(invite.Temporary)},
)
return values
}