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

@ -2,8 +2,6 @@
## How to use
`tf2-checker -ip 88.198.49.46 -port 27015 -name "TF2-DM #1" -command "docker compose restart tf2dm"`
`tf2-checker -ip 88.198.49.46 -port 27015 -name "TF2-DM #1" -command "docker compose restart tf2dm" -webhook "https://discord.com/webhook"`
## TODO:
- add Discord Webhook Notifiction on Update

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)
}

8
go.mod
View file

@ -2,4 +2,10 @@ module git.snrd.eu/spenny.tf/tf2-checker
go 1.23.2
require github.com/rumblefrog/go-a2s v1.0.2 // indirect
require (
github.com/disgoorg/disgo v0.18.13 // indirect
github.com/disgoorg/json v1.2.0 // indirect
github.com/disgoorg/snowflake/v2 v2.0.3 // indirect
github.com/rumblefrog/go-a2s v1.0.2 // indirect
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
)

8
go.sum
View file

@ -1,2 +1,10 @@
github.com/disgoorg/disgo v0.18.13 h1:RswLnakVS+RbCACBfK7mLv3YGdZh625N2SMnngnqd+Q=
github.com/disgoorg/disgo v0.18.13/go.mod h1:wZ/ZW6x43QivIVrYrJxwSeFbIbrMqpi5vAU1ovsod8o=
github.com/disgoorg/json v1.2.0 h1:6e/j4BCfSHIvucG1cd7tJPAOp1RgnnMFSqkvZUtEd1Y=
github.com/disgoorg/json v1.2.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/snowflake/v2 v2.0.3 h1:3B+PpFjr7j4ad7oeJu4RlQ+nYOTadsKapJIzgvSI2Ro=
github.com/disgoorg/snowflake/v2 v2.0.3/go.mod h1:W6r7NUA7DwfZLwr00km6G4UnZ0zcoLBRufhkFWgAc4c=
github.com/rumblefrog/go-a2s v1.0.2 h1:rT/QP/B+h2R9/3PEfmOkWPdHnEKExskOMPTTkeX+vuA=
github.com/rumblefrog/go-a2s v1.0.2/go.mod h1:6nq//LMUMa3ElowQ7eH8atnDbQG+nVMFsaMFzSo8p/M=
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad h1:qIQkSlF5vAUHxEmTbaqt1hkJ/t6skqEGYiMag343ucI=
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=