From a0c60f6fe134ea33ce9e43e4f65f640586cafa22 Mon Sep 17 00:00:00 2001 From: rkfg Date: Wed, 23 Dec 2020 15:57:07 +0300 Subject: [PATCH] Use embeds in seeding notifications --- bot.go | 7 ++++--- config_test.json | 2 +- query.go | 53 ++++++++++++++++++++++++++++++++++++++++-------- query_test.go | 25 +++++++++++++---------- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/bot.go b/bot.go index b54acb1..c1f111d 100644 --- a/bot.go +++ b/bot.go @@ -217,9 +217,10 @@ func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) { } } -func sendMsg(c chan string, s *discordgo.Session) { +func sendMsg(c chan discordgo.MessageSend, s *discordgo.Session) { for { - s.ChannelMessageSend(config.ChannelID, <-c) + msg := <-c + s.ChannelMessageSendComplex(config.ChannelID, &msg) time.Sleep(time.Second) } } @@ -251,7 +252,7 @@ func bot() (err error) { } defer dg.Close() dg.AddHandler(handleCommand) - sendChan := make(chan string, 10) + sendChan := make(chan discordgo.MessageSend, 10) go sendMsg(sendChan, dg) restartChan := make(chan bool) for i := range config.Servers { diff --git a/config_test.json b/config_test.json index d099720..3f06a2e 100644 --- a/config_test.json +++ b/config_test.json @@ -2,7 +2,7 @@ "query_interval": 10, "channel_id": "0", "token": "0", - "database_path": "/path/to/botdb/", + "bdb_database_path": "/path/to/botdb/", "servers": [ { "name": "TTO [Backup]", diff --git a/query.go b/query.go index 7c0bb97..3f640e4 100644 --- a/query.go +++ b/query.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/bwmarrin/discordgo" "github.com/rumblefrog/go-a2s" ) @@ -19,7 +20,7 @@ func (e queryError) Error() string { return fmt.Sprintf(e.description, e.err) } -func maybeNotify(srv *ns2server, sendChan chan string) { +func maybeNotify(srv *ns2server, sendChan chan discordgo.MessageSend) { playersCount := len(srv.players) newState := empty if playersCount < config.Seeding.Seeding { @@ -38,18 +39,52 @@ func maybeNotify(srv *ns2server, sendChan chan string) { srv.serverState = newState if srv.lastStateAnnounced != newState { srv.lastStateAnnounced = newState + specSlots := srv.SpecSlots + playerSlots := srv.PlayerSlots - len(srv.players) + freeSlots := playerSlots + srv.SpecSlots + if freeSlots < specSlots { + specSlots = freeSlots + } + if playerSlots < 0 { + playerSlots = 0 + } + msg := discordgo.MessageSend{Embed: &discordgo.MessageEmbed{ + Title: fmt.Sprintf("%s [%s]", srv.Name, srv.currentMap), + Fields: []*discordgo.MessageEmbedField{ + { + Name: "Players", + Value: fmt.Sprint(len(srv.players)), + Inline: true, + }, + { + Name: "Player slots", + Value: fmt.Sprint(playerSlots), + Inline: true, + }, + { + Name: "Spectator slots", + Value: fmt.Sprint(specSlots), + Inline: true, + }, + }, + Footer: &discordgo.MessageEmbedFooter{Text: fmt.Sprintf("Skill: %d", srv.avgSkill)}, + }, + } switch newState { case seedingstarted: - sendChan <- fmt.Sprintf("%s [%s] started seeding! Skill: %d. There are %d players there currently: %s", - srv.Name, srv.currentMap, srv.avgSkill, len(srv.players), srv.playersString()) + msg.Embed.Description = "Seeding started! Players on the server: " + srv.playersString() + msg.Embed.Color = 0x009900 srv.maxStateToMessage = specsonly + sendChan <- msg case almostfull: - sendChan <- fmt.Sprintf("%s [%s] is almost full! Skill: %d. There are %d players there currently", - srv.Name, srv.currentMap, srv.avgSkill, len(srv.players)) + msg.Embed.Description = "Server is almost full!" + msg.Embed.Color = 0xcc9900 + sendChan <- msg case specsonly: + msg.Embed.Description = "Server is full but you can still make it!" + msg.Embed.Color = 0xff3300 srv.maxStateToMessage = seedingstarted - sendChan <- fmt.Sprintf("%s [%s] is full but you can still make it! Skill: %d. There are %d spectator slots available currently", - srv.Name, srv.currentMap, srv.avgSkill, srv.PlayerSlots+srv.SpecSlots-len(srv.players)) + sendChan <- msg } } } else { @@ -63,7 +98,7 @@ func maybeNotify(srv *ns2server, sendChan chan string) { } } -func queryServer(client *a2s.Client, srv *ns2server, sendChan chan string) error { +func queryServer(client *a2s.Client, srv *ns2server, sendChan chan discordgo.MessageSend) error { info, err := client.QueryInfo() if err != nil { return queryError{"server info query: %s", err} @@ -94,7 +129,7 @@ func queryServer(client *a2s.Client, srv *ns2server, sendChan chan string) error return nil } -func query(srv *ns2server, sendChan chan string) { +func query(srv *ns2server, sendChan chan discordgo.MessageSend) { client, err := a2s.NewClient(srv.Address) if err != nil { log.Println("error creating client:", err) diff --git a/query_test.go b/query_test.go index 23c1a42..7bee6a0 100644 --- a/query_test.go +++ b/query_test.go @@ -6,6 +6,8 @@ import ( "strconv" "testing" "time" + + "github.com/bwmarrin/discordgo" ) func TestMain(t *testing.M) { @@ -16,7 +18,7 @@ func TestMain(t *testing.M) { } func notif(t *testing.T, srv *ns2server, expected string) { - sendChan := make(chan string) + sendChan := make(chan discordgo.MessageSend) q := make(chan bool) go func() { maybeNotify(srv, sendChan) @@ -24,10 +26,11 @@ func notif(t *testing.T, srv *ns2server, expected string) { }() select { case m := <-sendChan: + msg := m.Embed.Description if expected == "" { - t.Errorf("unexpected message reported: '%s'", m) - } else if m != expected { - t.Errorf("expected message '%s' got '%s'", expected, m) + t.Errorf("unexpected message reported: '%s'", msg) + } else if msg != expected { + t.Errorf("expected message '%s' got '%s'", expected, msg) } case <-q: if expected != "" { @@ -58,7 +61,7 @@ func TestNotification(t *testing.T) { } notif(t, srv, "") srv.players = fillPlayers(4) - notif(t, srv, "Test [test] started seeding! Skill: 0. There are 4 players there currently: 1, 2, 3, 4") + notif(t, srv, "Seeding started! Players on the server: 1, 2, 3, 4") // test demotion without cooldown srv.players = fillPlayers(3) notif(t, srv, "") @@ -67,9 +70,9 @@ func TestNotification(t *testing.T) { passTime(srv) notif(t, srv, "") srv.players = fillPlayers(13) - notif(t, srv, "Test [test] is almost full! Skill: 0. There are 13 players there currently") + notif(t, srv, "Server is almost full!") srv.players = fillPlayers(21) - notif(t, srv, "Test [test] is full but you can still make it! Skill: 0. There are 5 spectator slots available currently") + notif(t, srv, "Server is full but you can still make it!") srv.players = fillPlayers(26) notif(t, srv, "") // no message when full srv.players = fillPlayers(19) @@ -85,9 +88,9 @@ func TestNotification(t *testing.T) { notif(t, srv, "") // server became empty, seeding messages enabled again passTime(srv) srv.players = fillPlayers(7) - notif(t, srv, "Test [test] started seeding! Skill: 0. There are 7 players there currently: 1, 2, 3, 4, 5, 6, 7") + notif(t, srv, "Seeding started! Players on the server: 1, 2, 3, 4, 5, 6, 7") srv.players = fillPlayers(12) - notif(t, srv, "Test [test] is almost full! Skill: 0. There are 12 players there currently") + notif(t, srv, "Server is almost full!") srv.players = fillPlayers(6) passTime(srv) notif(t, srv, "") // some players left, seeding again but no message @@ -97,10 +100,10 @@ func TestNotification(t *testing.T) { passTime(srv) notif(t, srv, "") // server became empty, seeding messages enabled again srv.players = fillPlayers(4) - notif(t, srv, "Test [test] started seeding! Skill: 0. There are 4 players there currently: 1, 2, 3, 4") + notif(t, srv, "Seeding started! Players on the server: 1, 2, 3, 4") srv.players = fillPlayers(3) passTime(srv) notif(t, srv, "") // server became empty srv.players = fillPlayers(4) - notif(t, srv, "Test [test] started seeding! Skill: 0. There are 4 players there currently: 1, 2, 3, 4") // server is seeding again + notif(t, srv, "Seeding started! Players on the server: 1, 2, 3, 4") // server is seeding again }