generated from KOOKIIEStudios/python-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
318 lines (274 loc) · 11 KB
/
main.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import sys
import discord
from discord import app_commands
from client import fetch_config, Client
import logger
config = fetch_config()
log = logger.get_logger(__name__)
STEAM_PROFILE_URL = "https://steamcommunity.com/profiles/{steam_id}/"
class DiscordClient(discord.Client):
def __init__(self, *args, **kwargs):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(*args, **kwargs, intents=intents)
self.synced = False
async def on_ready(self):
await self.wait_until_ready()
if not self.synced:
await tree.sync()
self.synced = True
log.info("Bot is online!")
discord_client = DiscordClient()
tree = app_commands.CommandTree(discord_client)
# Bot helper functions ---------------------------------------------------------
def format_embed(embedded_message: discord.Embed) -> None:
embedded_message.set_footer(text=config["embed_footer"])
embedded_message.set_thumbnail(url=config["embed_thumbnail"])
# Start of Slash Commands ------------------------------------------------------
@tree.command(
name="info",
description="Get server information",
)
async def info(interaction: discord.Interaction):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
server_info, error_message = rcon_client.info()
if error_message:
error = error_message
if server_info:
embed_message = discord.Embed(
title=server_info.name,
colour=discord.Colour.blurple(),
description=f"Server Version: {server_info.version}",
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to fetch/send game server info: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@tree.command(
name="online",
description="Get information about all online players",
)
async def online(interaction: discord.Interaction):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
players, error_message = rcon_client.online()
if error_message:
error = error_message
player_count = len(players)
embed_message = discord.Embed(
title="Players Online",
colour=discord.Colour.blurple(),
description=f"Player(s) Online: {player_count}",
)
format_embed(embed_message)
# TODO: Add a pagination system for when there are a lot of players online
if player_count:
buffer = []
for key, value in players.items():
buffer.append(f"[{value}]({STEAM_PROFILE_URL.format(steam_id=key)})")
embed_message.add_field(name="Players", value="\n".join(buffer), inline=False)
except Exception as e:
log.error(f"Unable to fetch/send metadata of connected players: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@tree.command(
name="save",
description="Save the game server state",
)
@app_commands.checks.has_permissions(administrator=True)
async def save(interaction: discord.Interaction):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
response = rcon_client.save()
embed_message = discord.Embed(
title="Server Saving",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to save game server state: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@save.error
async def save_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
@tree.command(
name="shutdown",
description="Shutdown the server, with optional message and delay",
)
@app_commands.checks.has_permissions(administrator=True)
async def shutdown(interaction: discord.Interaction, seconds: int, message: str):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
# remove spaces
formatted_message = message.replace(" ", "_")
response = rcon_client.shutdown(str(seconds), formatted_message)
embed_message = discord.Embed(
title="Server Shutdown",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to shutdown game server: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@shutdown.error
async def shutdown_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
@tree.command(
name="announce",
description="Make an announcement in-game (spaces replaced with underscores)",
)
@app_commands.checks.has_permissions(administrator=True)
async def announce(interaction: discord.Interaction, message: str):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
# remove spaces
formatted_message = message.replace(" ", "_")
response = rcon_client.announce(formatted_message)
embed_message = discord.Embed(
title="Making In-game Announcement",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to make game announcement: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@announce.error
async def announce_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
@tree.command(
name="kick",
description="Kick a player from the game using Steam ID",
)
@app_commands.checks.has_permissions(administrator=True)
async def kick(interaction: discord.Interaction, steam_id: str):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
player_ign = rcon_client.get_ign_from_steam_id(steam_id)
formatted_ign = f"[{player_ign}]({STEAM_PROFILE_URL.format(steam_id=steam_id)})" if player_ign else ""
response = rcon_client.kick(steam_id)
embed_message = discord.Embed(
title=f"Kicking player {formatted_ign}",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to kick player: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@kick.error
async def kick_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
@tree.command(
name="ban_player",
description="Ban a player using Steam ID",
)
@app_commands.checks.has_permissions(administrator=True)
async def ban_player(interaction: discord.Interaction, steam_id: str):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
player_ign = rcon_client.get_ign_from_steam_id(steam_id)
formatted_ign = f"[{player_ign}]({STEAM_PROFILE_URL.format(steam_id=steam_id)})" if player_ign else ""
response = rcon_client.ban(steam_id)
embed_message = discord.Embed(
title=f"Banning player {formatted_ign}",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to ban player: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@ban_player.error
async def ban_player_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
@tree.command(
name="kill",
description="Force-kill the server immediately",
)
@app_commands.checks.has_permissions(administrator=True)
async def kill(interaction: discord.Interaction):
await interaction.response.defer()
embed_message = None
error = config["generic_bot_error"]
try:
rcon_client = Client(config=config)
response = rcon_client.force_stop()
embed_message = discord.Embed(
title="Forcing Server Termination",
colour=discord.Colour.blurple(),
description=response,
)
format_embed(embed_message)
except Exception as e:
log.error(f"Unable to forcibly terminate game server: {e}")
if embed_message:
await interaction.followup.send(embed=embed_message)
else:
await interaction.followup.send(content=error)
@kill.error
async def kill_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("You do not have the required permissions to use this command.")
# End of Slash Commands --------------------------------------------------------
def main(discord_bot_token):
if not config:
log.info("Shutting down PalCON...")
logger.shutdown_logger()
sys.exit(0)
log.info("Configuration files loaded")
log.info("Starting PalCON Discord Bot...")
discord_client.run(discord_bot_token)
if __name__ == "__main__":
main(config["discord_bot_token"])
logger.shutdown_logger()
sys.exit(0)