-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
397 lines (295 loc) · 11.9 KB
/
app.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
import discord
from discord import app_commands
from discord.ext import commands
from discord.app_commands import Choice
import os
import json
import aiohttp
import logging
from logging.handlers import TimedRotatingFileHandler
import dotenv
import python_aternos
dotenv.load_dotenv()
fP = os.path.dirname(os.path.realpath(__file__))
sP = os.path.dirname(os.path.realpath(__file__)) + "/sessions/{username}.aternos"
if not 'logs' in os.listdir(fP): os.mkdir(f"{fP}/logs")
if not 'sessions' in os.listdir(fP): os.mkdir(f"{fP}/sessions")
if not 'uconfig.json' in os.listdir(fP):
with open('uconfig.json', 'w') as f:
json.dump({"guilds": {}, "users": {}}, f, indent=2)
# Silence other loggers
#for log_name, log_obj in logging.Logger.manager.loggerDict.items():
# if log_name == "discord.client":
# log_obj.setLevel(logging.ERROR)
#
# elif log_name not in [__name__, "discord"]:
# log_obj.disabled = True
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S'
)
handler = TimedRotatingFileHandler(f"{fP}/logs/acs.log", when="midnight", interval=1)
handler.suffix = "%Y%m%d"
formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
intents = discord.Intents.default()
bot = commands.Bot(
command_prefix=commands.when_mentioned_or('aternos!'),
description="Obtain your Aternos servers status and control them!",
intents=intents
)
slash = bot.tree
bot.is_ready = False
@bot.event
async def on_ready():
if not bot.is_ready:
bot.is_ready = True
logger.info("%s Logged in as %s#%s %s", '<'*15, bot.user.name, bot.user.discriminator, '>'*15)
def get_config():
with open(f'{fP}/uconfig.json', 'r', encoding='utf-8') as f:
c = json.load(f)
return c
def save_config(cfg):
with open(f'{fP}/uconfig.json', 'w', encoding='utf-8') as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
def update_user(user_id, username: str=None, servers: list=None):
config = get_config()
u = config['users'].get(str(user_id))
if u:
username = username or u.get('username')
servers = servers or u.get('servers')
user = {
"username": username,
"servers": servers or []
}
config['users'][user_id] = user
save_config(config)
@bot.command()
async def sync(ctx):
if not str(ctx.message.author.id) == os.getenv('BOT_ADMIN'):
return
await bot.tree.sync()
await ctx.message.add_reaction('✅')
@bot.command()
async def showdb(ctx):
if not str(ctx.message.author.id) == os.getenv('BOT_ADMIN'):
return
await ctx.message.reply(file=discord.File(f"{fP}/uconfig.json"))
@slash.command()
async def informations(interaction: discord.Interaction):
msg = """
This bot does NOT save your credentials informations, but only your session settings, generated from the credentials you gave us.
We do not sell or use your data for anything more than providing this bot.
When you login to your Aternos account in a server, you allow EVERY member of the latter to start/stop & check status of your servers but only you will be able to perform elevated actions.
You will need to login to your account in all the servers you want your Aternos servers be made available (*might change*)
"""
await interaction.response.send_message(msg, ephemeral=True)
@slash.command(description="Login with your Aternos account in order to use this bot.")
@app_commands.describe(
username="Your Aternos username",
password="Your Aternos password, or its md5 hash"
)
async def login(interaction: discord.Interaction, username: str, password: str):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
await interaction.response.defer(ephemeral=True)
# Let's check that credentials are valid
try:
aclient = python_aternos.Client.from_credentials(username, password)
except python_aternos.CredentialsError:
await interaction.followup.send(
"Looks like your username and/or password is invalid. Please check and retry",
ephemeral=True
)
return
config = get_config()
if not config['guilds'].get(gid):
config['guilds'][gid] = {"logged_users": []}
config['guilds'][gid]['logged_users'].append(uid)
save_config(config)
update_user(uid, username=username, servers=[s.domain for s in aclient.list_servers()])
# Nice, let's save session settings for further interactions
aclient.save_session(file=sP.format(username=username))
await interaction.followup.send("✅ Successfully logged in!")
@slash.command(description="List servers available on this guild.")
async def list(interaction: discord.Interaction):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
await interaction.response.defer(ephemeral=True)
msg_e = discord.Embed(title="List of all Aternos servers available", description="")
config = get_config()
if config['guilds'].get(gid):
for user in config['guilds'][gid]['logged_users']:
cfg_user = config['users'].get(str(user))
if cfg_user:
msg_e.description += f"\n\nFrom <@{user}> ({cfg_user['username']}):"
auser = python_aternos.Client.restore_session(file=sP.format(username=cfg_user['username']))
update_user(user, servers=[s.domain for s in auser.list_servers()])
for server in auser.list_servers():
msg_e.description += f"\n\t- `{server.address}`, {server.version}"
else:
msg_e = discord.Embed(
title="❌ There's no available Aternos server in this guild!",
description="Start by login in using /login command",
color=0xFF0000
)
await interaction.followup.send(embed=msg_e, ephemeral=False)
@slash.command(description="Set the default server address for your guild.")
@app_commands.describe(
server_ip="The address of the server to set default."
)
async def setdefault(interaction: discord.Interaction, server_ip: str):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
config = get_config()
if not config['guilds'].get(gid):
config['guilds'][gid] = {"logged_users": []}
config['guilds'][gid]['default'] = server_ip
save_config(config)
await interaction.response.send_message(
f"✅ Done! `{server_ip}` is now set as your guild default Minecraft server!",
ephemeral=False
)
@slash.command(description="Get any server status")
@app_commands.describe(
private="Set to True if you don't want everyone to know you checked this server status.",
server_ip="The address of the server you wanna check"
)
async def status(interaction: discord.Interaction, server_ip: str="default", port: int=46390, private: bool=False):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
if server_ip == "default":
config = get_config()
guild = config['guilds'].get(gid)
if guild:
server_ip = guild.get('default')
if not server_ip:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
else:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
await interaction.response.defer(ephemeral=private)
async with aiohttp.ClientSession() as s:
async with s.get("https://mcapi.us/server/status", params={"ip": server_ip, "port": port}) as r:
res = json.loads(await r.text())
if res['status'] != "success":
msg = f"❌ There was an error.\n> {res['error']}"
if int(res['last_updated']) + 60*5 > time.time():
msg += "\n\n*/!\\ Be aware that the results are from less than 5 minutes ago, and thus might not be up to date!*"
else:
if res['players']['max'] == 0:
if "Server not found" in res['motd']:
msg = "❌ This aternos server was not found."
elif "This server is offline" in res['motd']:
msg = "❌ This aternos server is offline."
else:
if not res['online']:
msg = "❌ This server is offline"
else:
# Get server name, cleaned
sname = ''
for i, char in enumerate(res['motd']):
if char == "§" or res['motd'][max(0,i-1)] == "§":
continue
sname += char
msg = f"✅ **{sname}** is online!"
if res['players']['now'] > 0:
if res['players']['max'] == res['players']['now']:
msg += f"\n\nUnfortunately, the maximum number of {res['players']['max']} players has been reached.."
else:
msg += f"\n\nJoin the {res['players']['now']} current player{'s' if res['players']['now'] > 1 else ''}!"
msg += f"\n> ip: `{server_ip}`\n> version: `{res['server']['name']}`"
await interaction.followup.send(msg, ephemeral=private)
@slash.command(description="Turn on your Aternos servers")
@app_commands.describe(
private="Set to True if you don't want everyone to know you turned on this server.",
server_ip="The address of the server you wanna turn on"
)
async def turnon(interaction: discord.Interaction, server_ip: str="default", private: bool=False):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
if server_ip == "default":
config = get_config()
guild = config['guilds'].get(gid)
if guild:
server_ip = guild.get('default')
if not server_ip:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
else:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
await interaction.response.defer(ephemeral=private)
config = get_config()
for user in config['guilds'][gid]['logged_users']:
if server_ip in config['users'][user]['servers']:
aclient = python_aternos.Client.restore_session(file=sP.format(username=config['users'][user]['username']))
servers = aclient.list_servers()
for server in servers:
if server.address == server_ip or server.domain == server_ip:
try:
server.start()
msg = "✅ Server was successfully started! It should be up in 1 to 2 minutes."
except Exception as e:
msg = f"❌ An error occured.\n> {e}"
await interaction.followup.send(msg, ephemeral=private)
return
await interaction.followup.send("❌ No user logged in have this server. Ask the Aternos server owner to /login")
@slash.command(description="Turn off your Aternos servers")
@app_commands.describe(
private="Set to True if you don't want everyone to know you turned off this server.",
server_ip="The address of the server you wanna turn off"
)
async def turnoff(interaction: discord.Interaction, server_ip: str="default", private: bool=False):
gid = str(interaction.guild.id)
uid = str(interaction.user.id)
if server_ip == "default":
config = get_config()
guild = config['guilds'].get(gid)
if guild:
server_ip = guild.get('default')
if not server_ip:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
else:
await interaction.response.send_message(
"❌ This guild does NOT have any default server ip configured.. Use /setdefault to do so.",
ephemeral=False
)
return
await interaction.response.defer(ephemeral=private)
config = get_config()
for user in config['guilds'][gid]['logged_users']:
if server_ip in config['users'][user]['servers']:
aclient = python_aternos.Client.restore_session(file=sP.format(username=config['users'][user]['username']))
servers = aclient.list_servers()
for server in servers:
if server.address == server_ip or server.domain == server_ip:
try:
server.start()
msg = "✅ Server was successfully stopped!"
except Exception as e:
msg = f"❌ An error occured.\n> {e}"
await interaction.followup.send(msg, ephemeral=private)
return
await interaction.followup.send("❌ No user logged in have this server. Ask the Aternos server owner to /login")
bot.run(os.getenv('DISCORD_BOT_TOKEN'))