diff --git a/README.md b/README.md index 9a026d7..68d8675 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/config.go b/config.go index 7ceef73..856161a 100644 --- a/config.go +++ b/config.go @@ -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 { @@ -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"` diff --git a/config_sample.json b/config_sample.json index 8be6846..dcb3e5f 100644 --- a/config_sample.json +++ b/config_sample.json @@ -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", diff --git a/query.go b/query.go index ca8ac88..cd5a084 100644 --- a/query.go +++ b/query.go @@ -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