Add Command Functionality to Discord Package & Implement Feedback Webhooks (!3)
Co-authored-by: Layla <layla@layla.gg> Reviewed-on: https://gitea.sumulayla.synology.me/layla/birdbot/pulls/3
This commit is contained in:
125
modules/feedback_webhook.go
Normal file
125
modules/feedback_webhook.go
Normal file
@ -0,0 +1,125 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot/discord"
|
||||
)
|
||||
|
||||
type feedbackWebhookModule struct {
|
||||
session *discord.Discord
|
||||
webhookURL string
|
||||
payloadType string
|
||||
successMessage string
|
||||
failureMessage string
|
||||
}
|
||||
|
||||
type FeedbackWebhookConfiguration struct {
|
||||
SuccessMessage string
|
||||
FailureMessage string
|
||||
PayloadType string
|
||||
}
|
||||
|
||||
// NewFeedbackWebhookComponent creates a new component
|
||||
func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfiguration, session *discord.Discord) common.Module {
|
||||
m := &feedbackWebhookModule{
|
||||
session: session,
|
||||
webhookURL: webhookURL,
|
||||
payloadType: "default",
|
||||
successMessage: "Feedback recieved!",
|
||||
failureMessage: "Failed to send feedback!",
|
||||
}
|
||||
|
||||
if config.SuccessMessage != "" {
|
||||
m.successMessage = config.SuccessMessage
|
||||
}
|
||||
if config.FailureMessage != "" {
|
||||
m.failureMessage = config.FailureMessage
|
||||
}
|
||||
if config.PayloadType != "" {
|
||||
m.payloadType = config.PayloadType
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (c *feedbackWebhookModule) Initialize(birdbot common.ModuleManager) error {
|
||||
c.session.RegisterCommand("feedback", discord.CommandConfiguration{
|
||||
Description: "Sends a feedback message",
|
||||
EphemeralResponse: true,
|
||||
Options: map[string]discord.CommandOption{
|
||||
"message": {
|
||||
Description: "Content of what you'd like to communicate in your feedback.",
|
||||
Type: discord.CommandTypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}, func(user common.User, args map[string]any) string {
|
||||
|
||||
message, ok := args["message"]
|
||||
if !ok {
|
||||
return "Missing content in command"
|
||||
}
|
||||
|
||||
var data []byte
|
||||
|
||||
// Supported payload types
|
||||
switch c.payloadType {
|
||||
case "discord":
|
||||
data, _ = json.Marshal(map[string]any{
|
||||
"content": fmt.Sprintf("%s: %s", user.DisplayName, message),
|
||||
})
|
||||
case "slack":
|
||||
data, _ = json.Marshal(map[string]any{
|
||||
"text": fmt.Sprintf("%s: %s", user.DisplayName, message),
|
||||
})
|
||||
default:
|
||||
data, _ = json.Marshal(map[string]any{
|
||||
"message": message,
|
||||
"username": user.DisplayName,
|
||||
})
|
||||
}
|
||||
|
||||
body := bytes.NewBuffer(data)
|
||||
|
||||
// Send HTTP request
|
||||
resp, err := http.Post(c.webhookURL, "application/json", body)
|
||||
if err != nil {
|
||||
log.Printf("Failed to post feedback to url '%s': %s", c.webhookURL, err)
|
||||
return c.failureMessage
|
||||
}
|
||||
|
||||
// Validate response
|
||||
if resp.Status[0] != '2' {
|
||||
log.Printf("Webhook returned %v: %s", resp.Status, message)
|
||||
return c.failureMessage
|
||||
}
|
||||
|
||||
// Read body for any special response
|
||||
response := map[any]any{}
|
||||
responseBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return c.successMessage
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(responseBody, &response); err != nil {
|
||||
return c.successMessage
|
||||
}
|
||||
|
||||
if message, ok := response["message"]; ok {
|
||||
v := fmt.Sprint(message)
|
||||
if len(v) > 0 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return c.successMessage
|
||||
})
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user