-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-bot.go
69 lines (57 loc) · 2.01 KB
/
discord-bot.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// DiscordPayload represents the JSON payload structure for sending a message to Discord
type DiscordPayload struct {
Content string `json:"content"`
}
// sendDiscordMessage sends a message to a Discord channel using the Discord API
func sendDiscordMessage(payload DiscordPayload) {
discord_token := config.DiscordBotToken
if discord_token == "" {
resultQueue <- TaskResult{application: "discord", status: false, reason: "DISCORD_OAUTH_TOKEN is not set"}
return
}
channelID := config.DiscordChannelID
if channelID == "" {
resultQueue <- TaskResult{application: "discord", status: false, reason: "DISCORD_CHANNEL_ID is not set"}
return
}
url := fmt.Sprintf("https://discord.com/api/v10/channels/%s/messages", channelID)
payloadBytes, err := json.Marshal(payload)
if err != nil {
resultQueue <- TaskResult{application: "discord", status: false, reason: fmt.Sprintf("failed to marshal payload: %v", err)}
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
resultQueue <- TaskResult{application: "discord", status: false, reason: fmt.Sprintf("failed to create request: %v", err)}
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bot "+discord_token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
resultQueue <- TaskResult{application: "discord", status: false, reason: fmt.Sprintf("failed to send request: %v", err)}
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
resultQueue <- TaskResult{application: "discord", status: false, reason: fmt.Sprintf("failed to read response body: %v", err)}
return
}
if resp.StatusCode != http.StatusOK {
resultQueue <- TaskResult{application: "discord", status: false, reason: fmt.Sprintf("received non-OK response: %s", body)}
return
} else {
resultQueue <- TaskResult{application: "discord", status: true, reason: "None"}
return
}
}