add discord webhook notification

This commit is contained in:
Philipp 2024-10-26 22:25:44 +02:00
parent 7d9e425cb6
commit 37f718a7a2
Signed by: Philipp
GPG key ID: 9EBD8439AFBAB750
4 changed files with 42 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"context"
"encoding/json"
"flag"
"io"
@ -11,6 +12,8 @@ import (
"strconv"
"strings"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/webhook"
"github.com/rumblefrog/go-a2s"
)
@ -19,7 +22,7 @@ type apiResponse struct {
Success bool `json:"success"`
UpToDate bool `json:"up_to_date"`
VersionIsListable bool `json:"version_is_listable"`
RequiredVersion int `json:"require_version"`
RequiredVersion int `json:"required_version"`
Message string `json:"message"`
} `json:"response"`
}
@ -32,14 +35,24 @@ func main() {
var port int
var name string
var command string
var discordwebhook string
// Defining Flags
flag.StringVar(&ip, "ip", "127.0.0.1", "Specify ip to use.")
flag.IntVar(&port, "port", 27015, "Specify port to use.")
flag.StringVar(&name, "name", "Server", "Specify Server Name to use in the Webhook.")
flag.StringVar(&command, "command", "", "Specify Command to run if the Server is not on the newest Version.")
flag.StringVar(&discordwebhook, "webhook", "", "Specify Discord Webhook URL to use.")
flag.Parse()
// Creating Discord Webhook Client
whclient, err := webhook.NewWithURL(discordwebhook)
defer whclient.Close(context.TODO())
if err != nil {
log.Fatal("Failed to create Webhook message...")
}
// Creating A2S Request to Server
client, err := a2s.NewClient(ip + ":" + strconv.FormatUint(uint64(port), 10))
if err != nil {
@ -64,6 +77,7 @@ func main() {
// Creating httpClient and sending Request to Steam API
httpClient := http.Client{}
info.Version = "123"
req, err := http.NewRequest(http.MethodGet, url+info.Version, nil)
if err != nil {
log.Fatal(err)
@ -101,6 +115,17 @@ func main() {
err := cmd.Run()
discordEmbed := []discord.Embed{}
discordEmbed = append(discordEmbed, discord.NewEmbedBuilder().
SetTitlef("Update for Server %s", name).
SetDescriptionf("Updating to the newest Server Version %d", response.Response.RequiredVersion).
SetFooterTextf("Current Version %s", info.Version).
Build())
if _, err := whclient.CreateEmbeds(discordEmbed); err != nil {
log.Fatal("error sending message to webhook %s", err)
}
if err != nil {
log.Fatal(err)
}