-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_lookup.go
66 lines (60 loc) · 1.67 KB
/
player_lookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"regexp"
"strconv"
"github.com/Philipp15b/go-steamapi"
"go.etcd.io/bbolt"
"rkfg.me/ns2query/db"
)
var (
vanityRegex = regexp.MustCompile(`https://steamcommunity.com/id/([^/]*)/?`)
profileRegex = regexp.MustCompile(`https://steamcommunity.com/profiles/(\d*)/?`)
)
func playerIDFromDiscordName(username string) (uint32, error) {
var discordName string
err := bdb.View(func(t *bbolt.Tx) (err error) {
discordName, err = db.NewLowercaseBucket(t).FindFirstValue(username)
return
})
if err != nil {
return 0, fmt.Errorf("discord user name starting with '%s' was not found", username)
}
return getBind(discordName)
}
func playerIDFromSteamID(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.NewIdFromString(player)
if err != nil {
steamid, err = steamapi.NewIdFromVanityUrl(player, config.SteamKey)
if err != nil {
id64, err := strconv.ParseUint(player, 10, 64)
if err != nil {
return 0, fmt.Errorf("steam ID %s not found", player)
}
steamid = steamapi.NewIdFrom64bit(id64)
}
}
return steamid.As32Bit(), nil
}
func getPlayerAvatar(playerID uint32) string {
sum, err := steamapi.GetPlayerSummaries([]uint64{steamapi.NewIdFrom32bit(playerID).As64Bit()}, config.SteamKey)
if err != nil {
log.Printf("Error getting avatar for player %d: %s", playerID, err)
return ""
}
if len(sum) > 0 {
return sum[0].SmallAvatarURL
}
log.Printf("No data found for player %d", playerID)
return ""
}