From c52070ddd910c514fb46f389fb1e9dad75a200bf Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Tue, 29 Oct 2024 11:15:55 +0100 Subject: [PATCH] add some changes --- error/notifydiscord.go | 61 ++++++++++++++++++++++++++++++++++++++++++ models/data.go | 2 ++ models/discord.go | 11 ++++++++ 3 files changed, 74 insertions(+) create mode 100644 error/notifydiscord.go create mode 100644 models/discord.go diff --git a/error/notifydiscord.go b/error/notifydiscord.go new file mode 100644 index 0000000..f5ca723 --- /dev/null +++ b/error/notifydiscord.go @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Laura Kalb + * The code of this project is available under the MIT license. See the LICENSE file for more info. + * + */ + +package error + +import ( + "codeberg.org/lauralani/humble-bot/misc" + "codeberg.org/lauralani/humble-bot/models" + "encoding/json" + "fmt" + "github.com/rs/zerolog/log" + "github.com/spf13/viper" + "io/ioutil" + "net/http" + "net/url" +) + +func NotifyDiscord(message models.DiscordMessage) { + _, urlerr := url.ParseRequestURI(viper.GetString("errorhandling.discordhook")) + + if urlerr != nil || message.Content == "" { + log.Error().Str("integration", "discord").Str("config", "errorhandling.discordhook").Msgf("Tried to send discord error message, but" + + "message was empty or discord hook url in config is invalid. Message: %q", message.Content) + return + } + + url := "https://discord.com/api/webhooks/1146044096029405226/GJ-KfB7NIgAhuf8soP_DvxbOui5E8yLrR4hzAZDgL6BLTxlqymsGTA0Q1weM90fMjLUa" + method := "POST" + payload, err := json.Marshal(message) + if err != nil { + log.Error().Str("func", "NotifyDiscord").Msgf("Can't marshal disocrd message: %q", err) + } + + client := misc.CustomHttpClient() + req, err := http.NewRequest(method, url, payload) + + if err != nil { + fmt.Println(err) + return + } + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Cookie", "__cfruid=52458531a20bc4c09fc012c806f897199e3e5bed-1693308636; __dcfduid=797cba3c465f11ee9d809a4b39b1053e; __sdcfduid=797cba3c465f11ee9d809a4b39b1053e004a6a0e48439b5049639654379f965153994920b12b1713669e63cabceb2f7a; _cfuvid=OdkJt_LIxefbXs_wHs.2k94vJD0kWpNzWTxAGVu2tjQ-1693308636963-0-604800000") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(string(body)) + } +} diff --git a/models/data.go b/models/data.go index b6fdd53..bda7d0b 100644 --- a/models/data.go +++ b/models/data.go @@ -322,6 +322,8 @@ type Games struct { CsrfFormKey string `json:"csrfFormKey"` } +const test := Software.UserOptions.IsLoggedIn + type Software struct { UserOptions struct { IsLoggedIn bool `json:"is_logged_in"` diff --git a/models/discord.go b/models/discord.go new file mode 100644 index 0000000..ea98996 --- /dev/null +++ b/models/discord.go @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2023 Laura Kalb + * The code of this project is available under the MIT license. See the LICENSE file for more info. + * + */ + +package models + +type DiscordMessage struct { + Content string `json:"content"` +}