Skip to content

Commit

Permalink
Use embeds in seeding notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Dec 23, 2020
1 parent 9f1c5e3 commit a0c60f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
7 changes: 4 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down
53 changes: 44 additions & 9 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/bwmarrin/discordgo"
"github.com/rumblefrog/go-a2s"
)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 14 additions & 11 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"testing"
"time"

"github.com/bwmarrin/discordgo"
)

func TestMain(t *testing.M) {
Expand All @@ -16,18 +18,19 @@ 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)
close(q)
}()
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 != "" {
Expand Down Expand Up @@ -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, "")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit a0c60f6

Please sign in to comment.