From 62dd8e60f9235139e2be22637bc387a9852b7fe3 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 18:55:37 +0100 Subject: [PATCH] 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) +}