From ab94e9ae24adb82d4c7ffc7081d003a197c1852b Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 00:03:01 +0530 Subject: [PATCH 1/5] Feat : Added dto for DiscordMessage --- dtos/discord.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dtos/discord.go b/dtos/discord.go index eaf49ec..88945d8 100644 --- a/dtos/discord.go +++ b/dtos/discord.go @@ -2,7 +2,17 @@ package dtos import "github.com/bwmarrin/discordgo" +type data struct { + discordgo.ApplicationCommandInteractionData + GuildId string `json:"guild_id"` +} + type DiscordMessage struct { - Type discordgo.InteractionType `json:"type"` - User *discordgo.User `json:"user"` + AppPermissions string `json:"app_permissions"` + ApplicationId string `json:"application_id"` + Type discordgo.InteractionType `json:"type"` + Channel *discordgo.Channel `json:"channel"` + ChannelId string `json:"channel_id"` + Member *discordgo.Member `json:"member"` + Data *data `json:"data"` } From e8ef204fc30db04e30304e5f8fee6524b976c78e Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 01:23:57 +0530 Subject: [PATCH 2/5] Refactor : Simplified baseHandler code --- controllers/baseHandler.go | 12 +++++------- controllers/baseHandler_test.go | 4 ++-- dtos/discord.go | 4 ++-- fixtures/message.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 fixtures/message.go diff --git a/controllers/baseHandler.go b/controllers/baseHandler.go index 649e863..62a73a8 100644 --- a/controllers/baseHandler.go +++ b/controllers/baseHandler.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/Real-Dev-Squad/discord-service/dtos" + service "github.com/Real-Dev-Squad/discord-service/service" "github.com/Real-Dev-Squad/discord-service/utils" "github.com/bwmarrin/discordgo" "github.com/julienschmidt/httprouter" @@ -24,19 +25,16 @@ func HomeHandler(response http.ResponseWriter, request *http.Request, params htt return } switch message.Type { + case discordgo.InteractionPing: payload := map[string]interface{}{"type": uint8(discordgo.InteractionResponsePong)} utils.Success.NewDiscordResponse(response, "Pong", payload) return + case discordgo.InteractionApplicationCommand: - messageResponse := &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Hey there! Congratulations, you just executed your first slash command", - }, - } - utils.Success.NewDiscordResponse(response, "Success", messageResponse) + service.MainService(&message)(response, request) return + default: response.WriteHeader(http.StatusOK) } diff --git a/controllers/baseHandler_test.go b/controllers/baseHandler_test.go index 3eddadc..0062835 100644 --- a/controllers/baseHandler_test.go +++ b/controllers/baseHandler_test.go @@ -9,6 +9,7 @@ import ( "github.com/Real-Dev-Squad/discord-service/controllers" "github.com/Real-Dev-Squad/discord-service/dtos" + "github.com/Real-Dev-Squad/discord-service/fixtures" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" "github.com/bwmarrin/discordgo" "github.com/stretchr/testify/assert" @@ -47,8 +48,7 @@ func TestHomeHandler(t *testing.T) { }) t.Run("Should return 200 when request body is valid for interaction command", func(t *testing.T) { w := httptest.NewRecorder() - message := dtos.DiscordMessage{Type: discordgo.InteractionApplicationCommand} - jsonBytes, _ := json.Marshal(message) + jsonBytes, _ := json.Marshal(fixtures.HelloCommand) r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) controllers.HomeHandler(w, r, nil) diff --git a/dtos/discord.go b/dtos/discord.go index 88945d8..9670362 100644 --- a/dtos/discord.go +++ b/dtos/discord.go @@ -2,7 +2,7 @@ package dtos import "github.com/bwmarrin/discordgo" -type data struct { +type Data struct { discordgo.ApplicationCommandInteractionData GuildId string `json:"guild_id"` } @@ -14,5 +14,5 @@ type DiscordMessage struct { Channel *discordgo.Channel `json:"channel"` ChannelId string `json:"channel_id"` Member *discordgo.Member `json:"member"` - Data *data `json:"data"` + Data *Data `json:"data"` } diff --git a/fixtures/message.go b/fixtures/message.go new file mode 100644 index 0000000..f27af89 --- /dev/null +++ b/fixtures/message.go @@ -0,0 +1,30 @@ +package fixtures + +import ( + "github.com/Real-Dev-Squad/discord-service/dtos" + "github.com/bwmarrin/discordgo" +) + +var HelloCommand = &dtos.DiscordMessage{ + AppPermissions: "0", + ApplicationId: "123456789012345678", + Type: discordgo.InteractionApplicationCommand, + Channel: &discordgo.Channel{ + ID: "987654321098765432", + Name: "general", + }, + ChannelId: "987654321098765432", + Member: &discordgo.Member{ + User: &discordgo.User{ + ID: "123456789012345678", + Username: "ExampleUser", + Discriminator: "1234", + }, + }, + Data: &dtos.Data{ + GuildId: "876543210987654321", + ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{ + Name: "hello", + }, + }, +} From 33cacfdd601b99b3474139d931aabc9461758211 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 02:14:10 +0530 Subject: [PATCH 3/5] Feat : Added service to handle commands --- service/helloService.go | 18 ++++++++++++++ service/helloService_test.go | 29 ++++++++++++++++++++++ service/main.go | 27 +++++++++++++++++++++ service/main_test.go | 47 ++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 service/helloService.go create mode 100644 service/helloService_test.go create mode 100644 service/main.go create mode 100644 service/main_test.go diff --git a/service/helloService.go b/service/helloService.go new file mode 100644 index 0000000..5d13850 --- /dev/null +++ b/service/helloService.go @@ -0,0 +1,18 @@ +package service + +import ( + "net/http" + + "github.com/Real-Dev-Squad/discord-service/utils" + "github.com/bwmarrin/discordgo" +) + +func (s *CommandService) HelloService(response http.ResponseWriter, request *http.Request) { + messageResponse := &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Hey there! Congratulations, you just executed your first slash command", + }, + } + utils.Success.NewDiscordResponse(response, "Success", messageResponse) +} diff --git a/service/helloService_test.go b/service/helloService_test.go new file mode 100644 index 0000000..3e2c267 --- /dev/null +++ b/service/helloService_test.go @@ -0,0 +1,29 @@ +package service + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/Real-Dev-Squad/discord-service/fixtures" + _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" + + "github.com/bwmarrin/discordgo" + "github.com/stretchr/testify/assert" +) + +func TestHelloService(t *testing.T) { + t.Run("should return a success response with a message", func(t *testing.T) { + w := httptest.NewRecorder() + r, _ := http.NewRequest("GET", "/", nil) + CS.discordMessage = fixtures.HelloCommand + CS.HelloService(w, r) + assert.Equal(t, http.StatusOK, w.Code) + var response discordgo.InteractionResponse + err := json.NewDecoder(w.Body).Decode(&response) + assert.NoError(t, err) + assert.Equal(t, discordgo.InteractionResponseChannelMessageWithSource, response.Type) + assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", response.Data.Content) + }) +} diff --git a/service/main.go b/service/main.go new file mode 100644 index 0000000..91e5fd5 --- /dev/null +++ b/service/main.go @@ -0,0 +1,27 @@ +package service + +import ( + "net/http" + + "github.com/Real-Dev-Squad/discord-service/dtos" +) + +type CommandService struct { + discordMessage *dtos.DiscordMessage +} + +var CS = CommandService{} + +func MainService(discordMessage *dtos.DiscordMessage) func(response http.ResponseWriter, request *http.Request) { + CS.discordMessage = discordMessage + switch discordMessage.Data.Name { + + case "hello": + return CS.HelloService + + default: + return func(response http.ResponseWriter, request *http.Request) { + response.WriteHeader(http.StatusOK) + } + } +} diff --git a/service/main_test.go b/service/main_test.go new file mode 100644 index 0000000..012c9fa --- /dev/null +++ b/service/main_test.go @@ -0,0 +1,47 @@ +package service + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Real-Dev-Squad/discord-service/dtos" + _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" + "github.com/bwmarrin/discordgo" + "github.com/stretchr/testify/assert" +) + +func TestMainService(t *testing.T) { + t.Run("should return HelloService when command name is hello", func(t *testing.T) { + discordMessage := &dtos.DiscordMessage{ + Data: &dtos.Data{ + GuildId: "876543210987654321", + ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{ + Name: "hello", + }, + }, + } + handler := MainService(discordMessage) + w := httptest.NewRecorder() + r, _ := http.NewRequest("GET", "/", nil) + handler(w, r) + assert.Equal(t, http.StatusOK, w.Code) + }) + + t.Run("should return default handler when command name is not in record", func(t *testing.T) { + discordMessage := &dtos.DiscordMessage{ + Data: &dtos.Data{ + GuildId: "876543210987654321", + ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{ + Name: "unknown", + }, + }, + } + handler := MainService(discordMessage) + w := httptest.NewRecorder() + r, _ := http.NewRequest("GET", "/", nil) + handler(w, r) + assert.Equal(t, http.StatusOK, w.Code) + }) + +} From 07f8b11b20219ad1e4c8e98a688b57e792769afe Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 02:32:03 +0530 Subject: [PATCH 4/5] Refactor : Simplified existing code --- controllers/baseHandler_test.go | 2 -- service/helloService.go | 2 +- service/helloService_test.go | 10 ++++++++-- service/main.go | 1 - service/main_test.go | 11 ++--------- utils/responseGenerator.go | 11 +++++++++++ 6 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 utils/responseGenerator.go diff --git a/controllers/baseHandler_test.go b/controllers/baseHandler_test.go index 0062835..0f0b7b7 100644 --- a/controllers/baseHandler_test.go +++ b/controllers/baseHandler_test.go @@ -58,8 +58,6 @@ func TestHomeHandler(t *testing.T) { assert.Nil(t, err) assert.Equal(t, float64(discordgo.InteractionResponseChannelMessageWithSource), response["type"]) - data := response["data"].(map[string]interface{}) - assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", data["content"]) }) t.Run("Should return 200 when interaction type is unknown", func(t *testing.T) { diff --git a/service/helloService.go b/service/helloService.go index 5d13850..33a3530 100644 --- a/service/helloService.go +++ b/service/helloService.go @@ -11,7 +11,7 @@ func (s *CommandService) HelloService(response http.ResponseWriter, request *htt messageResponse := &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Hey there! Congratulations, you just executed your first slash command", + Content: utils.ResponseGenerator.HelloResponse(s.discordMessage.Member.User.ID), }, } utils.Success.NewDiscordResponse(response, "Success", messageResponse) diff --git a/service/helloService_test.go b/service/helloService_test.go index 3e2c267..85938fd 100644 --- a/service/helloService_test.go +++ b/service/helloService_test.go @@ -1,6 +1,7 @@ package service import ( + "bytes" "encoding/json" "net/http" "net/http/httptest" @@ -8,22 +9,27 @@ import ( "github.com/Real-Dev-Squad/discord-service/fixtures" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" + "github.com/Real-Dev-Squad/discord-service/utils" "github.com/bwmarrin/discordgo" "github.com/stretchr/testify/assert" ) func TestHelloService(t *testing.T) { + t.Run("should return a success response with a message", func(t *testing.T) { w := httptest.NewRecorder() - r, _ := http.NewRequest("GET", "/", nil) + jsonBytes, _ := json.Marshal(fixtures.HelloCommand) + r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) + CS.discordMessage = fixtures.HelloCommand CS.HelloService(w, r) + assert.Equal(t, http.StatusOK, w.Code) var response discordgo.InteractionResponse err := json.NewDecoder(w.Body).Decode(&response) assert.NoError(t, err) assert.Equal(t, discordgo.InteractionResponseChannelMessageWithSource, response.Type) - assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", response.Data.Content) + assert.Equal(t, utils.ResponseGenerator.HelloResponse(fixtures.HelloCommand.Member.User.ID), response.Data.Content) }) } diff --git a/service/main.go b/service/main.go index 91e5fd5..93f6bc8 100644 --- a/service/main.go +++ b/service/main.go @@ -15,7 +15,6 @@ var CS = CommandService{} func MainService(discordMessage *dtos.DiscordMessage) func(response http.ResponseWriter, request *http.Request) { CS.discordMessage = discordMessage switch discordMessage.Data.Name { - case "hello": return CS.HelloService diff --git a/service/main_test.go b/service/main_test.go index 012c9fa..b9553cf 100644 --- a/service/main_test.go +++ b/service/main_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/Real-Dev-Squad/discord-service/dtos" + "github.com/Real-Dev-Squad/discord-service/fixtures" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" "github.com/bwmarrin/discordgo" "github.com/stretchr/testify/assert" @@ -13,15 +14,7 @@ import ( func TestMainService(t *testing.T) { t.Run("should return HelloService when command name is hello", func(t *testing.T) { - discordMessage := &dtos.DiscordMessage{ - Data: &dtos.Data{ - GuildId: "876543210987654321", - ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{ - Name: "hello", - }, - }, - } - handler := MainService(discordMessage) + handler := MainService(fixtures.HelloCommand) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) handler(w, r) diff --git a/utils/responseGenerator.go b/utils/responseGenerator.go new file mode 100644 index 0000000..c1c88ea --- /dev/null +++ b/utils/responseGenerator.go @@ -0,0 +1,11 @@ +package utils + +import "fmt" + +type responseGenerator struct{} + +func (r *responseGenerator) HelloResponse(userId string) string { + return fmt.Sprintf("Hey there <@%s>! Congratulations, you just executed your first slash command", userId) +} + +var ResponseGenerator = &responseGenerator{} From ef7623b1e667ef5ec6f9585973e18d3a6b5de820 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 02:43:08 +0530 Subject: [PATCH 5/5] Chore : Added spaces --- service/main_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service/main_test.go b/service/main_test.go index b9553cf..0bf3925 100644 --- a/service/main_test.go +++ b/service/main_test.go @@ -18,6 +18,7 @@ func TestMainService(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) handler(w, r) + assert.Equal(t, http.StatusOK, w.Code) }) @@ -30,10 +31,12 @@ func TestMainService(t *testing.T) { }, }, } + handler := MainService(discordMessage) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) handler(w, r) + assert.Equal(t, http.StatusOK, w.Code) })