From ed232fc78565d9578e82dcb5a53ad31cc2cf0ca9 Mon Sep 17 00:00:00 2001 From: Ethorbit Date: Sun, 3 Mar 2024 00:29:58 -0800 Subject: [PATCH] Fix outdated broken bot discordgo upgraded from 0.20.0 to 0.24.0 which fixed the bot starting with a websocket error and remaining offline. Additionally, the command prefix and MessageHandling commands have been replaced with Discord's newer slash commands. --- Gopkg.toml | 2 +- config/config.go | 1 - main.go | 4 +- serverstatus/serverstatus.go | 77 ++++++++++++++++++++++++++---------- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/Gopkg.toml b/Gopkg.toml index 43dc884..c90f478 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,7 +31,7 @@ [[constraint]] name = "github.com/bwmarrin/discordgo" - version = "0.20.0" + version = "0.24.0" [prune] go-tests = true diff --git a/config/config.go b/config/config.go index 2a1b8ad..2feda38 100644 --- a/config/config.go +++ b/config/config.go @@ -19,7 +19,6 @@ type configStruct struct { Servers []Server `json:"Servers"` GameStatus string `json:"GameStatus"` PollingInterval time.Duration `json:"PollingInterval"` - BotPrefix string `json:"BotPrefix"` } type Server struct { diff --git a/main.go b/main.go index c1c9753..6349814 100644 --- a/main.go +++ b/main.go @@ -22,13 +22,13 @@ func main() { bot.Connect(config.Config.Token) // add handlers - bot.AddHandler(serverstatus.MessageHandler) + bot.AddHandler(serverstatus.InteractionHandler) //start websocket to listen for messages bot.Start() //start server status task - serverstatus.Start() + serverstatus.Start() // Simple way to keep program running until CTRL-C is pressed. <-make(chan struct{}) diff --git a/serverstatus/serverstatus.go b/serverstatus/serverstatus.go index 4d81f1a..96f794f 100644 --- a/serverstatus/serverstatus.go +++ b/serverstatus/serverstatus.go @@ -21,18 +21,36 @@ const ( blue = 0x42adf4 ) -// Start - start port scanner and bot listeners +// Start - add command, start port scanner and bot listeners func Start() { - //set each server status as online to start + //add command + _, err := bot.Session.ApplicationCommandCreate(bot.Session.State.User.ID, "", &discordgo.ApplicationCommand { + Name: "server-status", + Description: "Get the status of the servers.", + }) + + if err != nil { + log.Panicf("Cannot create status command '%v'", err) + } + + //set each server status as online to start for i := range config.Config.Servers { config.Config.Servers[i].Online = true config.Config.Servers[i].OnlineTimestamp = time.Now() config.Config.Servers[i].OfflineTimestamp = time.Now() } - err := bot.Session.UpdateStatus(0, config.Config.GameStatus) + err = bot.Session.UpdateStatusComplex(discordgo.UpdateStatusData { + Status: "online", + Activities: []*discordgo.Activity { + &discordgo.Activity { + Type: discordgo.ActivityTypeGame, + Name: config.Config.GameStatus, + }, + }, + }) - sendMessageToRooms(blue, "Server Status", "Bot started! Type !ServerStatus to see the status of your servers :smiley:", false) + sendMessageToRooms(blue, "Server Status", "Bot started! Type /server-status to see the status of your servers :smiley:", false) if err != nil { log.Println(err) @@ -132,24 +150,43 @@ func sendEmbeddedMessage(roomID string, color int, title, description string) { bot.Session.ChannelMessageSendEmbed(roomID, embed) } -// MessageHandler will be called every time a new -// message is created on any channel that the autenticated bot has access to. -func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { +// InteractionHandler will be called every time an interaction from a user occurs +// Command interaction handling requires bot command scope +func InteractionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + // A user is calling us with our status command + if i.ApplicationCommandData().Name == "server-status" { + online := "" + offline := "" - // Ignore all messages created by the bot itself - if m.Author.ID == bot.BotID { - return - } + for _, server := range config.Config.Servers { + if server.Online { + online = online + server.Name + " : " + fmtDuration(time.Since(server.OnlineTimestamp)) + "\n" + } else { + offline = offline + server.Name + " : " + fmtDuration(time.Since(server.OfflineTimestamp)) + "\n" + } + } - if m.Content == config.Config.BotPrefix+"ServerStatus" { - for _, server := range config.Config.Servers { - if server.Online { - sendEmbeddedMessage(m.ChannelID, green, server.Name, "Online!\nUptime: "+fmtDuration(time.Since(server.OnlineTimestamp))) - } else { - sendEmbeddedMessage(m.ChannelID, red, server.Name, "Offline!\nDowntime: "+fmtDuration(time.Since(server.OfflineTimestamp))) - } - } - } + // Only one message can be an interaction response. Messages can only contain up to 10 embeds. + // Our message will therefore instead be two embeds (online and offline), each with a list of servers in text. + // Embed descriptions can be ~4096 characters, so no limits should get hit with this. + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse { + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData { + Embeds: []*discordgo.MessageEmbed { + { + Title: ":white_check_mark: Online", + Color: green, + Description: online, + }, + { + Title: ":x: Offline", + Color: red, + Description: offline, + }, + }, + }, + }) + } } func fmtDuration(d time.Duration) string {