Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (97 loc) · 2.47 KB
/
Copy pathmain.go
File metadata and controls
114 lines (97 loc) · 2.47 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"regexp"
"syscall"
"github.com/bwmarrin/discordgo"
)
// Variables used for command line parameters
var (
delete = regexp.MustCompile("\\`\\`\\`go|(\\`\\`\\`)")
noLoops = regexp.MustCompile("for\\n?\\{")
sintaxError = &discordgo.MessageEmbed{
Title: "SomethingWrong",
Description: "check your code",
}
)
func start() string {
// opent the settings
fs, err := os.Open("config.json")
if err != nil {
fmt.Println("something is wrong", err)
return "error"
}
defer fs.Close()
byteValue, _ := ioutil.ReadAll(fs)
var conf map[string]string
json.Unmarshal([]byte(byteValue), &conf)
return "Bot " + conf["token"]
}
func main() {
dg, err := discordgo.New(start())
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
fmt.Println("bot on ready")
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages)
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if len(m.Content) > 9 {
if m.Content[:9] == "$compile " && delete.MatchString(m.Content) && !noLoops.MatchString(m.Content) {
programName := "clientPrograms/" + m.ID + ".go"
//make the programm
fs, err := os.Create(programName)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "we cant create the archive")
log.Println(err)
return
}
//write the programm
defer fs.Close()
content := delete.ReplaceAllString(m.Content[9:], "")
fmt.Println(content)
_, err = fs.WriteString(content)
if err != nil {
s.ChannelMessageSendEmbed(m.ChannelID, sintaxError)
log.Println(err)
return
}
// compile the programm
output, err := exec.Command("go", "run", programName).Output()
outputEmbed := &discordgo.MessageEmbed{
Title: "Your programm is fine",
Description: "```" + string(output) + "```",
}
s.ChannelMessageSendEmbed(m.ChannelID, outputEmbed)
if err != nil {
s.ChannelMessageSendEmbed(m.ChannelID, sintaxError)
os.Remove(programName)
}
// remove the programm
err = os.Remove(programName)
if err != nil {
fmt.Println("something is wrong", err)
}
}
}
}