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