From 69001223076871975e593ef370974e3f2ec65394 Mon Sep 17 00:00:00 2001 From: Kaniel Vicencio <34636449+kanielv@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:46:18 -0500 Subject: [PATCH] refactored webhook; able to create different wewebhook instances; added testing (WIP) (#91) --- server/discord/discord.go | 40 --------- server/discord/discord.md | 46 ---------- server/discord/go.mod | 14 --- server/discord/go.sum | 18 ---- server/discord/main.go | 25 ------ server/discord/webhook.go | 39 --------- server/go.mod | 32 +------ server/go.sum | 85 ++----------------- .../punishments/discord/webhook/routes.go | 18 ++-- .../punishments/discord/webhook/webhook.go | 54 ++++++++++++ .../discord/webhook/webhook_test.go | 36 ++++++++ server/server.go | 23 ----- server/server.ts | 0 13 files changed, 113 insertions(+), 317 deletions(-) delete mode 100644 server/discord/discord.go delete mode 100644 server/discord/discord.md delete mode 100644 server/discord/go.mod delete mode 100644 server/discord/go.sum delete mode 100644 server/discord/main.go delete mode 100644 server/discord/webhook.go create mode 100644 server/internal/punishments/discord/webhook/webhook_test.go delete mode 100644 server/server.go delete mode 100644 server/server.ts diff --git a/server/discord/discord.go b/server/discord/discord.go deleted file mode 100644 index 21bef98..0000000 --- a/server/discord/discord.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - "os" - - "github.com/bwmarrin/discordgo" -) - -var ( - Token string -) - -// Init flags -func init() { - flag.StringVar(&Token, "t", "", "Bot Token") - flag.Parse() - - if Token == "" { - flag.Usage() - os.Exit(1) - } -} - -func start_bot() { - session, _ := discordgo.New("Bot " + Token) - - session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { - fmt.Println("Bot online") - }) - - err := session.Open() - if err != nil { - log.Fatalf("Cannot open the session: %v", err) - } - defer session.Close() - -} diff --git a/server/discord/discord.md b/server/discord/discord.md deleted file mode 100644 index 8bdcdf4..0000000 --- a/server/discord/discord.md +++ /dev/null @@ -1,46 +0,0 @@ -# Discord Webhook Documentation - -## Prerequisites - -To run this project, you'll need to install the required Go packages: - -1. Install the discordwebhook package: - -``` -go get github.com/gtuk/discordwebhook -``` - -Install the godotenv package: - -``` -go get github.com/joho/godotenv -``` - - -### Create .env file - -1. In the root directory of the project (where main.go is located), create a .env file. -2. Inside this .env file, define your Discord webhook URL like so: - -``` -DISCORD_WEBHOOK= -``` - -**DO NOT COMMIT THE .env FILE** - -## Running the Webhook - -Running the go files inside this directory requires you to run all go files associated with file that is being run. - -For example: - -Running main.go - -``` -go run main.go webhook.go -``` - -## Additional links -- [Creating Webhook](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) -- [Webhook Structure](https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html) -- [Webhook github package](https://github.com/gtuk/discordwebhook?tab=readme-ov-file) \ No newline at end of file diff --git a/server/discord/go.mod b/server/discord/go.mod deleted file mode 100644 index e43b70d..0000000 --- a/server/discord/go.mod +++ /dev/null @@ -1,14 +0,0 @@ -module ufosc.com/Alarm-Clock/discord - -go 1.23.1 - -require github.com/bwmarrin/discordgo v0.28.1 - -require ( - github.com/bensch777/discord-webhook-golang v0.0.6 // indirect - github.com/gorilla/websocket v1.4.2 // indirect - github.com/gtuk/discordwebhook v1.2.0 // indirect - github.com/joho/godotenv v1.5.1 // indirect - golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect - golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect -) diff --git a/server/discord/go.sum b/server/discord/go.sum deleted file mode 100644 index f9467b2..0000000 --- a/server/discord/go.sum +++ /dev/null @@ -1,18 +0,0 @@ -github.com/bensch777/discord-webhook-golang v0.0.6 h1:91BMU6vKgymAMfRwtXPMUrKX+SUoPPHTDJHTFA/1Kgk= -github.com/bensch777/discord-webhook-golang v0.0.6/go.mod h1:GcIorMZAZaHZyQJkjNoYKvZ6VpZo8XLib/eD51xN7Is= -github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= -github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gtuk/discordwebhook v1.2.0 h1:7+gWPKSGyXjopu/6X9+oGbn0knTkDVXUM909+IXGZ/U= -github.com/gtuk/discordwebhook v1.2.0/go.mod h1:U3LdXNJ1e0bx3MMe2a4mB1VBantPHOPly2jNd8ZWXec= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= -golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/server/discord/main.go b/server/discord/main.go deleted file mode 100644 index ea24300..0000000 --- a/server/discord/main.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - - "github.com/joho/godotenv" -) - -func main() { - - err := godotenv.Load("./../../.env") - if err != nil { - log.Fatalf("Error loading .env file") - } - - webhook_url := os.Getenv("DISCORD_WEBHOOK") - if webhook_url == "" { - log.Fatalf("DISCORD_WEBHOOK_URL is not set in the .env file") - } - - fmt.Println("Sending Webhook") - send_example_message(webhook_url) -} diff --git a/server/discord/webhook.go b/server/discord/webhook.go deleted file mode 100644 index 0e6b321..0000000 --- a/server/discord/webhook.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "log" - - "github.com/gtuk/discordwebhook" -) - -func send_example_message(discord_webhook string) { - 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" - - image := discordwebhook.Image{ - Url: &url2, - } - - embeds := discordwebhook.Embed{ - Title: &title, - Description: &description, - Color: &color, - Image: &image, - } - - message := discordwebhook.Message{ - Username: &username, - Content: &content, - Embeds: &[]discordwebhook.Embed{embeds}, - } - - err := discordwebhook.SendMessage(discord_webhook, message) - if err != nil { - log.Fatal(err) - } -} diff --git a/server/go.mod b/server/go.mod index 2464392..34c1647 100644 --- a/server/go.mod +++ b/server/go.mod @@ -1,33 +1,9 @@ -module server +module ufosc/alarm-clock go 1.23.1 require ( - github.com/bytedance/sonic v1.11.6 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect - github.com/cloudwego/base64x v0.1.4 // indirect - github.com/cloudwego/iasm v0.2.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.3 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.10.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.20.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.7 // indirect - github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.8.0 // indirect - golang.org/x/crypto v0.23.0 // indirect - golang.org/x/net v0.25.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + github.com/gorilla/mux v1.8.1 + github.com/gtuk/discordwebhook v1.2.0 + github.com/joho/godotenv v1.5.1 ) diff --git a/server/go.sum b/server/go.sum index b657a9f..76852c3 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,79 +1,6 @@ -github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= -github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= -github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= -github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= -github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= -github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= -github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= -github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= -github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= -github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= -github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= -github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= -github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= -github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= -github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= -golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= -golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= -google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gtuk/discordwebhook v1.2.0 h1:7+gWPKSGyXjopu/6X9+oGbn0knTkDVXUM909+IXGZ/U= +github.com/gtuk/discordwebhook v1.2.0/go.mod h1:U3LdXNJ1e0bx3MMe2a4mB1VBantPHOPly2jNd8ZWXec= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/server/internal/punishments/discord/webhook/routes.go b/server/internal/punishments/discord/webhook/routes.go index 055a655..e842952 100644 --- a/server/internal/punishments/discord/webhook/routes.go +++ b/server/internal/punishments/discord/webhook/routes.go @@ -13,13 +13,21 @@ import ( ) func RegisterRoutes(router *mux.Router) { - // router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodGet) router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodGet) } func handleDiscordWebHook(w http.ResponseWriter, r *http.Request) { - // TODO: Create Webhook + //Create Webhook (This a JSON req object) + 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" + + webhookExample := NewDiscordWebHook(username, content, title, description, color, url2) fmt.Println("Webhook activated") err := godotenv.Load("../.env") @@ -28,9 +36,9 @@ func handleDiscordWebHook(w http.ResponseWriter, r *http.Request) { log.Fatal(err) } - token := os.Getenv("DISCORD_WEBHOOK") + token := os.Getenv("DISCORD_WEBHOOK") - SendMessageExample(token) + webhookExample.SendMessage(token) utils.WriteJSON(w, http.StatusOK, "Discord Webhook sent") -} \ No newline at end of file +} diff --git a/server/internal/punishments/discord/webhook/webhook.go b/server/internal/punishments/discord/webhook/webhook.go index 60df4ef..0e918d9 100644 --- a/server/internal/punishments/discord/webhook/webhook.go +++ b/server/internal/punishments/discord/webhook/webhook.go @@ -6,6 +6,60 @@ import ( "github.com/gtuk/discordwebhook" ) +type DiscordWebHook struct { + username string + content string + + title string + description string + color string + imgURL string +} + +func NewDiscordWebHook( + username string, + content string, + title string, + description string, + color string, + imgURL string, +) *DiscordWebHook { + + return &DiscordWebHook{ + 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, + } + + embeds := discordwebhook.Embed{ + Title: &d.title, + Description: &d.description, + Color: &d.color, + Image: &image, + } + + message := discordwebhook.Message{ + Username: &d.username, + Content: &d.content, + Embeds: &[]discordwebhook.Embed{embeds}, + } + + err := discordwebhook.SendMessage(webhookURL, message) + + return err + +} + func SendMessageExample(discord_webhook string) { var username = "Wake Up" var content = "Simple Message" diff --git a/server/internal/punishments/discord/webhook/webhook_test.go b/server/internal/punishments/discord/webhook/webhook_test.go new file mode 100644 index 0000000..252f960 --- /dev/null +++ b/server/internal/punishments/discord/webhook/webhook_test.go @@ -0,0 +1,36 @@ +package webhook + +import ( + "fmt" + "log" + "os" + "testing" +) + +func TestSendMessage(t *testing.T) { + 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" + + webhookTest := NewDiscordWebHook(username, content, title, description, color, url2) + + cwd, err := os.Getwd() + fmt.Println(cwd) + + // err := godotenv.Load(".env") + + if err != nil { + log.Fatal(err) + } + + webhookURL := os.Getenv("DISCORD_WEBHOOK") + + if err := webhookTest.SendMessage(webhookURL); err != nil { + t.Logf("Discord Webhook did not executre properly") + } + +} diff --git a/server/server.go b/server/server.go deleted file mode 100644 index 031caf5..0000000 --- a/server/server.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -func main() { - router := gin.Default() - - // GET route for root URL - router.GET("/", get_root) - - // Start the HTTP server on port 8080 - router.Run("localhost:8080") -} - -func get_root(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "HTTP is Online!", - }) -} diff --git a/server/server.ts b/server/server.ts deleted file mode 100644 index e69de29..0000000