/
cmd-minecraft.go
347 lines (310 loc) · 8.54 KB
/
cmd-minecraft.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"4d63.com/tz"
"github.com/Syfaro/minepong"
"github.com/bwmarrin/discordgo"
humanize "github.com/dustin/go-humanize"
"github.com/minotar/minecraft"
)
type MCServerDescription struct {
Extra []MCServerDescriptionExtra `json:"extra"`
Text string `json:"text"`
}
type MCServerDescriptionExtra struct {
Text string `json:"text"`
Color string `json:"color"`
Obfuscated bool `json:"obfuscated"`
Bold bool `json:"bold"`
Strikethrough bool `json:"strikethrough"`
Underline bool `json:"underline"`
Italic bool `json:"italic"`
}
type MCOriginalUser struct {
ID string `json:"id"`
Username string `json:"name"`
}
type MCUsernameHistory struct {
Username string `json:"name"`
TimeChanged int64 `json:"changedToAt"`
}
func GetAPIOldProfile(mc *minecraft.Minecraft, username string) (*minecraft.APIProfileResponse, error) {
originalUser := &MCOriginalUser{}
url := fmt.Sprintf("https://api.mojang.com/users/profiles/minecraft/%s?at=0", username)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", mc.UserAgent)
res, err := mc.Client.Do(req)
if err != nil {
return nil, err
}
err = unmarshal(res, originalUser)
if err != nil {
return nil, err
}
if originalUser.ID == "" || originalUser.Username == "" {
return nil, errors.New("not found")
}
profile, err := mc.GetAPIProfile(originalUser.Username)
return &profile, err
}
func GetAPIUsernameHistory(mc *minecraft.Minecraft, uuid string) ([]MCUsernameHistory, error) {
history := make([]MCUsernameHistory, 0)
url := fmt.Sprintf("https://api.mojang.com/user/profiles/%s/names", uuid)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", mc.UserAgent)
res, err := mc.Client.Do(req)
if err != nil {
return nil, err
}
err = unmarshal(res, &history)
if err != nil {
return nil, err
}
return history, nil
}
func commandMinecraft(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
timezone := userSettings[env.User.ID].Timezone
if timezone == "" {
return NewErrorEmbed("Minecraft Error", "Please set a timezone first!\n\nEx: ``"+env.BotPrefix+"user timezone America/New_York``")
}
location, err := tz.LoadLocation(timezone)
if err != nil {
return NewErrorEmbed("Minecraft Error", "You have an invalid timezone set, please set a new one first!\n\nEx: ``"+env.BotPrefix+"user timezone America/New_York``")
}
switch args[0] {
case "user", "player", "avatar", "skin", "uuid":
minecraftAPI := minecraft.NewMinecraft()
profileAPI, err := minecraftAPI.GetAPIProfile(args[1])
if err != nil {
oldProfileAPI, err := GetAPIOldProfile(minecraftAPI, args[1])
if err != nil {
return NewErrorEmbed("Minecraft Error", "Invalid or unknown username ``"+args[1]+"``.")
}
profileAPI = *oldProfileAPI
}
minecraftEmbed := NewEmbed().
SetTitle("Minecraft - " + profileAPI.User.Username).
SetImage("https://minotar.net/armor/body/" + profileAPI.User.UUID + ".png").
SetFooter("UUID: " + profileAPI.User.UUID).
SetColor(0x5B8731)
if profileAPI.Legacy {
minecraftEmbed.AddField("Account Type", "Legacy")
} else if profileAPI.Demo {
minecraftEmbed.AddField("Account Type", "Demo")
}
usernameHistory, _ := GetAPIUsernameHistory(minecraftAPI, profileAPI.User.UUID)
if len(usernameHistory) > 0 {
history := make([]string, 0)
for _, username := range usernameHistory {
if username.TimeChanged != 0 {
timeChanged := time.Unix(username.TimeChanged, 0).In(location)
history = append(history, username.Username+" (changed "+humanize.Time(timeChanged.In(location))+" at ``"+timeChanged.In(location).String()+"``)")
} else {
history = append(history, username.Username)
}
}
if len(history) > 0 {
minecraftEmbed.AddField("Username History", strings.Join(history, "\n"))
}
}
profileSession, err := minecraftAPI.GetSessionProfile(profileAPI.User.UUID)
if err == nil {
for _, property := range profileSession.Properties {
switch property.Name {
case "textures":
continue
}
minecraftEmbed.AddField("Property: "+property.Name, property.Value)
}
}
return minecraftEmbed.MessageEmbed
case "server", "host", "ip", "ping":
host := args[1]
if ipPort := strings.Split(args[1], ":"); len(ipPort) == 1 {
host += ":25565"
}
server, err := minepong.Ping(host)
if err != nil {
return NewErrorEmbed("Minecraft Error", "Invalid or unknown server ``"+args[1]+"``.")
}
title := "Minecraft - " + args[1]
if server.ResolvedHost != "" {
title = "Minecraft - " + server.ResolvedHost
}
version := strconv.Itoa(server.Version.Protocol)
if server.Version.Name != "" {
version = server.Version.Name + "\n" + version
}
minecraftEmbed := NewEmbed().
SetTitle(title).
AddField("Version", version).
AddField("Players", strconv.Itoa(server.Players.Online)+"/"+strconv.Itoa(server.Players.Max)).
AddField("Description", mcFormat(server.Description)).
SetColor(0x5B8731)
if server.FavIcon != "" {
hostReplaced := strings.Replace(host, ":", "/", 1)
minecraftEmbed.SetThumbnail("https://api.minetools.eu/favicon/" + hostReplaced)
}
return minecraftEmbed.MessageEmbed
}
return NewErrorEmbed("Minecraft Error", "Unknown command ``"+args[1]+"``.")
}
func mcFormat(desc interface{}) string {
switch v := desc.(type) {
case map[string]interface{}:
jsonData, err := json.Marshal(desc)
if err == nil {
var advancedDescription *MCServerDescription
if err := json.Unmarshal(jsonData, &advancedDescription); err == nil {
if len(advancedDescription.Extra) > 0 {
formattedDescription := ""
for _, extra := range advancedDescription.Extra {
text := strings.TrimPrefix(strings.TrimSuffix(extra.Text, " "), " ")
if extra.Bold {
text = "**" + text + "**"
}
if extra.Strikethrough {
text = "~~" + text + "~~"
}
if extra.Underline {
text = "__" + text + "__"
}
if extra.Italic {
text = "*" + text + "*"
}
formattedDescription += text + " "
}
return mcFormat(formattedDescription)
} else if advancedDescription.Text != "" {
return mcFormat(advancedDescription.Text)
}
}
}
case string:
if v != "" {
format := false
obfuscated := false
bold := false
strikethrough := false
underline := false
italic := false
formattedDescription := ""
for _, character := range v {
switch string(character) {
case "§":
format = true
obfuscated = false
continue
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f":
if format {
format = false
continue
}
case "k":
if format {
format = false
obfuscated = true
continue
}
case "l":
if format {
format = false
if bold == false {
bold = true
formattedDescription += "**"
}
continue
}
case "m":
if format {
format = false
if strikethrough == false {
strikethrough = true
formattedDescription += "~~"
}
continue
}
case "n":
if format {
format = false
if underline == false {
underline = true
formattedDescription += "__"
}
continue
}
case "o":
if format {
format = false
if italic == false {
italic = true
formattedDescription += "*"
}
continue
}
case "r":
if format {
format = false
if bold {
bold = false
formattedDescription += "**"
}
if strikethrough {
strikethrough = false
formattedDescription += "~~"
}
if underline {
underline = false
formattedDescription += "__"
}
if italic {
italic = false
formattedDescription += "*"
}
continue
}
case "\\":
formattedDescription += "\\"
continue
case "*":
formattedDescription += "\\*"
case "_":
formattedDescription += "\\_"
case "~":
formattedDescription += "\\~"
}
if obfuscated {
formattedDescription += "▓"
} else {
formattedDescription += string(character)
}
}
if bold {
formattedDescription += "**"
}
if strikethrough {
formattedDescription += "~~"
}
if underline {
formattedDescription += "__"
}
if italic {
formattedDescription += "*"
}
return formattedDescription
}
}
return ""
}