-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
146 lines (111 loc) · 3.94 KB
/
main.go
File metadata and controls
146 lines (111 loc) · 3.94 KB
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
144
145
146
/*
SRB2Kart discord bot, takes in kart server address, discord bot token and other options and runs forever
Copyright (C) 2026 Indev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"os"
"os/signal"
"github.com/Indev450/ganymede/kart"
)
func ensureEnv(name string) string {
value, ok := os.LookupEnv(name)
if !ok {
fmt.Println("Missing environment variable:", name)
os.Exit(1)
}
return value
}
func defaultEnv(name, def string) string {
value, ok := os.LookupEnv(name)
if !ok { return def }
return value
}
// :3
func copyleft() {
fmt.Println("Copyright (C) 2026 Indev")
fmt.Println("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>")
fmt.Println("This is free software: you are free to change and redistribute it.")
fmt.Println("There is NO WARRANTY, to the extent permitted by law.")
}
func main() {
// Target server for bot
address := defaultEnv("SRB2KART_ADDRESS", "127.0.0.1:5029")
// Which protocol to use?
protocol_name := defaultEnv("SRB2KART_PROTO", "srb2kart-16p")
// Override for gametype in status ("Watching n players %s")
status_gametype := os.Getenv("SRB2KART_STATUS_GAMEMODE")
// Override for gametype /players ("Player 1, PLayer 2 and Player 3 are %s at Green Hills Zone")
slashplayers_gametype := os.Getenv("SRB2KART_SLASHPLAYERS_GAMEMODE")
// Ignore this player for /players and number of players in status
seedplayer := os.Getenv("SRB2KART_SEEDPLAYER")
// File to open to fetch server gamemodes for /gamemode command
// Gamemodes are written one per line
gamemodefile := os.Getenv("SRB2KART_GAMEMODEFILE")
// Token
token := "Bot " + ensureEnv("DISCORD_TOKEN")
// Prefix for commands, for example DISCORD_CMDPREFIX="blan-" will turn /players and /gamemode into
// /blan-players and /blan-gamemode, useful if you have multiple bots for different games on server
cmdprefix := os.Getenv("DISCORD_CMDPREFIX")
proto := kart.GetProtocol(protocol_name)
if proto == nil {
fmt.Println("Unknown protocol:", protocol_name)
return
}
copyleft()
connection := kart.StartKartConnection(address, proto)
// Couldn't setup server connection, bot will not be able to do anything!
if connection == nil {
return
}
config := DiscordBotConfig {
Token: token,
Commands: make(map[string]Command),
SeedPlayer: seedplayer,
StatusGametype: status_gametype,
}
// /players
config.Commands[cmdprefix + "players"] = Command {
Func: func(config *DiscordBotConfig, connection *kart.KartConnection) string {
info, ok := connection.GetServerInfo()
if !ok {
return "Server is currently unavailable."
}
verb := slashplayers_gametype
if len(verb) == 0 {
verb = info.GetGametypeVerb()
}
return doSlashPlayers(&info, verb)
},
Description: "Get player info",
}
// /gamemode
if len(gamemodefile) > 0 {
config.Commands[cmdprefix + "gamemode"] = Command {
Func: func(config *DiscordBotConfig, connection *kart.KartConnection) string {
return doSlashGamemode(gamemodefile)
},
Description: "Get current gamemode",
}
}
// Starting discord session failed, bot will not be able to do anything!
if !startDiscordSession(&config, connection) {
return
}
// Run until canceled manually
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
<-sigch
// TODO - notify kart and discord threads so they can terminate gracefully?
}