Skip to content

Instantly share code, notes, and snippets.

@Tanami
Last active Jul 18, 2021
Embed
What would you like to do?
discord sidebar stats for srcds_linux / a2s
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
"github.com/rumblefrog/go-a2s"
)
type server struct {
name string
addr string
id string
}
var servers = []server{
{"🍺 Pub: ", "144.48.37.114:27015", "864832817749819452"},
{"🤍 WL: ", "144.48.37.118:27015", "864834961508007946"},
{"🪄 Trikz: ", "144.48.37.119:27015", "864834981649317899"},
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
go func() {
ticker := time.NewTicker(time.Second * 60)
for ; true; <-ticker.C {
for _, server := range servers {
process(s, server.name, server.addr, server.id)
}
}
}()
}
func process(s *discordgo.Session, name string, addr string, id string) {
go func() {
fmt.Println("working on " + name)
client, err := a2s.NewClient(addr)
if err != nil {
// don't care
fmt.Println("ruh roh")
} else {
defer client.Close()
info, err := client.QueryInfo()
if err != nil {
// todo: do something here
fmt.Printf("%s crapped itself\n", name)
} else {
real := float64(info.Players - info.Bots)
status := fmt.Sprintf("%s%0.f", name, real)
fmt.Println(status)
// todo: do not update same value to save rate limits
// update the discord bot
ch, err := s.ChannelEdit(id, status)
if err != nil {
spew.Dump(ch)
fmt.Println(err)
}
}
}
}()
}
func main() {
discord, err := discordgo.New("Bot " + "<auth token>")
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}
discord.AddHandler(ready)
err = discord.Open()
if err != nil {
fmt.Println("Error opening Discord session: ", err)
return
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
discord.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment