basic setup, /ping added
This commit is contained in:
14
relay/commands/commands.go
Normal file
14
relay/commands/commands.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package commands
|
||||
|
||||
import "github.com/disgoorg/disgo/discord"
|
||||
|
||||
func getPingCommand() discord.SlashCommandCreate {
|
||||
return discord.SlashCommandCreate{
|
||||
Name: "ping",
|
||||
Description: "Check whether the bot is responding.",
|
||||
}
|
||||
}
|
||||
|
||||
var Commands = []discord.ApplicationCommandCreate{
|
||||
getPingCommand(),
|
||||
}
|
||||
47
relay/commands/ping.go
Normal file
47
relay/commands/ping.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
"github.com/disgoorg/disgo/handler"
|
||||
)
|
||||
|
||||
func PingHandler(e *handler.CommandEvent) error {
|
||||
|
||||
now := time.Now()
|
||||
latency := now.Sub(e.CreatedAt())
|
||||
msLatency := latency.Milliseconds()
|
||||
if msLatency < 0 {
|
||||
msLatency = -msLatency
|
||||
}
|
||||
|
||||
// Gateway latency
|
||||
var gwMs int64
|
||||
if gw := e.Client().Gateway(); gw != nil {
|
||||
gwMs = gw.Latency().Milliseconds()
|
||||
} else {
|
||||
gwMs = 0
|
||||
}
|
||||
|
||||
lat := float64(msLatency)
|
||||
if lat > 5000 {
|
||||
lat = 5000
|
||||
}
|
||||
red := int((lat / 5000) * 255)
|
||||
green := 255 - red
|
||||
color := (red << 16) | (green << 8)
|
||||
|
||||
embed := discord.NewEmbedBuilder().
|
||||
SetTitle("🏓 Pong!").
|
||||
AddField("Interaction latency", fmt.Sprintf("%d ms", msLatency), true).
|
||||
AddField("Gateway latency", fmt.Sprintf("%d ms", gwMs), true).
|
||||
SetColor(color).
|
||||
Build()
|
||||
|
||||
return e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||
SetEmbeds(embed).
|
||||
Build(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user