-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEBot.py
460 lines (379 loc) · 20.5 KB
/
SEBot.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import discord
from discord.ext import commands, tasks
import aiohttp
import time
from discord import app_commands
import os
import sys
import redis
import subprocess
from dotenv import load_dotenv
from datetime import datetime, timezone, timedelta
import re
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN') #A .env file is needed or you can replace the "os.getenv('DISCORD_TOKEN')" with the token this applies to everything below this too.
REDIS_HOST = os.getenv('REDIS_HOST')
REDIS_PORT = int(os.getenv('REDIS_PORT'))
REDIS_DB = int(os.getenv('REDIS_DB'))
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD')
github_link = "https://github.com/Mr-Baguetter/Space-Engineers-Discord-Bot" # Change to your repo if you create a fork, or create a new repo
allowed_user_id = 617462103938302098 #Change this to your Discord id.
BOT_PATH = 'C:\\Users\\admin\\Downloads\\Discordbot\\Space-Engineers-Discord-Bot\\SEBot.py' #Change this to the directory the bot is in. "SEBot.py" dosent need to be changed unless renamed.
EXPRESS_SERVER_PATH = "C:\\Users\\admin\\Downloads\\Discordbot\\server.js" #Change this to the directory the javascript file is in. "server.js" dosent need to be changed unless renamed.
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD, db=REDIS_DB, decode_responses=True) #I would recommend setting up Redis since alot of the code requires it. Bot likely wont start without it.
SERVER_URL = 'http://localhost:3000/players' # URL of the Express server. If self hosted should be http://localhost:3000/players
CHECK_INTERVAL = 2
log_channel_key = "log_channel_id"
utc_minus_5 = timezone(timedelta(hours=-5)) #Change hours=x to your UTC time offset.
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)
start_time = None
server_was_offline = False
async def fetch_player_count():
async with aiohttp.ClientSession() as session:
async with session.get(SERVER_URL) as response:
if response.status == 200:
players = await response.json()
return len(players)
else:
return None
@tasks.loop(seconds=1)
async def update_status():
player_count = await fetch_player_count()
if player_count is not None:
await bot.change_presence(
activity=discord.Activity(type=discord.ActivityType.watching, name=f"{player_count}/16 players online"))
@bot.event
async def on_ready():
await bot.tree.sync()
global start_time
start_time = datetime.now(utc_minus_5)
bot.node_process = subprocess.Popen(['node', EXPRESS_SERVER_PATH], cwd=os.path.dirname(EXPRESS_SERVER_PATH))
print(f'Logged in as {bot.user.name}')
print('Commands have been synced.')
check_server_status.start()
update_status.start()
previous_players = set()
player_join_times = {}
@tasks.loop(seconds=CHECK_INTERVAL)
async def check_server_status():
global previous_players
global server_was_offline
try:
async with aiohttp.ClientSession() as session:
async with session.get(SERVER_URL) as response:
if response.status == 200:
current_players = await response.json()
current_player_names = set(player['name'] for player in current_players)
new_players = current_player_names - previous_players
for player in new_players:
player_join_times[player] = datetime.now(utc_minus_5)
await notify_player_joined(player)
left_players = previous_players - current_player_names
for player in left_players:
join_time = player_join_times.pop(player, None)
if join_time:
time_spent = datetime.now(utc_minus_5) - join_time
await notify_player_left(player, time_spent)
previous_players = current_player_names
if server_was_offline:
await notify_server_online()
server_was_offline = False
else:
if not server_was_offline:
await notify_server_offline()
server_was_offline = True
except aiohttp.ClientError:
if not server_was_offline:
await notify_server_offline()
server_was_offline = True
async def notify_player_joined(player_name):
if not player_name:
print("Player name is missing. Ignoring this event.")
return
log_channel_id = await get_log_channel_id()
if log_channel_id:
channel = bot.get_channel(int(log_channel_id))
if channel:
await channel.send(f"Player **{player_name}** has joined the server at {datetime.now(utc_minus_5).strftime('%H:%M:%S UTC')}.")
else:
print("Log channel not found.")
else:
print("Log channel has not been set.")
async def notify_player_left(player_name, time_spent):
if not player_name:
print("Player name is missing. Ignoring this event.")
return
log_channel_id = await get_log_channel_id()
notifications_key = "leave_notifications"
if log_channel_id:
channel = bot.get_channel(int(log_channel_id))
if channel:
hours, remainder = divmod(time_spent.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
time_spent_str = f"{int(hours)} hour{'s' if hours != 1 else ''}, {int(minutes)} minute{'s' if minutes != 1 else ''}, and {int(seconds)} second{'s' if seconds != 1 else ''}"
subscribed_user_ids = redis_client.smembers(notifications_key)
if subscribed_user_ids:
mentions = ' '.join([f"<@{user_id}>" for user_id in subscribed_user_ids])
await channel.send(f"Player **{player_name}** has left the server after {time_spent_str}. {mentions}")
else:
await channel.send(f"Player **{player_name}** has left the server after {time_spent_str}.")
else:
print("Log channel not found.")
else:
print("Log channel has not been set.")
@bot.tree.command(name="setlogchannel", description="Set the log channel for server status updates")
async def setlogchannel(interaction: discord.Interaction, channel: discord.TextChannel):
if interaction.user.id == allowed_user_id:
await set_log_channel_id(channel.id)
await interaction.response.send_message(f"Log channel has been set to {channel.mention}.")
else:
await interaction.response.send_message("You don't have permission to set the logs channel.")
async def set_log_channel_id(channel_id):
redis_client.set('log_channel_id', channel_id)
async def get_log_channel_id():
return redis_client.get('log_channel_id')
async def notify_server_offline():
print("Error: API is offline or the server is restarting.")
log_channel_id = await get_log_channel_id()
if log_channel_id:
channel = bot.get_channel(int(log_channel_id))
if channel:
await channel.send(
"Error: API is offline or the server is restarting. Please notify <@617462103938302098> if the API is down."
)
else:
print("Log channel not found.")
else:
print("Log channel has not been set.")
async def notify_server_online():
log_channel_id = await get_log_channel_id()
if log_channel_id:
channel = bot.get_channel(int(log_channel_id))
if channel:
await channel.send(
"Connection to the server reestablished. The API is back online!"
)
else:
print("Log channel not found.")
else:
print("Log channel has not been set.")
def seconds_to_hours_and_minutes(seconds):
hours, remainder = divmod(int(seconds), 3600)
minutes = remainder // 60
if hours > 0:
return f"{hours} hour{'s' if hours != 1 else ''}, {minutes} minute{'s' if minutes != 1 else ''}"
else:
return f"{minutes} minute{'s' if minutes != 1 else ''}"
@bot.tree.command(name='playerlist', description='Get the current players on Keen NA1!')
@app_commands.allowed_installs(guilds=True, users=True)
async def playerlist(interaction: discord.Interaction):
async with aiohttp.ClientSession() as session:
try:
async with session.get(SERVER_URL) as response:
if response.status == 200:
players = await response.json()
player_count = len(players)
if players:
embed = discord.Embed(
title=f"Current Players on the Server ({player_count}/16)",
description="Here are the players currently online, sorted by playtime:",
color=discord.Color.green()
)
for player in players:
playtime_seconds = player['raw'].get('time', 0)
playtime = seconds_to_hours_and_minutes(playtime_seconds)
embed.add_field(
name=player['name'],
value=f"Playtime: {playtime}",
inline=False
)
await interaction.response.send_message(embed=embed)
else:
await interaction.response.send_message('No players are currently online.')
else:
await interaction.response.send_message(
"Error: cannot fetch player list, please notify <@617462103938302098> that the API is down.",
ephemeral=True
)
except aiohttp.ClientError:
await interaction.response.send_message(
"Error: cannot fetch player list, please notify <@617462103938302098> that the API is down.",
ephemeral=True
)
@bot.tree.command(name="ping", description="Check the bot's latency and response time.")
@app_commands.allowed_installs(guilds=True, users=True)
async def ping(interaction: discord.Interaction):
start_time = time.time()
await interaction.response.send_message("Pinging...")
end_time = time.time()
latency = bot.latency * 1000
response_time = (end_time - start_time) * 1000
await interaction.edit_original_response(
content=f"Pong! Latency: {latency:.2f}ms | Response Time: {response_time:.2f}ms")
@bot.tree.command(name='help', description='Get information about the bot.')
@app_commands.allowed_installs(guilds=True, users=True)
async def info(interaction: discord.Interaction):
bot_description = (
"I am a bot created to get the player list of Keen NA1. "
"You can use various commands to interact with me and get information.\n"
"Available commands are:\n"
"/help : Shows this command\n"
"/playerlist : Lists all the players in NA1\n"
"/ping : Bot ping\n"
"/source : Bot source code\n"
"/orecalc : Ore calculator spreadsheet \n"
"/suggestion : Suggest things to be added to the bot \n"
"/uptime : View the bots uptime"
"/versioninfo : View latest changes."
"/playerleavenotification : Get notified when a player leaves the server."
'/stopplayerleavenotification : Stop getting notified when a player leaves the server.'
)
await interaction.response.send_message(f"Bot Info:\n{bot_description}")
@bot.tree.command(name='source', description='Get the link to the GitHub repository.')
@app_commands.allowed_installs(guilds=True, users=True)
async def link(interaction: discord.Interaction):
await interaction.response.send_message(f"Check out the source code here: {github_link}")
@bot.tree.command(name='orecalc', description='Get a link to the ore calculator spreadsheet.')
@app_commands.allowed_installs(guilds=True, users=True)
async def link(interaction: discord.Interaction):
spreadsheet_link = "https://docs.google.com/spreadsheets/d/1gXqODCeVkEtX4inPnZikTXRwo3n0_wzogJGda3W9JJg/edit?usp=sharing"
await interaction.response.send_message(f"Please make a copy of the spreadsheet here: {spreadsheet_link}")
@bot.tree.command(name='restartbot', description='Restart the bot. (Only Mr. Baguetter can run this)')
@app_commands.allowed_installs(guilds=True, users=True)
async def restartbot(interaction: discord.Interaction):
if interaction.user.id == allowed_user_id:
await interaction.response.send_message('Restarting the bot...')
print('Restart command issued.')
await bot.close()
os.execv(sys.executable, ['python', BOT_PATH])
else:
await interaction.response.send_message("You don't have permission to restart the bot.")
@bot.tree.command(name='suggestion', description='Submit a suggestion.')
@app_commands.describe(suggestion="Your suggestion for the bot.")
async def suggestion(interaction: discord.Interaction, suggestion: str):
suggestions_key = "suggestions_list"
redis_client.rpush(suggestions_key, suggestion)
await interaction.response.send_message(f"Thank you for your suggestion! Your idea has been submitted.")
@bot.tree.command(name='showsuggestions', description='Show all suggestions.')
@app_commands.allowed_installs(guilds=True, users=True)
async def showsuggestions(interaction: discord.Interaction):
if interaction.user.id == allowed_user_id:
suggestions_key = "suggestions_list"
suggestions = redis_client.lrange(suggestions_key, 0, -1)
if suggestions:
embed = discord.Embed(
title="Suggestions",
description="Here are the suggestions submitted:",
color=discord.Color.green()
)
for idx, suggestion in enumerate(suggestions, 1):
embed.add_field(
name=f"Suggestion #{idx}",
value=suggestion.decode('utf-8'),
inline=False
)
await interaction.response.send_message(embed=embed)
else:
await interaction.response.send_message("No suggestions found.")
else:
await interaction.response.send_message("You do not have permission to view suggestions.")
@bot.tree.command(name='changelog', description='Bot changelog')
@app_commands.allowed_installs(guilds=True, users=True)
async def changelog(interaction: discord.Interaction):
if interaction.user.id == allowed_user_id:
bot_changelog = (
"Changed how the playerlist command shows playtime. \n"
"Added the /uptime command"
)
await interaction.response.send_message(f"Changelog 9/16/24 (M/D/Y):\n{bot_changelog}")
else:
await interaction.response.send_message("You do not have permission to send the changelog")
def get_uptime():
now = datetime.now(utc_minus_5)
uptime_duration = now - start_time
days = uptime_duration.days
hours, remainder = divmod(uptime_duration.seconds, 3600)
minutes, _ = divmod(remainder, 60)
time_components = []
if days > 0:
time_components.append(f"{days} day{'s' if days != 1 else ''}")
if hours > 0:
time_components.append(f"{hours} hour{'s' if hours != 1 else ''}")
if minutes > 0 or (days == 0 and hours == 0):
time_components.append(f"{minutes} minute{'s' if minutes != 1 else ''}")
if len(time_components) > 1:
uptime_str = ', '.join(time_components[:-1]) + f", and {time_components[-1]}"
else:
uptime_str = time_components[0]
return uptime_str
@bot.tree.command(name='uptime', description="Displays the bot's uptime")
@app_commands.allowed_installs(guilds=True, users=True)
async def uptime(interaction: discord.Interaction):
uptime_str = get_uptime()
await interaction.response.send_message(f"Bot has been up for: {uptime_str}")
@bot.tree.command(name='shutdown', description='shutdown the bot. (Only Mr. Baguetter can run this!)')
@app_commands.allowed_installs(guilds=True, users=True)
async def shutdown(interaction: discord.Interaction):
if interaction.user.id == allowed_user_id:
await interaction.response.send_message('Shuting down the bot...')
print('Shutdown command issued.')
await bot.close()
else:
await interaction.response.send_message("You don't have permission to shutdown the bot.")
BOT_VERSION = "1.6.6"
LATEST_ADDITIONS = "1.6.0 Added player join/leave logging. \n 1.6.1 Added time joined/left to player join/leave logs. \n 1.6.2 Added player leave notification commands. \n 1.6.3 Code improvements. No noticable changes. \n 1.6.4 - 1.6.5 Bug chasing. \n 1.6.6 Added the /serverset command"
@bot.tree.command(name='versioninfo', description='Get the current version information of the bot.')
@app_commands.allowed_installs(guilds=True, users=True)
async def versioninfo(interaction: discord.Interaction):
embed = discord.Embed(
title="Bot Version Information",
description="Here is the current version information for the bot.",
color=discord.Color.gold()
)
embed.add_field(name="Bot Version", value=BOT_VERSION, inline=False)
embed.add_field(name="Latest Additions", value=LATEST_ADDITIONS, inline=False)
await interaction.response.send_message(embed=embed)
@bot.tree.command(name="playerleavenotification", description="Subscribe to notifications when a player leaves.")
async def playerleavenotification(interaction: discord.Interaction):
user_id = str(interaction.user.id)
notifications_key = "leave_notifications"
if redis_client.sismember(notifications_key, user_id):
await interaction.response.send_message("You are already subscribed to player leave notifications.", ephemeral=True)
else:
redis_client.sadd(notifications_key, user_id)
await interaction.response.send_message("You have successfully subscribed to player leave notifications.", ephemeral=True)
@bot.tree.command(name="stopplayerleavenotification", description="Unsubscribe from notifications when a player leaves.")
async def stopplayerleavenotification(interaction: discord.Interaction):
user_id = str(interaction.user.id)
notifications_key = "leave_notifications"
if redis_client.sismember(notifications_key, user_id):
redis_client.srem(notifications_key, user_id)
await interaction.response.send_message("You have successfully unsubscribed from player leave notifications.", ephemeral=True)
else:
await interaction.response.send_message("You are not subscribed to player leave notifications.", ephemeral=True)
@bot.tree.command(name="serverset", description="Set the server IP address and port")
@app_commands.describe(ip="The IP address to set for the server", port="The port number to set for the server (1-65535)")
async def serverset(interaction: discord.Interaction, ip: str, port: int):
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
await interaction.response.send_message("Invalid IP format. Please use a valid IP address.", ephemeral=True)
return
if not (0 < port < 65536):
await interaction.response.send_message("Invalid port. Please provide a valid port number between 1 and 65535.", ephemeral=True)
return
try:
with open(EXPRESS_SERVER_PATH, 'r') as file:
content = file.read()
content = re.sub(r"host: '.*?'", f"host: '{ip}'", content)
content = re.sub(r"port: \d+", f"port: {port}", content)
with open(EXPRESS_SERVER_PATH, 'w') as file:
file.write(content)
if hasattr(bot, 'node_process') and bot.node_process:
bot.node_process.terminate()
bot.node_process.wait()
bot.node_process = subprocess.Popen(['node', EXPRESS_SERVER_PATH], cwd=os.path.dirname(EXPRESS_SERVER_PATH))
await interaction.response.send_message(f"Server IP and port successfully updated to: {ip}:{port}. Node.js server restarted.", ephemeral=True)
except Exception as e:
await interaction.response.send_message(f"Error updating server settings: {e}", ephemeral=True)
bot.run(TOKEN)