Skip to content

Commit

Permalink
modified webhook code to take req object
Browse files Browse the repository at this point in the history
  • Loading branch information
kanielv committed Nov 17, 2024
1 parent 6900122 commit b4bc6a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
24 changes: 14 additions & 10 deletions server/internal/punishments/discord/webhook/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ import (
)

func RegisterRoutes(router *mux.Router) {
router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodGet)
router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodPost)

}

func handleDiscordWebHook(w http.ResponseWriter, r *http.Request) {
//Create Webhook (This a JSON req object)
var username = "Wake Up"
var content = "Simple Message"
// var username = "Wake Up"
// var content = "Simple Message"

var title = "Testing Embed"
var description = "Embed Description."
var color = "383483" // Must be a numerical value not nex
var url2 = "https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExbXluOXlraTJzNzU1eHhqdnNta2xld3l2anA0bG0zemM1NTBiajIxYyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/qpCvOBBmBkble/giphy.webp"
// var title = "Testing Embed"
// var description = "Embed Description."
// var color = "383483" // Must be a numerical value not nex
// var url2 = "https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExbXluOXlraTJzNzU1eHhqdnNta2xld3l2anA0bG0zemM1NTBiajIxYyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/qpCvOBBmBkble/giphy.webp"

webhookExample := NewDiscordWebHook(username, content, title, description, color, url2)
d := DiscordWebHook{}
if err := utils.ParseJSON(r, &d); err != nil {
log.Fatal(err)
}

webhookExample := NewDiscordWebHook(d.Username, d.Content, d.Title, d.Description, d.Color, d.ImgURL)

fmt.Println("Webhook activated")
err := godotenv.Load("../.env")

if err != nil {
if err := godotenv.Load("../.env"); err != nil {
log.Fatal(err)
}

Expand Down
36 changes: 18 additions & 18 deletions server/internal/punishments/discord/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type DiscordWebHook struct {
username string
content string
Username string `json:"username"`
Content string `json:"content"`

title string
description string
color string
imgURL string
Title string `json:"title"`
Description string `json:"description"`
Color string `json:"color"`
ImgURL string `json:"url"`
}

func NewDiscordWebHook(
Expand All @@ -26,31 +26,31 @@ func NewDiscordWebHook(
) *DiscordWebHook {

return &DiscordWebHook{
username: username,
content: content,
title: title,
description: description,
color: color,
imgURL: imgURL,
Username: username,
Content: content,
Title: title,
Description: description,
Color: color,
ImgURL: imgURL,
}

}

func (d *DiscordWebHook) SendMessage(webhookURL string) error {
image := discordwebhook.Image{
Url: &d.imgURL,
Url: &d.ImgURL,
}

embeds := discordwebhook.Embed{
Title: &d.title,
Description: &d.description,
Color: &d.color,
Title: &d.Title,
Description: &d.Description,
Color: &d.Color,
Image: &image,
}

message := discordwebhook.Message{
Username: &d.username,
Content: &d.content,
Username: &d.Username,
Content: &d.Content,
Embeds: &[]discordwebhook.Embed{embeds},
}

Expand Down

0 comments on commit b4bc6a3

Please sign in to comment.