Files
youtube-downloader/downloader/discordNotifier.go

52 lines
1.2 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
func NotifyDiscordErrorless(message string) {
err := NotifyDiscord(message)
if err != nil {
log.Printf("Error notifying discord: %v", err)
}
}
func NotifyDiscord(message string) error {
webhookURL := os.Getenv("YTDL_DISCORD_WEBHOOK_URL")
if webhookURL == "" {
return fmt.Errorf("error notifying discord: webhook URL is not set in environment variables")
}
jsonData := map[string]string{"content": message}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return fmt.Errorf("error notifying discord: error marshalling JSON: %v", err)
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonBytes))
if err != nil {
return fmt.Errorf("error notifying discord: error creating request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error notifying discord: error sending request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error notifying discord: error reading response body: %v", err)
}
log.Printf("Response from Discord: %s", string(body))
return nil
}