mirror of
https://github.com/aramperes/ts-activity.git
synced 2025-09-07 05:28:31 -04:00
Initial commit
This commit is contained in:
parent
343471e6a7
commit
f64a55b206
5 changed files with 234 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.envrc
|
29
Dockerfile
Normal file
29
Dockerfile
Normal file
|
@ -0,0 +1,29 @@
|
|||
FROM golang:latest as builder
|
||||
|
||||
# Set destination for COPY
|
||||
WORKDIR /app
|
||||
|
||||
# Download Go modules
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# Copy the source code.
|
||||
COPY *.go ./
|
||||
|
||||
# Build
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/ts-activity
|
||||
|
||||
# Runner image
|
||||
FROM alpine:latest
|
||||
|
||||
RUN adduser --disabled-password tsactivity
|
||||
RUN apk --no-cache add dumb-init
|
||||
WORKDIR /home/tsactivity
|
||||
|
||||
COPY --from=builder /app/ts-activity /home/tsactivity/ts-activity
|
||||
RUN chmod +x /home/tsactivity/ts-activity
|
||||
|
||||
# Run
|
||||
USER tsactivity
|
||||
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
|
||||
CMD ["/home/tsactivity/ts-activity"]
|
145
cmd.go
Normal file
145
cmd.go
Normal file
|
@ -0,0 +1,145 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gtuk/discordwebhook"
|
||||
"github.com/multiplay/go-ts3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
discord := os.Getenv("TS_DISCORD_WEBHOOK")
|
||||
if discord == "" {
|
||||
log.Fatal("Must configure: TS_DISCORD_WEBHOOK")
|
||||
}
|
||||
|
||||
// Connect and login
|
||||
c, err := ts3.NewClient(os.Getenv("TS_QUERY_ADDR"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
user := os.Getenv("TS_QUERY_USER")
|
||||
pass := os.Getenv("TS_QUERY_PASS")
|
||||
if err := c.Login(user, pass); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := c.Use(1); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if v, err := c.Whoami(); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.Println("Hello, ts-activity:", *v)
|
||||
}
|
||||
|
||||
// Subscribe to server events (e.g. client connections)
|
||||
if err := c.Register(ts3.ServerEvents); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// List current clients
|
||||
cl, err := c.Server.ClientList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
clientMap := make(map[int]string)
|
||||
|
||||
log.Println("Online clients:")
|
||||
for _, client := range cl {
|
||||
if client.Type != 0 {
|
||||
continue
|
||||
}
|
||||
log.Println("-", client)
|
||||
clientMap[client.ID] = client.Nickname
|
||||
}
|
||||
|
||||
// Listen for client updates
|
||||
notifs := c.Notifications()
|
||||
|
||||
for {
|
||||
event := <-notifs
|
||||
log.Println("=>", event)
|
||||
|
||||
if event.Type == "cliententerview" {
|
||||
if event.Data["client_type"] != "0" {
|
||||
continue
|
||||
}
|
||||
|
||||
clientId, err := strconv.Atoi(event.Data["clid"])
|
||||
if err != nil {
|
||||
log.Println("Failed to get client ID:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
clientNick, ok := event.Data["client_nickname"]
|
||||
if !ok {
|
||||
log.Println("User has no nickname:", clientId)
|
||||
continue
|
||||
}
|
||||
|
||||
_, previous := clientMap[clientId]
|
||||
clientMap[clientId] = clientNick
|
||||
|
||||
if !previous {
|
||||
ClientConnected(discord, clientNick)
|
||||
}
|
||||
} else if event.Type == "clientleftview" {
|
||||
clientId, err := strconv.Atoi(event.Data["clid"])
|
||||
if err != nil {
|
||||
log.Println("Failed to get client ID:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
clientNick, ok := clientMap[clientId]
|
||||
if !ok {
|
||||
log.Println("Unknown user left:", clientId)
|
||||
continue
|
||||
}
|
||||
|
||||
delete(clientMap, clientId)
|
||||
ClientDisconnected(discord, clientNick)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ClientConnected(discord string, nick string) {
|
||||
bot := os.Getenv("TS_DISCORD_USERNAME")
|
||||
if bot == "" {
|
||||
bot = "Jeff"
|
||||
}
|
||||
|
||||
content := fmt.Sprintf("Client connected: %s", nick)
|
||||
message := discordwebhook.Message{
|
||||
Username: &bot,
|
||||
Content: &content,
|
||||
}
|
||||
|
||||
if err := discordwebhook.SendMessage(discord, message); err != nil {
|
||||
log.Println("Failed to log Discord message:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func ClientDisconnected(discord string, nick string) {
|
||||
bot := os.Getenv("TS_DISCORD_USERNAME")
|
||||
if bot == "" {
|
||||
bot = "Jeff"
|
||||
}
|
||||
|
||||
content := fmt.Sprintf("Client disconnected: %s", nick)
|
||||
message := discordwebhook.Message{
|
||||
Username: &bot,
|
||||
Content: &content,
|
||||
}
|
||||
|
||||
if err := discordwebhook.SendMessage(discord, message); err != nil {
|
||||
log.Println("Failed to log Discord message:", err)
|
||||
}
|
||||
}
|
11
go.mod
Normal file
11
go.mod
Normal file
|
@ -0,0 +1,11 @@
|
|||
module github.com/aramperes/ts-activity
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gtuk/discordwebhook v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/multiplay/go-ts3 v1.1.0 // indirect
|
||||
golang.org/x/crypto v0.3.0 // indirect
|
||||
golang.org/x/sys v0.2.0 // indirect
|
||||
)
|
48
go.sum
Normal file
48
go.sum
Normal file
|
@ -0,0 +1,48 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gtuk/discordwebhook v1.1.0 h1:8vsfpzqbpXTWYvwbF4ghxUeXe0uP07wZeRNrAjW+WFM=
|
||||
github.com/gtuk/discordwebhook v1.1.0/go.mod h1:U3LdXNJ1e0bx3MMe2a4mB1VBantPHOPly2jNd8ZWXec=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/multiplay/go-ts3 v1.1.0 h1:OWOjRxBCRds+FbpyM1JKSscRbbmYr/IIrh6V78CM5Xw=
|
||||
github.com/multiplay/go-ts3 v1.1.0/go.mod h1:OdNmiO3uV++4SldaJDQTIGg8gNAu5MOiccZiAqVqUZA=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
Loading…
Add table
Add a link
Reference in a new issue