-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (76 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
93
94
package main
import (
"fmt"
"log"
"os"
"io"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"net/http"
)
var discord *discordgo.Session
func startDiscord() *discordgo.Session {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
discordKey := os.Getenv("DISCORD_KEY")
sess, err := discordgo.New("Bot " + discordKey)
if err != nil {
log.Fatal(err)
}
sess.Identify.Intents = discordgo.IntentsAllWithoutPrivileged
err = sess.Open()
if err != nil {
log.Fatal(err)
}
return sess
}
func homePage(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Cyber Esteban is running 🚀")
}
func activityMessage(w http.ResponseWriter, r *http.Request) {
channelID := os.Getenv("CHANNELID")
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
validAPIKey := os.Getenv("API_KEY")
apiKey := r.Header.Get("X-API-Key")
if apiKey != validAPIKey {
http.Error(w, "Invalid API key", http.StatusForbidden)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
message := string(body)
_, err = discord.ChannelMessageSend(channelID, message)
if err != nil {
http.Error(w, "Error sending Discord message", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Received and sent to Discord: %s", message)
}
func handleRequests() {
http.HandleFunc("/", homePage)
http.HandleFunc("/api", activityMessage)
fmt.Println("Starting HTTP server on :8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Printf("HTTP server error: %v\n", err)
}
}
func main() {
discord = startDiscord()
defer discord.Close()
fmt.Println("Bot is running")
go handleRequests()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}