mirror of
https://github.com/mgerb/ServerStatus
synced 2026-01-08 10:32:48 +00:00
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.
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/bwmarrin/discordgo"
|
||||
version = "0.20.0"
|
||||
version = "0.24.0"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
|
||||
@@ -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 {
|
||||
|
||||
4
main.go
4
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{})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user