-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (75 loc) · 2.01 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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"codeberg.org/redson/sedcord-go/events"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil && os.Getenv("TOKEN") == "" {
log.Fatal("Error loading the .env file:", err)
}
token := os.Getenv("TOKEN")
// Creating a new Discord section
dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord section:", err)
return
}
dg.AddHandler(events.Ready)
dg.AddHandler(messageCreate)
// Open a websocket connection to Discord
if err := dg.Open(); err != nil {
fmt.Println("Error oppening connection:", err)
return
}
fmt.Println("Running, press CTRL + C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func sed(sedargs string, inputmsg string) (string, error) {
cmd := exec.Command("sed", "--sandbox", sedargs)
stdin, err := cmd.StdinPipe()
if err != nil {
return "", errors.New("Could not connect to Stdin")
}
go func() {
defer stdin.Close()
io.WriteString(stdin, inputmsg)
}()
out, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New("Could not get Stdout")
}
return string(out), nil
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
// If message has prefix and is a reply to another message.
if strings.Contains(m.Content, "!s") && m.Message.Type == 19 {
contentSlice := strings.Split(m.Content, " ")
contentSlice = append(contentSlice[:0], contentSlice[1:]...)
stringslice := strings.Join(contentSlice, " ")
text, err := sed(stringslice, m.ReferencedMessage.Content)
if err != nil {
s.ChannelMessageSendReply(m.ChannelID, "Error: "+err.Error(), m.MessageReference)
return
}
if text == "" {
text = "[Empty message]"
}
s.ChannelMessageSendReply(m.ChannelID, text, m.ReferencedMessage.Reference())
}
}