From b6b2493b5c7c65584f530e5258f9b2906d84847a Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 17:28:58 +0100 Subject: [PATCH] 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 {