-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
175 lines (146 loc) · 3.48 KB
/
main.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
package main
import (
"errors"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"time"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
var prefix = "!gocaml"
const (
OCAML string = "ocaml"
Python string = "python"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Println(err)
}
gin.SetMode(gin.ReleaseMode)
}
func main() {
// db.Connect(os.Getenv("URI"), os.Getenv("DATABASE"))
d, err := discordgo.New("Bot " + os.Getenv("TOKEN"))
d.AddHandler(onMessage)
d.AddHandler(onMessageUpdate)
err = d.Open()
if err != nil {
log.Fatal(err)
}
log.Println("Bot is now running. Press CTRL-C to exit.")
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "success",
})
})
r.Run(":" + os.Getenv("PORT"))
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
d.Close()
}
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// don't process bot messages
if m.Author.ID == s.State.User.ID {
return
}
// isStats, userID := IsStats(m)
// if isStats {
// if userID == "" {
// users, err := models.GetUsers()
// if err != nil {
// log.Println(err)
// }
// s.ChannelMessageSend(m.Message.ChannelID, ParseStats(users))
// return
// }
// user, err := models.GetUser(userID)
// if err != nil {
// log.Println(err)
// }
// s.ChannelMessageSend(m.Message.ChannelID, ParseStats([]models.User{user}))
// }
isEval, code, lang := IsCodeEvaluation(m)
if !isEval {
return
}
result, err := evaluateCode(code, lang)
if err != nil {
s.ChannelMessageSend(m.Message.ChannelID, "**RUNTIME ERROR**\n```"+err.Error()+"```")
return
}
//format
var formatted string = result
if result != "" && lang == OCAML {
formatted = FormatEvaluation(result)
}
s.ChannelMessageSend(m.Message.ChannelID, "**Evaluation**:\n```"+lang+"\n"+formatted+"```")
// user, err := models.GetUser(m.Author.ID)
// if err == mgo.ErrNotFound {
// u := models.User{
// DiscordID: m.Author.ID,
// Username: m.Author.Username,
// Discriminator: m.Author.Discriminator,
// }
// user, err = u.Create()
// if err != nil {
// log.Println(err)
// }
// }
// if ContainsError(formatted) {
// log.Println(formatted)
// user.IncrementError()
// } else {
// user.IncrementSuccess()
// }
}
func onMessageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
message := discordgo.MessageCreate(*m)
onMessage(s, &message)
}
func evaluateCode(code string, lang string) (string, error) {
log.Println(code)
if lang == OCAML {
command := "echo \"" + code + "\" | ocaml -no-version"
process := exec.Command("bash", "-c", command)
terminated := false
time.AfterFunc(5*time.Second, func() {
if terminated {
return
}
p := exec.Command("bash", "-c", "pkill -f ocamlrun")
_, err := p.Output()
if err != nil {
log.Println(err)
}
})
out, err := process.Output()
terminated = true
return string(out), err
} else if lang == Python {
code = strings.Replace(code, "\\\"", "\"", -1)
err := VerifyPythonCode(code)
if err != nil {
return "", err
}
cmd := exec.Command("python3", "-c", code)
terminated := false
time.AfterFunc(5*time.Second, func() {
if terminated {
return
}
syscall.Kill(cmd.Process.Pid, syscall.SIGKILL)
})
out, err := cmd.CombinedOutput()
terminated = true
return string(out), err
}
return "", errors.New("Language not supported")
}