From fb556d8ace692ee153f4c39acdc328a9f6f46880 Mon Sep 17 00:00:00 2001 From: darius Date: Sat, 4 May 2024 18:36:21 +0200 Subject: [PATCH] http command --- commands/commands.go | 24 ++++++++++ handlers/httpCommand.go | 96 ++++++++++++++++++++++++++++++++++++++++ routers/commandRouter.go | 2 + 3 files changed, 122 insertions(+) create mode 100644 handlers/httpCommand.go diff --git a/commands/commands.go b/commands/commands.go index e50ac7b..b6c6dbd 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -41,6 +41,30 @@ var Commands = []*discordgo.ApplicationCommand{ }, }, }, + { + Name: "http", + Description: "send http request", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "type", + Description: "request type", + Required: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "url", + Description: "request url", + Required: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "body", + Description: "key1:val1,key2:val2...", + Required: false, + }, + }, + }, { Name: "stop", Description: "stop bot", diff --git a/handlers/httpCommand.go b/handlers/httpCommand.go new file mode 100644 index 0000000..aa4893d --- /dev/null +++ b/handlers/httpCommand.go @@ -0,0 +1,96 @@ +package handlers + +import ( + "bytes" + "encoding/json" + "github.com/bwmarrin/discordgo" + "kleincordBot/services" + "log" + "net/http" + "net/http/httputil" + "strings" +) + +func HttpCommand(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session, i *discordgo.InteractionCreate) { + + var requestType string + var requestUrl string + var requestBody string + var err error + var response *http.Response + var responsColor int + + for i, option := range data.Options { + if len(option.Options) > i+1 { + break + } + switch option.Name { + case "type": + requestType = option.Value.(string) + case "url": + requestUrl = option.Value.(string) + case "body": + requestBody = option.Value.(string) + } + } + body := strings.Split(requestBody, ",") + bodyMap := make(map[string]any) + for _, entry := range body { + KeyValPair := strings.Split(entry, ":") + if len(KeyValPair) < 2 { + break + } + bodyMap[KeyValPair[0]] = KeyValPair[1] + } + postBody, _ := json.Marshal(bodyMap) + responseBody := bytes.NewBuffer(postBody) + + switch requestType { + case "post": + response, err = http.Post(requestUrl, "application/json", responseBody) + if err != nil { + log.Fatalf("An Error Occured %v", err) + } + case "get": + response, err = http.Get(requestUrl) + if err != nil { + log.Fatalf("An Error Occured %v", err) + } + } + byts, _ := httputil.DumpResponse(response, true) + + switch statusCode := response.StatusCode; { + case statusCode >= 200 && statusCode < 300: + responsColor = 0x008000 + case statusCode >= 300 && statusCode < 400: + responsColor = 0xffff00 + case statusCode >= 400 && statusCode < 500: + responsColor = 0xffa500 + case statusCode >= 500 && statusCode < 600: + responsColor = 0xff0000 + } + + err = s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Title: response.Status, + Color: responsColor, + Fields: []*discordgo.MessageEmbedField{ + { + Value: string(byts), + }, + }, + Type: discordgo.EmbedTypeArticle, + }, + }, + }, + }, + ) + if err != nil { + services.HandleError(err, s) + } +} diff --git a/routers/commandRouter.go b/routers/commandRouter.go index 224e3d4..e011ebe 100644 --- a/routers/commandRouter.go +++ b/routers/commandRouter.go @@ -20,5 +20,7 @@ func CommandRouter(s *discordgo.Session, i *discordgo.InteractionCreate) { handlers.DeleteCommand(data, s, i) case "stop": handlers.StopCommand(s, i) + case "http": + handlers.HttpCommand(data, s, i) } }