-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaumlog.go
130 lines (117 loc) · 3.05 KB
/
aumlog.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)
// TODO: Reduce the complexity of this function (i.e. split it up).
func aumLog(s *discordgo.Session, m *discordgo.MessageCreate) {
if len(m.Attachments) == 0 || len(m.Attachments) > 1 {
return
}
if m.Attachments[0].Filename != "aum-log.txt" {
return
}
resp, err := http.Get(m.Attachments[0].URL)
if err != nil {
logrus.Errorf("Error while fetching AmongUsMenu log: %v", err)
return
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Errorf("Error while reading HTTP response body: %v", err)
return
}
logs := strings.Split(string(b), "\n")
r := struct {
commitHash string
branch string
buildType string
auVersion string
}{}
infoIndex := -1
for i, log := range logs {
if !strings.Contains(log, "[INFO - AUM - Run]") {
continue
}
infoIndex = i
break
}
if infoIndex == -1 && len(logs) < 5 {
message := "```"
for _, log := range logs {
message += log
}
message += "```"
_, err = s.ChannelMessageSendReply(m.ChannelID, message, m.MessageReference)
if err != nil {
logrus.Errorf("Error trying to send reply %v", err)
return
}
return
}
if infoIndex == -1 {
logrus.Errorf("Invalid log format: unable to find run info")
return
}
re := *regexp.MustCompile(`^\tBuild:\s(.*)$`)
if re.MatchString(strings.TrimSpace(logs[infoIndex+2])) {
// TODO: Make this into a Discord message response.
logrus.Errorf("Invalid log format: unable to find build type")
return
}
r.buildType = strings.Split(logs[infoIndex+2], ":")[1]
re = *regexp.MustCompile(`^\tCommit: (.*) - (.*)$`)
if re.MatchString(strings.TrimSpace(logs[infoIndex+3])) {
// TODO: Make this into a Discord message response.
logrus.Errorf("Invalid log format: unable to find git info")
return
}
git := strings.Split(logs[infoIndex+3], ":")[1]
r.commitHash = strings.TrimSpace(strings.Split(git, " - ")[0])
r.branch = strings.TrimSpace(strings.Split(git, " - ")[1])
re = *regexp.MustCompile(`^\tAmong Us Version: (.*)$`)
if re.MatchString(strings.TrimSpace(logs[infoIndex+4])) {
// TODO: Make this into a Discord message response.
logrus.Errorf("Invalid log format: unable to find among us version")
return
}
r.auVersion = strings.Split(logs[infoIndex+4], ":")[1]
embed := discordgo.MessageEmbed{
Title: "AmongUsMenu log",
Fields: []*discordgo.MessageEmbedField{
{
Name: "Build type",
Value: r.buildType,
Inline: true,
},
{
Name: "AU Version",
Value: r.auVersion,
Inline: true,
},
{
Name: "Git branch",
Value: r.branch,
Inline: true,
},
{
Name: "Git commit",
Value: fmt.Sprintf("[%s](https://github.com/BitCrackers/AmongUsMenu/commit/%s)", r.commitHash[:7], r.commitHash),
Inline: true,
},
},
}
_, err = s.ChannelMessageSendComplex(m.ChannelID, &discordgo.MessageSend{
Embed: &embed,
Reference: m.Reference(),
})
if err != nil {
logrus.Errorf("Error trying to send embed %v", err)
return
}
}