-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.go
143 lines (117 loc) · 3.15 KB
/
info.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
var (
DISCORD_TOKEN string
SQUAD_SERVER_ID string
BM_API_URL *url.URL
)
func init() {
flag.StringVar(&DISCORD_TOKEN, "t", "", "Discord bot token")
flag.StringVar(&SQUAD_SERVER_ID, "s", "", "Squad server id")
flag.Parse()
var err error
BM_API_URL, err = url.Parse("https://api.battlemetrics.com/servers/?fields[server]=players,maxPlayers,details")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type BMServer struct {
Data *BMServerData `json:"data"`
}
type BMServerData struct {
Id string `json:"id"`
Attributes *BMServerAttributes `json:"attributes"`
}
type BMServerAttributes struct {
Players int `json:"players"`
MaxPlayers int `json:"maxPlayers"`
Details *BMServerDetails `json:"details"`
}
type BMServerDetails struct {
Map string `json:"map"`
PublicQueue int `json:"squad_publicQueue"`
}
func main() {
dg, err := discordgo.New("Bot " + DISCORD_TOKEN)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
go update_status(dg)
go fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
dg.Close()
}
func update_status(discord_session *discordgo.Session) {
for {
bmServer, err := get_bm_info(SQUAD_SERVER_ID)
if err != nil {
fmt.Println("Ошибка при получении информации из Battlemetrics, ", err)
time.Sleep(10 * time.Second)
continue
}
var status_text string
if bmServer.Data.Attributes.Players+bmServer.Data.Attributes.Details.PublicQueue >= bmServer.Data.Attributes.MaxPlayers {
status_text = fmt.Sprintf(
"полный сервер, очередь %v, карта %v",
bmServer.Data.Attributes.Details.PublicQueue,
bmServer.Data.Attributes.Details.Map,
)
} else if bmServer.Data.Attributes.Players < 30 {
status_text = fmt.Sprintf("SEED, %v игроков", bmServer.Data.Attributes.Players)
} else {
status_text = fmt.Sprintf(
"%v(%v)/%v, карта %v",
bmServer.Data.Attributes.Players,
bmServer.Data.Attributes.Details.PublicQueue,
bmServer.Data.Attributes.MaxPlayers,
bmServer.Data.Attributes.Details.Map,
)
}
err = discord_session.UpdateGameStatus(0, status_text)
if err != nil {
fmt.Println("Ошибка при отправке сообщения в дискорд", err)
}
time.Sleep(10 * time.Second)
}
}
func get_bm_info(server_id string) (*BMServer, error) {
req, err := http.Get(BM_API_URL.JoinPath(server_id).String())
if err != nil {
return nil, err
}
defer req.Body.Close()
if req.StatusCode != 200 {
return nil, fmt.Errorf("status code is not 200, %v", req.StatusCode)
}
body, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
var bmServer BMServer
err = json.Unmarshal(body, &bmServer)
if err != nil {
return nil, err
}
return &bmServer, nil
}