mirror of
https://github.com/mgerb/ServerStatus
synced 2026-01-09 02:52:47 +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]]
|
[[constraint]]
|
||||||
name = "github.com/bwmarrin/discordgo"
|
name = "github.com/bwmarrin/discordgo"
|
||||||
version = "0.20.0"
|
version = "0.24.0"
|
||||||
|
|
||||||
[prune]
|
[prune]
|
||||||
go-tests = true
|
go-tests = true
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ type configStruct struct {
|
|||||||
Servers []Server `json:"Servers"`
|
Servers []Server `json:"Servers"`
|
||||||
GameStatus string `json:"GameStatus"`
|
GameStatus string `json:"GameStatus"`
|
||||||
PollingInterval time.Duration `json:"PollingInterval"`
|
PollingInterval time.Duration `json:"PollingInterval"`
|
||||||
BotPrefix string `json:"BotPrefix"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -22,13 +22,13 @@ func main() {
|
|||||||
bot.Connect(config.Config.Token)
|
bot.Connect(config.Config.Token)
|
||||||
|
|
||||||
// add handlers
|
// add handlers
|
||||||
bot.AddHandler(serverstatus.MessageHandler)
|
bot.AddHandler(serverstatus.InteractionHandler)
|
||||||
|
|
||||||
//start websocket to listen for messages
|
//start websocket to listen for messages
|
||||||
bot.Start()
|
bot.Start()
|
||||||
|
|
||||||
//start server status task
|
//start server status task
|
||||||
serverstatus.Start()
|
serverstatus.Start()
|
||||||
|
|
||||||
// Simple way to keep program running until CTRL-C is pressed.
|
// Simple way to keep program running until CTRL-C is pressed.
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
|
|||||||
@@ -21,18 +21,36 @@ const (
|
|||||||
blue = 0x42adf4
|
blue = 0x42adf4
|
||||||
)
|
)
|
||||||
|
|
||||||
// Start - start port scanner and bot listeners
|
// Start - add command, start port scanner and bot listeners
|
||||||
func Start() {
|
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 {
|
for i := range config.Config.Servers {
|
||||||
config.Config.Servers[i].Online = true
|
config.Config.Servers[i].Online = true
|
||||||
config.Config.Servers[i].OnlineTimestamp = time.Now()
|
config.Config.Servers[i].OnlineTimestamp = time.Now()
|
||||||
config.Config.Servers[i].OfflineTimestamp = 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 {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@@ -132,24 +150,43 @@ func sendEmbeddedMessage(roomID string, color int, title, description string) {
|
|||||||
bot.Session.ChannelMessageSendEmbed(roomID, embed)
|
bot.Session.ChannelMessageSendEmbed(roomID, embed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageHandler will be called every time a new
|
// InteractionHandler will be called every time an interaction from a user occurs
|
||||||
// message is created on any channel that the autenticated bot has access to.
|
// Command interaction handling requires bot command scope
|
||||||
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
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
|
for _, server := range config.Config.Servers {
|
||||||
if m.Author.ID == bot.BotID {
|
if server.Online {
|
||||||
return
|
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" {
|
// Only one message can be an interaction response. Messages can only contain up to 10 embeds.
|
||||||
for _, server := range config.Config.Servers {
|
// Our message will therefore instead be two embeds (online and offline), each with a list of servers in text.
|
||||||
if server.Online {
|
// Embed descriptions can be ~4096 characters, so no limits should get hit with this.
|
||||||
sendEmbeddedMessage(m.ChannelID, green, server.Name, "Online!\nUptime: "+fmtDuration(time.Since(server.OnlineTimestamp)))
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse {
|
||||||
} else {
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
sendEmbeddedMessage(m.ChannelID, red, server.Name, "Offline!\nDowntime: "+fmtDuration(time.Since(server.OfflineTimestamp)))
|
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 {
|
func fmtDuration(d time.Duration) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user