From b6b2493b5c7c65584f530e5258f9b2906d84847a Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 17:28:58 +0100 Subject: [PATCH 1/6] added ping command --- command_ping.go | 38 ++++++++++++++++++++++++++++++++++++++ go.mod | 1 - go.sum | 2 -- main.go | 1 - register_commands.go | 2 +- 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 command_ping.go diff --git a/command_ping.go b/command_ping.go new file mode 100644 index 0000000..7ef8cab --- /dev/null +++ b/command_ping.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "net/http" + "time" + + "github.com/bwmarrin/discordgo" +) + +var ping_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "ping", + Description: "Returns the ping of the bot", + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + start := time.Now() + + client := http.Client{ + Timeout: 5 * time.Second, + } + + resp, err := client.Get("https://discord.com/api/v9/gateway/bot") + if err != nil { + fmt.Println("Error:", err) + return + } + defer resp.Body.Close() + + ping := time.Since(start) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: fmt.Sprintf("Ping: %.2fms", ping.Seconds()*1000), + }, + }) + }, +} diff --git a/go.mod b/go.mod index 1a43f85..8be6a50 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.21.6 require ( github.com/bwmarrin/discordgo v0.27.1 github.com/google/uuid v1.6.0 - github.com/iancoleman/strcase v0.3.0 github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 ) diff --git a/go.sum b/go.sum index d15266f..cf13ea7 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= -github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= diff --git a/main.go b/main.go index c7f3faa..9d8ecba 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,6 @@ func main() { defer removeCommandFromAllGuilds(discord) discord.AddHandler(ready) discord.AddHandler(interactionCreate) - err = discord.Open() if err != nil { fmt.Println("error opening connection,", err) diff --git a/register_commands.go b/register_commands.go index 74e98bf..4f5f189 100644 --- a/register_commands.go +++ b/register_commands.go @@ -15,7 +15,7 @@ type Command struct { ModalID string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command} +var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command} func ready(s *discordgo.Session, event *discordgo.Ready) { for _, guild := range event.Guilds { From 8c801a3e0a2e597c49662170d32403b03356fa86 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 18:20:32 +0100 Subject: [PATCH 2/6] renamed files because folders are stupid in golang --- command_dadjoke.go => cmd_dadjoke.go | 0 command_notify.go => cmd_notify.go | 0 command_ping.go => cmd_ping.go | 0 command_tag.go => cmd_tag.go | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename command_dadjoke.go => cmd_dadjoke.go (100%) rename command_notify.go => cmd_notify.go (100%) rename command_ping.go => cmd_ping.go (100%) rename command_tag.go => cmd_tag.go (100%) diff --git a/command_dadjoke.go b/cmd_dadjoke.go similarity index 100% rename from command_dadjoke.go rename to cmd_dadjoke.go diff --git a/command_notify.go b/cmd_notify.go similarity index 100% rename from command_notify.go rename to cmd_notify.go diff --git a/command_ping.go b/cmd_ping.go similarity index 100% rename from command_ping.go rename to cmd_ping.go diff --git a/command_tag.go b/cmd_tag.go similarity index 100% rename from command_tag.go rename to cmd_tag.go From f93dbeeab98aae271aefe9440920564a798d99c0 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 18:22:43 +0100 Subject: [PATCH 3/6] removed unused file --- cmd_notify.go | 63 --------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 cmd_notify.go diff --git a/cmd_notify.go b/cmd_notify.go deleted file mode 100644 index e4e0379..0000000 --- a/cmd_notify.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/bwmarrin/discordgo" -) - -// disabled -var notify_command Command = Command{ - Definition: discordgo.ApplicationCommand{ - Name: "notify", - Description: "Manage social media notifications.", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionSubCommand, - Name: "add", - Description: "Set channels where your social media notifications should appear.", - Options: []*discordgo.ApplicationCommandOption{ - { - Name: "platform", - Type: discordgo.ApplicationCommandOptionString, - Description: "The social media platform to receive notifications from.", - Required: true, - Choices: []*discordgo.ApplicationCommandOptionChoice{ - { - Name: "Twitch", - Value: "twitch", - }, - }, - }, - { - Name: "username", - Type: discordgo.ApplicationCommandOptionString, - Required: true, - Description: "The social media platform to receive notifications from.", - }, - { - Name: "channel", - Type: discordgo.ApplicationCommandOptionChannel, - Required: true, - Description: "The social media platform to receive notifications from.", - }, - }, - }, - { - Type: discordgo.ApplicationCommandOptionSubCommand, - Name: "remove", - Description: "Remove a social media notification.", - }, - }, - }, - Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - options := i.ApplicationCommandData().Options[0] - switch options.Name { - case "add": - switch options.Options[0].Value { - case "twitch": - fmt.Print("twitch") - } - } - }, -} From 62dd8e60f9235139e2be22637bc387a9852b7fe3 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 18:55:37 +0100 Subject: [PATCH 4/6] improved ping command --- cmd_ping.go | 17 ++++++++++++++++- config.go | 8 ++++++++ main.go | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 config.go diff --git a/cmd_ping.go b/cmd_ping.go index 7ef8cab..015f565 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -28,10 +28,25 @@ var ping_command Command = Command{ defer resp.Body.Close() ping := time.Since(start) + var ping_color string + if ping.Milliseconds() < 200 { + ping_color = "green" + } else if ping.Milliseconds() < 400 { + ping_color = "yellow" + } else { + ping_color = "red" + } s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: fmt.Sprintf("Ping: %.2fms", ping.Seconds()*1000), + Embeds: []*discordgo.MessageEmbed{ + { + Title: "Bot ping", + Description: fmt.Sprintf("**%.2fms**", ping.Seconds()*1000), + Type: discordgo.EmbedTypeArticle, + Color: hexToDecimal(color[ping_color]), + }, + }, }, }) }, diff --git a/config.go b/config.go new file mode 100644 index 0000000..b9f179a --- /dev/null +++ b/config.go @@ -0,0 +1,8 @@ +package main + +var color map[string]string = map[string]string{ + "red": "#FF6B6B", + "yellow": "#FFD93D", + "green": "#6BCB77", + "blue": "#4D96FF", +} diff --git a/main.go b/main.go index 9d8ecba..48d36eb 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "log" "os" "os/signal" + "strconv" + "strings" "syscall" "database/sql" @@ -57,3 +59,13 @@ func main() { func int64Ptr(i int64) *int64 { return &i } + +func hexToDecimal(hexColor string) int { + // Remove the hash symbol if it's present + hexColor = strings.TrimPrefix(hexColor, "#") + decimal, err := strconv.ParseInt(hexColor, 16, 64) + if err != nil { + return 0 + } + return int(decimal) +} From 8e0d8480aeaf04225fcee44d0a5db64964375a9f Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 21:25:09 +0100 Subject: [PATCH 5/6] improved ping embed --- cmd_ping.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd_ping.go b/cmd_ping.go index 015f565..b080eca 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -41,8 +41,8 @@ var ping_command Command = Command{ Data: &discordgo.InteractionResponseData{ Embeds: []*discordgo.MessageEmbed{ { - Title: "Bot ping", - Description: fmt.Sprintf("**%.2fms**", ping.Seconds()*1000), + Title: s.State.User.Username + " ping", + Description: fmt.Sprintf("# %.2fms", ping.Seconds()*1000), Type: discordgo.EmbedTypeArticle, Color: hexToDecimal(color[ping_color]), }, From f898010bcb6ae208f47c19e907d94962f3f84b5b Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 22:06:00 +0100 Subject: [PATCH 6/6] added ask command --- cmd_ask.go | 34 ++++++++++++++++++++++++++++++++++ cmd_dadjoke.go | 35 +---------------------------------- config.go | 8 ++++---- gather_apis.go | 33 +++++++++++++++++++++++++++++++++ register_commands.go | 2 +- 5 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 cmd_ask.go create mode 100644 gather_apis.go diff --git a/cmd_ask.go b/cmd_ask.go new file mode 100644 index 0000000..8fc3e62 --- /dev/null +++ b/cmd_ask.go @@ -0,0 +1,34 @@ +package main + +import "github.com/bwmarrin/discordgo" + +var ask_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "ask", + Description: "Ask anything and get a gif as response!", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "question", + Description: "The question you want to ask", + Required: true, + }, + }, + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Type: discordgo.EmbedTypeImage, + Color: hexToDecimal(color["primary"]), + Image: &discordgo.MessageEmbedImage{ + URL: simpleGetFromAPI("image", "https://yesno.wtf/api").(string), + }, + }, + }, + }, + }) + }, +} diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index fe53f72..42b7898 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -1,11 +1,6 @@ package main import ( - "encoding/json" - "io" - "log" - "net/http" - "github.com/bwmarrin/discordgo" ) @@ -15,38 +10,10 @@ var dadjoke_command Command = Command{ Description: "Gives you a random joke that is as bad as your dad would tell them", }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - client := &http.Client{} - req, err := http.NewRequest("GET", "https://icanhazdadjoke.com/", nil) - if err != nil { - log.Println("Error creating request:", err) - return - } - req.Header.Set("Accept", "application/json") - resp, err := client.Do(req) - if err != nil { - log.Println("Error making request:", err) - return - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Println("Error reading response body:", err) - return - } - - type Joke struct { - Joke string `json:"joke"` - } - var joke Joke - err = json.Unmarshal(body, &joke) - if err != nil { - log.Println("Error decoding JSON:", err) - return - } s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: joke.Joke, + Content: simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string), }, }) }, diff --git a/config.go b/config.go index b9f179a..65d970c 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,8 @@ package main var color map[string]string = map[string]string{ - "red": "#FF6B6B", - "yellow": "#FFD93D", - "green": "#6BCB77", - "blue": "#4D96FF", + "red": "#FF6B6B", + "yellow": "#FFD93D", + "green": "#6BCB77", + "primary": "#4D96FF", } diff --git a/gather_apis.go b/gather_apis.go new file mode 100644 index 0000000..66f861b --- /dev/null +++ b/gather_apis.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "io" + "log" + "net/http" +) + +func simpleGetFromAPI(key string, url string) interface{} { + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Println("Error creating request:", err) + } + req.Header.Set("Accept", "application/json") + resp, err := client.Do(req) + if err != nil { + log.Println("Error making request:", err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error reading response body:", err) + } + + var result map[string]interface{} + err = json.Unmarshal(body, &result) + if err != nil { + log.Println("Error decoding JSON:", err) + } + return result[key] +} diff --git a/register_commands.go b/register_commands.go index 4f5f189..615f2a5 100644 --- a/register_commands.go +++ b/register_commands.go @@ -15,7 +15,7 @@ type Command struct { ModalID string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command} +var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command} func ready(s *discordgo.Session, event *discordgo.Ready) { for _, guild := range event.Guilds {