Skip to content

Commit

Permalink
Add user ping on server going down/up
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Feb 24, 2024
1 parent b02bcec commit 71e71ca
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ player (aka regular) that left the server is forgotten by the bot and can be ann
case the player leaves and rejoins in a short time (because of a crash or otherwise). If you want these announcements to go to a different
channel, set `regular_channel_id`.

`down_notify_ids` and `up_notify_ids` may be optionally set to arrays of Discord IDs to notify (ping) if the server goes down and back online. It's NOT your Discord username but a long unique number ID that you can find by right-clicking a user and choosing "Copy User ID" in the dropdown menu. These parameters should ALWAYS be set as arrays even if you only want to ping one user.

If you already have a database that's been populated before these changes, run the bot with `--reindex` to fill the Steam ID => Discord
index. All new players registering themselves with `-bind` will be indexed automatically.

Expand Down
68 changes: 43 additions & 25 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,33 @@ type regular struct {
}

type ns2server struct {
Name string `json:"name"`
Address string `json:"address"`
SpecSlots int `json:"spec_slots"`
PlayerSlots int `json:"player_slots"`
StatusTemplate string `json:"status_template"`
IDURL string `json:"id_url"`
QueryIDInterval time.Duration `json:"query_id_interval"`
AnnounceDelay time.Duration `json:"announce_delay"`
RegularTimeout time.Duration `json:"regular_timeout"`
RegularChannelID string `json:"regular_channel_id"`
regularTimeouts map[uint32]*time.Time
regularNames []string
newRegulars map[uint32]regular
announceScheduled bool
statusTemplate *template.Template
players []string
serverState state
maxStateToMessage state
lastStateAnnounced state
lastStatePromotion time.Time
currentMap string
avgSkill int
restartChan chan struct{}
failures int
downSince *time.Time
Name string `json:"name"`
Address string `json:"address"`
SpecSlots int `json:"spec_slots"`
PlayerSlots int `json:"player_slots"`
StatusTemplate string `json:"status_template"`
IDURL string `json:"id_url"`
QueryIDInterval time.Duration `json:"query_id_interval"`
AnnounceDelay time.Duration `json:"announce_delay"`
RegularTimeout time.Duration `json:"regular_timeout"`
RegularChannelID string `json:"regular_channel_id"`
DownNotifyDiscordIDs []string `json:"down_notify_ids"`
UpNotifyDiscordIDs []string `json:"up_notify_ids"`
regularTimeouts map[uint32]*time.Time
regularNames []string
newRegulars map[uint32]regular
announceScheduled bool
statusTemplate *template.Template
players []string
serverState state
maxStateToMessage state
lastStateAnnounced state
lastStatePromotion time.Time
currentMap string
avgSkill int
restartChan chan struct{}
failures int
downSince *time.Time
}

func (s *ns2server) playersString() string {
Expand Down Expand Up @@ -78,6 +80,22 @@ func (s *ns2server) playersString() string {
return result
}

func idsToPing(ids []string) (result string) {
for _, id := range ids {
result += fmt.Sprintf("<@%s>", id)
}
return
}

func (s *ns2server) formatDowntimeMsg(down bool) string {
if down {
return fmt.Sprintf("Server %s is down! %s", s.Name, idsToPing(s.DownNotifyDiscordIDs))
} else {
return fmt.Sprintf("Server %s is back up! Was down since: %s %s",
s.Name, s.downSince.Format(timeFormat), idsToPing(s.UpNotifyDiscordIDs))
}
}

type seeding struct {
Seeding int `json:"seeding"`
AlmostFull int `json:"almost_full"`
Expand Down
4 changes: 3 additions & 1 deletion config_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"announce_delay": 300,
"regular_timeout": 3600,
"regular_channel_id": "123412342564546234",
"status_template": "{{ .ServerName }} Pl:{{ .Players }}/{{ .PlayerSlots }}+{{ .SpecSlots }}={{ .TotalSlots }};{{ .Map }}@{{ .Skill }}"
"status_template": "{{ .ServerName }} Pl:{{ .Players }}/{{ .PlayerSlots }}+{{ .SpecSlots }}={{ .TotalSlots }};{{ .Map }}@{{ .Skill }}",
"down_notify_ids": ["373545713602910362", "1231241134234"],
"up_notify_ids": ["373545713602910362"]
},
{
"name": "Server 2",
Expand Down
5 changes: 2 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,11 @@ func (srv *ns2server) serverLoop() {
if srv.failures > config.FailureLimit && srv.downSince == nil {
now := time.Now().In(time.UTC)
srv.downSince = &now
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: fmt.Sprintf("Server %s is down!", srv.Name)}}
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: srv.formatDowntimeMsg(true)}}
}
} else {
if srv.failures > config.FailureLimit && srv.downSince != nil {
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: fmt.Sprintf("Server %s is back up! Was down since: %s",
srv.Name, srv.downSince.Format(timeFormat))}}
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: srv.formatDowntimeMsg(false)}}
srv.downSince = nil
}
srv.failures = 0
Expand Down

0 comments on commit 71e71ca

Please sign in to comment.