Skip to content

Commit

Permalink
Add skill query
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Nov 13, 2020
1 parent ab0e9a7 commit b54253f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
94 changes: 87 additions & 7 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,83 @@ package main

import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"

"github.com/bwmarrin/discordgo"
"github.com/rkfg/go-steamapi"
)

const cmdPrefix = "-"

type hive struct {
Alias string
Skill int
SkillOffset int `json:"skill_offset"`
CommSkill int
CommSkillOffset int `json:"comm_skill_offset"`
}

var (
vanityRegex = regexp.MustCompile(`https://steamcommunity.com/id/([^/]*)/?`)
profileRegex = regexp.MustCompile(`https://steamcommunity.com/profiles/(\d*)/?`)
)

func steamIDFromPlayer(player string) (uint32, error) {
vanityName := vanityRegex.FindStringSubmatch(player)
if vanityName != nil {
player = vanityName[1]
} else {
profileID := profileRegex.FindStringSubmatch(player)
if profileID != nil {
player = profileID[1]
}
}
steamid, err := steamapi.NewIdFromVanityUrl(player, config.SteamKey)
if err != nil {
id64, err := strconv.ParseUint(player, 10, 64)
if err != nil {
return 0, fmt.Errorf("user/id %s not found", player)
}
steamid = steamapi.NewIdFrom64bit(id64)
}
id32 := steamid.As32Bit()
return id32, nil
}

func getSkill(player string) (string, error) {
id32, err := steamIDFromPlayer(player)
if err != nil {
return "", err
}
url := fmt.Sprintf("http://hive2.ns2cdt.com/api/get/playerData/%d", id32)
hiveResp, err := http.Get(url)
if err != nil {
return "", err
}
var skill hive
json.NewDecoder(hiveResp.Body).Decode(&skill)
if skill.Alias == "" {
return "", fmt.Errorf("user/id %s not found", player)
}
return fmt.Sprintf(
`%s (ID: %d) skill breakdown:
Marine skill: %d (commander: %d)
Alien skill: %d (commander: %d)
`, skill.Alias, id32,
skill.Skill+skill.SkillOffset, skill.CommSkill+skill.CommSkillOffset,
skill.Skill-skill.SkillOffset, skill.CommSkill-skill.CommSkillOffset), nil
}

func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
if s.State.User.ID == m.Author.ID {
return
Expand All @@ -25,15 +88,32 @@ func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
msg = strings.TrimPrefix(msg, cmdPrefix)
fields := strings.Fields(msg)
response := ""
switch msg {
case "status":
for i := range config.Servers {
srv := &config.Servers[i]
response += fmt.Sprintf("%s [%s], skill: %d, players: %d\n", srv.Name, srv.currentMap, srv.avgSkill, len(srv.players))
if len(fields) > 0 {
switch fields[0] {
case "status":
for i := range config.Servers {
srv := &config.Servers[i]
response += fmt.Sprintf("%s [%s], skill: %d, players: %d\n", srv.Name, srv.currentMap, srv.avgSkill, len(srv.players))
}
case "skill":
if len(fields) < 2 {
break
}
var err error
if response, err = getSkill(fields[1]); err != nil {
response = err.Error()
}
case "help":
response =
`Commands:
-status show server maps, skills and player count
-skill player show skill breakdown for player
If your Steam profile page URL looks like <https://steamcommunity.com/profiles/76561197960287930>, use 76561197960287930 as a -skill argument.
If it looks like <https://steamcommunity.com/id/gabelogannewell>, use gabelogannewell instead. Or just pass the entire URL, we don't judge!`
}
case "help":
response = "Commands:\n\t-status\t\tshow server maps, skills and player count"
}
if response != "" {
s.ChannelMessageSend(m.ChannelID, response)
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type seeding struct {

var config struct {
Token string
SteamKey string `json:"steam_key"`
ChannelID string `json:"channel_id"`
QueryInterval time.Duration `json:"query_interval"`
Servers []ns2server
Expand Down
3 changes: 2 additions & 1 deletion config_sample.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"token": "your.discordbot.token",
"channel_id": "channel identifier",
"query_interval": 10,
"steam_key": "get your key at https://steamcommunity.com/dev/apikey",
"query_interval": 60,
"servers": [
{
"name": "Server 1",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.15

require (
github.com/bwmarrin/discordgo v0.22.0
github.com/rkfg/go-steamapi v0.0.0-20201113090838-66f1df2acf40
github.com/rumblefrog/go-a2s v1.0.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/bwmarrin/discordgo v0.22.0 h1:uBxY1HmlVCsW1IuaPjpCGT6A2DBwRn0nvOguQIx
github.com/bwmarrin/discordgo v0.22.0/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/multiplay/go-source v0.0.0-20171201124051-cb29ef5320bb h1:n3wijII160M3fIQqHeLWULRK36kXmEWfrtSFMXNExk0=
github.com/multiplay/go-source v0.0.0-20171201124051-cb29ef5320bb/go.mod h1:o/iuncLTF1DDwozJ6BFN+Q00cvguIvUyRMSlHKIc/FM=
github.com/rkfg/go-steamapi v0.0.0-20201113090838-66f1df2acf40 h1:8ebSLdTPix7ZUJdPFhsM6BfGhZEre0UdEJWMmWrJBIg=
github.com/rkfg/go-steamapi v0.0.0-20201113090838-66f1df2acf40/go.mod h1:XWS07Cev9jhIvpAoBhLce/SvddN/x5nV5VbUnd97gFE=
github.com/rumblefrog/go-a2s v1.0.0 h1:9IIVIOQ1bXZJeTilmzkJDeGa/9W1c089VciTbp+Wp1Y=
github.com/rumblefrog/go-a2s v1.0.0/go.mod h1:JwbTgMTRGZcWzr3T2MUfDusrJU5Bdg8biEeZzPtN0So=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
Expand Down

0 comments on commit b54253f

Please sign in to comment.