added ping command

This commit is contained in:
2024-02-20 17:28:58 +01:00
parent 10e57fe2a0
commit b6b2493b5c
5 changed files with 39 additions and 5 deletions

38
command_ping.go Normal file
View File

@@ -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),
},
})
},
}

1
go.mod
View File

@@ -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
)

2
go.sum
View File

@@ -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=

View File

@@ -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)

View File

@@ -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 {