mirror of
https://github.com/mgerb/ServerStatus
synced 2026-01-11 03:32:50 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 25ab128a58 | |||
|
|
e4320bcecb | ||
| 50a06b98be | |||
|
|
ed232fc785 | ||
| 12c20b928c | |||
| 2fdc0eddd3 | |||
| 18b1a0b6e3 | |||
| bf79d376f3 | |||
| 8f430b5982 |
21
Dockerfile
21
Dockerfile
@@ -1,17 +1,26 @@
|
|||||||
FROM golang:1.11.1-alpine
|
FROM golang:1.22-alpine3.19
|
||||||
|
|
||||||
WORKDIR /go/src/github.com/mgerb/ServerStatus
|
WORKDIR /go/src/github.com/mgerb/ServerStatus
|
||||||
ADD . .
|
ADD . .
|
||||||
RUN apk add --no-cache git alpine-sdk
|
RUN apk add --no-cache git alpine-sdk
|
||||||
RUN go get -u github.com/golang/dep/cmd/dep
|
RUN go get
|
||||||
RUN dep ensure
|
|
||||||
RUN make linux
|
RUN make linux
|
||||||
|
|
||||||
|
|
||||||
FROM alpine:3.8
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
ARG UNAME="server-status"
|
||||||
|
ARG GNAME="server-status"
|
||||||
|
ARG UID=1000
|
||||||
|
ARG GID=1000
|
||||||
|
|
||||||
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
|
|
||||||
WORKDIR /server-status
|
WORKDIR /server-status
|
||||||
COPY --from=0 /go/src/github.com/mgerb/ServerStatus/dist/ServerStatus-linux .
|
COPY --from=0 /go/src/github.com/mgerb/ServerStatus/dist/ServerStatus-linux .
|
||||||
ENTRYPOINT ./ServerStatus-linux
|
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
|
||||||
|
RUN addgroup -g ${GID} "${GNAME}"
|
||||||
|
RUN adduser -D -u ${UID} -G "${GNAME}" "${UNAME}" &&\
|
||||||
|
chown "${UNAME}":"${GNAME}" -R /server-status/
|
||||||
|
|
||||||
|
USER ${UNAME}
|
||||||
|
|
||||||
|
ENTRYPOINT ./ServerStatus-linux
|
||||||
|
|||||||
90
Gopkg.lock
generated
90
Gopkg.lock
generated
@@ -1,90 +0,0 @@
|
|||||||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
|
||||||
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
digest = "1:05eebdd5727fea23083fce0d98d307d70c86baed644178e81608aaa9f09ea469"
|
|
||||||
name = "github.com/Sirupsen/logrus"
|
|
||||||
packages = ["."]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "60c74ad9be0d874af0ab0daef6ab07c5c5911f0d"
|
|
||||||
version = "v1.6.0"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
digest = "1:b7e05c8da029b985907b15e126cde5f42f2814b08211a184dac436eeec6fe780"
|
|
||||||
name = "github.com/anvie/port-scanner"
|
|
||||||
packages = [
|
|
||||||
".",
|
|
||||||
"predictors",
|
|
||||||
"predictors/webserver",
|
|
||||||
]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "8159197d3770eb6dbf3a9706a6d40462ebb69cec"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
digest = "1:d87c9221a974263e3b369bfd3513707b2a53e27a6cd799d472f94dc6a6157e59"
|
|
||||||
name = "github.com/bwmarrin/discordgo"
|
|
||||||
packages = ["."]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "ed4d6904961d1688b3f5601b3d73e95a71046734"
|
|
||||||
version = "v0.20.3"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
digest = "1:6d29f02f0f01c627c2be40fb7347669a9ff2aa215cb97747294c1d13ffa74bdd"
|
|
||||||
name = "github.com/gorilla/websocket"
|
|
||||||
packages = ["."]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "b65e62901fc1c0d968042419e74789f6af455eb9"
|
|
||||||
version = "v1.4.2"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
digest = "1:50993a8fbb3042b88dfecb8f4473a42f13ac70d6fd86f64df525a28b357c2d5b"
|
|
||||||
name = "github.com/kidoman/go-steam"
|
|
||||||
packages = ["."]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "2e40e0d508cbac591bab4ae18b231153295f3a0a"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
digest = "1:09cb61dc19af93deae01587e2fdb1c081e0bf48f1a5ad5fa24f48750dc57dce8"
|
|
||||||
name = "github.com/konsorten/go-windows-terminal-sequences"
|
|
||||||
packages = ["."]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "edb144dfd453055e1e49a3d8b410a660b5a87613"
|
|
||||||
version = "v1.0.3"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
digest = "1:1714bd928fd176237ccdea21695bf72801c1ee5f51bccefb69b87f0fd376a6aa"
|
|
||||||
name = "golang.org/x/crypto"
|
|
||||||
packages = [
|
|
||||||
"internal/subtle",
|
|
||||||
"nacl/secretbox",
|
|
||||||
"poly1305",
|
|
||||||
"salsa20/salsa",
|
|
||||||
]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "70a84ac30bf957c7df57edd1935d2081871515e1"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
branch = "master"
|
|
||||||
digest = "1:145abe7dfa46d17ef35e5126ed1cc87b9100e78b9f428c9460deea34bfeabafb"
|
|
||||||
name = "golang.org/x/sys"
|
|
||||||
packages = [
|
|
||||||
"cpu",
|
|
||||||
"internal/unsafeheader",
|
|
||||||
"unix",
|
|
||||||
]
|
|
||||||
pruneopts = "UT"
|
|
||||||
revision = "226ff32320da7b90d0b5bc2365f4e359c466fb78"
|
|
||||||
|
|
||||||
[solve-meta]
|
|
||||||
analyzer-name = "dep"
|
|
||||||
analyzer-version = 1
|
|
||||||
input-imports = [
|
|
||||||
"github.com/anvie/port-scanner",
|
|
||||||
"github.com/bwmarrin/discordgo",
|
|
||||||
"github.com/kidoman/go-steam",
|
|
||||||
]
|
|
||||||
solver-name = "gps-cdcl"
|
|
||||||
solver-version = 1
|
|
||||||
42
Gopkg.toml
42
Gopkg.toml
@@ -1,42 +0,0 @@
|
|||||||
# Gopkg.toml example
|
|
||||||
#
|
|
||||||
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
|
|
||||||
# for detailed Gopkg.toml documentation.
|
|
||||||
#
|
|
||||||
# required = ["github.com/user/thing/cmd/thing"]
|
|
||||||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
|
||||||
#
|
|
||||||
# [[constraint]]
|
|
||||||
# name = "github.com/user/project"
|
|
||||||
# version = "1.0.0"
|
|
||||||
#
|
|
||||||
# [[constraint]]
|
|
||||||
# name = "github.com/user/project2"
|
|
||||||
# branch = "dev"
|
|
||||||
# source = "github.com/myfork/project2"
|
|
||||||
#
|
|
||||||
# [[override]]
|
|
||||||
# name = "github.com/x/y"
|
|
||||||
# version = "2.4.0"
|
|
||||||
#
|
|
||||||
# [prune]
|
|
||||||
# non-go = false
|
|
||||||
# go-tests = true
|
|
||||||
# unused-packages = true
|
|
||||||
|
|
||||||
|
|
||||||
[[constraint]]
|
|
||||||
branch = "master"
|
|
||||||
name = "github.com/anvie/port-scanner"
|
|
||||||
|
|
||||||
[[constraint]]
|
|
||||||
name = "github.com/bwmarrin/discordgo"
|
|
||||||
version = "0.20.0"
|
|
||||||
|
|
||||||
[prune]
|
|
||||||
go-tests = true
|
|
||||||
unused-packages = true
|
|
||||||
|
|
||||||
[[constraint]]
|
|
||||||
branch = "master"
|
|
||||||
name = "github.com/kidoman/go-steam"
|
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
{
|
{
|
||||||
"Token": "your bot token",
|
"Token": "your bot token",
|
||||||
"RoomIDList":["room id list goes here"],
|
"RoomIDList": ["room id list goes here"],
|
||||||
"RolesToNotify": ["<@&roleid>", "<@userid>"],
|
"RolesToNotify": ["<@&roleid>", "<@userid>"],
|
||||||
"GameStatus": "current playing game",
|
"GameStatus": "current playing game",
|
||||||
"PollingInterval": 10,
|
"PollingInterval": 10,
|
||||||
"BotPrefix": "!",
|
"Servers": [
|
||||||
"Servers": [
|
{
|
||||||
{
|
"Name": "Your awesome server",
|
||||||
"Name": "Your awesome server",
|
"Address": "game.server.com",
|
||||||
"Address": "game.server.com",
|
"Port": 80
|
||||||
"Port": 80
|
},
|
||||||
}, {
|
{
|
||||||
"Name": "Another awesome server",
|
"Name": "Another awesome server",
|
||||||
"Address": "awesome.server.com",
|
"Address": "awesome.server.com",
|
||||||
"Port": 8080
|
"Port": 8080
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
version: "2"
|
version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
server-status:
|
server-status:
|
||||||
|
|||||||
17
go.mod
Normal file
17
go.mod
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
module github.com/mgerb/ServerStatus
|
||||||
|
|
||||||
|
go 1.22.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770
|
||||||
|
github.com/bwmarrin/discordgo v0.27.1
|
||||||
|
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Sirupsen/logrus v1.0.6 // indirect
|
||||||
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
|
||||||
|
)
|
||||||
19
go.sum
Normal file
19
go.sum
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
github.com/Sirupsen/logrus v1.0.6 h1:HCAGQRk48dRVPA5Y+Yh0qdCSTzPOyU1tBJ7Q9YzotII=
|
||||||
|
github.com/Sirupsen/logrus v1.0.6/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U=
|
||||||
|
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770 h1:1KEvfMGAjISVzk3Ti6pfaOgtoC3naoU0LfiJooZDNO8=
|
||||||
|
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770/go.mod h1:QGzdstKeoHmMWwi9oNHZ7DQzEj9pi7H42171pkj9htk=
|
||||||
|
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
|
||||||
|
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb h1:+3H4rb1CvcN/BEuBlk774uxxab272M6YU9nb9p/GZm8=
|
||||||
|
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb/go.mod h1:PKiM4eL8SN6mJ38F9m6ZJpVtJKnmSxOxBsi2p3TOru4=
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
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{})
|
||||||
|
|||||||
11
readme.md
11
readme.md
@@ -1,8 +1,17 @@
|
|||||||
# Server Status
|
# Server Status
|
||||||
Monitors a list of servers and sends a chat notification when a server goes on or offline.
|
Monitors a list of servers and sends a chat notification when a server goes on or offline.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- send channel notifications
|
||||||
|
- track server up/down time
|
||||||
- **TCP** - should work with all servers
|
- **TCP** - should work with all servers
|
||||||
- **UDP** - [Source RCON Protocol](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) is supported
|
- **UDP** - [Source RCON Protocol](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) is supported
|
||||||
|
- [Docker](https://hub.docker.com/r/mgerb/server-status)
|
||||||
|
|
||||||
|
### Want to see more features?
|
||||||
|
|
||||||
|
[Submit a new issue](https://github.com/mgerb/ServerStatus/issues/new/choose)
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
- Download the latest release [here](https://github.com/mgerb/ServerStatus/releases)
|
- Download the latest release [here](https://github.com/mgerb/ServerStatus/releases)
|
||||||
@@ -41,7 +50,7 @@ services:
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
To get the current status of your servers simply type `!ServerStatus` in chat.
|
To get the current status of your servers simply type `/server-status` in chat.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,18 @@ 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() {
|
||||||
|
//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
|
//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
|
||||||
@@ -30,9 +40,17 @@ func Start() {
|
|||||||
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)
|
||||||
@@ -57,7 +75,7 @@ func scanServers() {
|
|||||||
|
|
||||||
for index := range config.Config.Servers {
|
for index := range config.Config.Servers {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go worker(index, &config.Config.Servers[index], &wg)
|
go worker(&config.Config.Servers[index], &wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@@ -66,7 +84,7 @@ func scanServers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func worker(index int, server *config.Server, wg *sync.WaitGroup) {
|
func worker(server *config.Server, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
prevServerUp := server.Online //set value to previous server status
|
prevServerUp := server.Online //set value to previous server status
|
||||||
@@ -78,26 +96,28 @@ func worker(index int, server *config.Server, wg *sync.WaitGroup) {
|
|||||||
for {
|
for {
|
||||||
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
|
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
|
||||||
serverUp = serverScanner.IsOpen(server.Port) //check if the port is open
|
serverUp = serverScanner.IsOpen(server.Port) //check if the port is open
|
||||||
|
|
||||||
|
// if server isn't up check RCON protocol (UDP)
|
||||||
|
if !serverUp {
|
||||||
|
host := server.Address + ":" + strconv.Itoa(server.Port)
|
||||||
|
steamConnection, err := steam.Connect(host)
|
||||||
|
if err == nil {
|
||||||
|
defer steamConnection.Close()
|
||||||
|
_, err := steamConnection.Ping()
|
||||||
|
if err == nil {
|
||||||
|
serverUp = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if serverUp || retryCounter >= 5 {
|
if serverUp || retryCounter >= 5 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
retryCounter++
|
retryCounter++
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if server isn't up check RCON protocol (UDP)
|
|
||||||
if !serverUp {
|
|
||||||
host := server.Address + ":" + strconv.Itoa(server.Port)
|
|
||||||
steamConnection, err := steam.Connect(host)
|
|
||||||
if err == nil {
|
|
||||||
defer steamConnection.Close()
|
|
||||||
_, err := steamConnection.Ping()
|
|
||||||
if err == nil {
|
|
||||||
serverUp = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if serverUp && serverUp != prevServerUp {
|
if serverUp && serverUp != prevServerUp {
|
||||||
server.OnlineTimestamp = time.Now()
|
server.OnlineTimestamp = time.Now()
|
||||||
sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
|
sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
|
||||||
@@ -130,30 +150,56 @@ 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
|
|
||||||
if m.Author.ID == bot.BotID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.Content == config.Config.BotPrefix+"ServerStatus" {
|
|
||||||
for _, server := range config.Config.Servers {
|
for _, server := range config.Config.Servers {
|
||||||
if server.Online {
|
if server.Online {
|
||||||
sendEmbeddedMessage(m.ChannelID, green, server.Name, "Online!\nUptime: "+fmtDuration(time.Since(server.OnlineTimestamp)))
|
online = online + server.Name + " : " + fmtDuration(time.Since(server.OnlineTimestamp)) + "\n"
|
||||||
} else {
|
} else {
|
||||||
sendEmbeddedMessage(m.ChannelID, red, server.Name, "Offline!\nDowntime: "+fmtDuration(time.Since(server.OfflineTimestamp)))
|
offline = offline + server.Name + " : " + fmtDuration(time.Since(server.OfflineTimestamp)) + "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
embeds := []*discordgo.MessageEmbed{}
|
||||||
|
|
||||||
|
if online != "" {
|
||||||
|
embeds = append(embeds, &discordgo.MessageEmbed{
|
||||||
|
Title: ":white_check_mark: Online",
|
||||||
|
Color: green,
|
||||||
|
Description: online,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if offline != "" {
|
||||||
|
embeds = append(embeds, &discordgo.MessageEmbed{
|
||||||
|
Title: ":x: Offline",
|
||||||
|
Color: red,
|
||||||
|
Description: offline,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only one message can be an interaction response. Messages can only contain up to 10 embeds.
|
||||||
|
// Our message will therefore instead be two embeds (online and offline), each with a list of servers in text.
|
||||||
|
// Embed descriptions can be ~4096 characters, so no limits should get hit with this.
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: embeds,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fmtDuration(d time.Duration) string {
|
func fmtDuration(d time.Duration) string {
|
||||||
|
|
||||||
days := int(d.Hours()) / 24
|
days := int(d.Hours()) / 24
|
||||||
hours := int(d.Hours()) % 60
|
hours := int(d.Hours()) % 24
|
||||||
minutes := int(d.Minutes()) % 60
|
minutes := int(d.Minutes()) % 60
|
||||||
seconds := int(d.Seconds()) % 60
|
seconds := int(d.Seconds()) % 60
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user