-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (151 loc) · 4.39 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
176
177
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var s *discordgo.Session
var (
commands = []discordgo.ApplicationCommand{
{
Name: "mfuse",
Description: "Test your morfuse scripts.",
},
}
commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"mfuse": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
CustomID: "mfuse_" + i.Interaction.Member.User.ID,
Title: "Morfuse",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "mfuse",
Label: "Write here your morfuse script to test.",
Style: discordgo.TextInputParagraph,
Required: true,
MaxLength: 2000,
},
},
},
},
},
})
if err != nil {
log.Fatalf("Error: %v", err)
}
},
}
)
func main() {
var err error
err = godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Bot parameters
var (
TOKEN = os.Getenv("TOKEN") // get token from .env
CLIENT_ID = os.Getenv("CLIENT_ID") // get bot client id from .env
GUILD_ID = os.Getenv("GUILD_ID") // get guild id from .env
)
s, err = discordgo.New("Bot " + TOKEN)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionModalSubmit:
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "**Sent :white_check_mark:**",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Fatalf("Error: %v", err)
}
data := i.ModalSubmitData()
if !strings.HasPrefix(data.CustomID, "mfuse") {
return
}
textValue := data.Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
morfuseResponse := morfuse(textValue)
username := i.Member.User.Username
_, err = s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{
Title: ":green_circle: Script executed",
Description: fmt.Sprintf("```\n%s```", morfuseResponse),
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("Execution time: %s\nRequested By: %s • %s", elapsed, username, time.Now().Format("03:04 PM")),
},
})
if err != nil {
log.Fatalf("Error: %v", err)
}
}
})
cmdIDs := make(map[string]string, len(commands))
for _, cmd := range commands {
rcmd, err := s.ApplicationCommandCreate(CLIENT_ID, GUILD_ID, &cmd)
if err != nil {
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
}
cmdIDs[rcmd.ID] = rcmd.Name
}
err = s.Open()
s.UpdateGameStatus(0, "/mfuse")
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Graceful shutdown")
for id, name := range cmdIDs {
err := s.ApplicationCommandDelete(CLIENT_ID, GUILD_ID, id)
if err != nil {
log.Fatalf("Cannot delete slash command %q: %v", name, err)
}
}
}
var start time.Time
var elapsed time.Duration
func morfuse(script string) string {
start = time.Now() // get current time
os.Setenv("LD_LIBRARY_PATH", "morfuse")
cmd := exec.Command("morfuse/mfuse", script) // compile and execute the script using the Morfuse executable
out := catchOutput(cmd) // get output
return out
}
func catchOutput(cmd *exec.Cmd) string {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
elapsed = time.Since(start) // get elapsed time
if err == nil {
return stdout.String() // print morfuse execution result
} else {
return stderr.String() // print morfuse execution result error
}
}