add pastebin upload functionality and implement it for uer and command debugging

This commit is contained in:
lyx0 2023-09-16 17:18:14 +02:00
parent 10bd7c0342
commit 90031bfe40
3 changed files with 89 additions and 3 deletions

View file

@ -177,7 +177,13 @@ func (app *application) DebugCommand(name string, message twitch.PrivateMessage)
cmd.Help, cmd.Help,
) )
//app.Send(message.Channel, reply) resp, err := app.uploadPaste(reply)
if err != nil {
app.Log.Errorln("Could not upload paste:", err)
app.Send(message.Channel, "Something went wrong FeelsBadMan")
return
}
app.Send(message.Channel, resp)
app.SendEmail(fmt.Sprintf("DEBUG for command %s", name), reply) app.SendEmail(fmt.Sprintf("DEBUG for command %s", name), reply)
return return
} else { } else {
@ -191,6 +197,13 @@ func (app *application) DebugCommand(name string, message twitch.PrivateMessage)
) )
//app.Send(message.Channel, reply) //app.Send(message.Channel, reply)
resp, err := app.uploadPaste(reply)
if err != nil {
app.Log.Errorln("Could not upload paste:", err)
app.Send(message.Channel, "Something went wrong FeelsBadMan")
return
}
app.Send(message.Channel, resp)
app.SendEmail(fmt.Sprintf("DEBUG for command %s", name), reply) app.SendEmail(fmt.Sprintf("DEBUG for command %s", name), reply)
return return
} }

60
cmd/nourybot/paste.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// uploadPaste uploads a given text to a pastebin site and returns the link
//
// this whole function was pretty much yoinked from here
// https://github.com/zneix/haste-client/blob/master/main.go <3
func (app *application) uploadPaste(text string) (string, error) {
var hasteURL = "https://haste.noury.ee"
var apiRoute = "/documents"
var httpClient = &http.Client{Timeout: 10 * time.Second}
type pasteResponse struct {
Key string `json:"key,omitempty"`
}
req, err := http.NewRequest("POST", hasteURL+apiRoute, bytes.NewBufferString(text))
if err != nil {
app.Log.Error("Could not upload paste:", err)
return "", err
}
req.Header.Set("User-Agent", fmt.Sprintf("nourybot"))
resp, err := httpClient.Do(req)
if err != nil {
app.Log.Error("Error while sending HTTP request:", err)
return "", err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusMultipleChoices {
app.Log.Errorln("Failed to upload data, server responded with", resp.StatusCode)
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
app.Log.Errorln("Error while reading response:", err)
return "", err
}
jsonResponse := new(pasteResponse)
if err := json.Unmarshal(body, jsonResponse); err != nil {
app.Log.Fatalln("Error while unmarshalling JSON response:", err)
return "", err
}
finalURL := hasteURL + "/" + jsonResponse.Key
return finalURL, nil
}

View file

@ -30,9 +30,22 @@ func (app *application) DebugUser(login string, message twitch.PrivateMessage) {
app.Send(message.Channel, reply) app.Send(message.Channel, reply)
return return
} else { } else {
subject := fmt.Sprintf("DEBUG for %v", login) subject := fmt.Sprintf("DEBUG for user %v", login)
body := fmt.Sprintf("DEBUG information for user %v\n\nid=%v \nlogin=%v \nlevel=%v \nlocation=%v \nlastfm=%v", login, user.TwitchID, user.Login, user.Level, user.Location, user.LastFMUsername) body := fmt.Sprintf("id=%v \nlogin=%v \nlevel=%v \nlocation=%v \nlastfm=%v",
user.TwitchID,
user.Login,
user.Level,
user.Location,
user.LastFMUsername,
)
resp, err := app.uploadPaste(body)
if err != nil {
app.Log.Errorln("Could not upload paste:", err)
app.Send(message.Channel, "Something went wrong FeelsBadMan")
return
}
app.Send(message.Channel, resp)
app.SendEmail(subject, body) app.SendEmail(subject, body)
return return
} }