Add root permissions
This commit is contained in:
parent
ae9bbfa67a
commit
9908c06b3a
@ -1,6 +1,6 @@
|
|||||||
TOKEN="YOUR-DISCORD-BOT-TOKEN"
|
TOKEN="YOUR-DISCORD-BOT-TOKEN"
|
||||||
|
|
||||||
allowed_users = ["discorduserid"]
|
allowed_users = {"discorduserid": "*", "discorduserid2": ["services"]}
|
||||||
services = ["services", "you", "want", "to", "monitor"]
|
services = ["services", "you", "want", "to", "monitor"]
|
||||||
command_prefix = ":"
|
command_prefix = ":"
|
||||||
logs_dir = "/home/user/logs"
|
logs_dir = "/home/user/logs"
|
49
main.py
49
main.py
@ -14,7 +14,19 @@ import config
|
|||||||
|
|
||||||
def login_required(fn):
|
def login_required(fn):
|
||||||
async def decorated_fn(ctx, *args):
|
async def decorated_fn(ctx, *args):
|
||||||
if str(ctx.author.id) not in config.allowed_users:
|
if str(ctx.author.id) not in config.allowed_users.keys():
|
||||||
|
await ctx.reply("Vous n'êtes pas autorisé à exécuter cette commande.")
|
||||||
|
return None
|
||||||
|
return await fn(ctx, *args)
|
||||||
|
|
||||||
|
return decorated_fn
|
||||||
|
|
||||||
|
def root_required(fn):
|
||||||
|
async def decorated_fn(ctx, *args):
|
||||||
|
if str(ctx.author.id) not in config.allowed_users.keys():
|
||||||
|
await ctx.reply("Vous n'êtes pas autorisé à exécuter cette commande.")
|
||||||
|
return None
|
||||||
|
if config.allowed_users[str(ctx.author.id)] != "*":
|
||||||
await ctx.reply("Vous n'êtes pas autorisé à exécuter cette commande.")
|
await ctx.reply("Vous n'êtes pas autorisé à exécuter cette commande.")
|
||||||
return None
|
return None
|
||||||
return await fn(ctx, *args)
|
return await fn(ctx, *args)
|
||||||
@ -34,8 +46,11 @@ def get_temperature():
|
|||||||
except subprocess.CalledProcessError as err:
|
except subprocess.CalledProcessError as err:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.messages = True
|
||||||
|
intents.message_content = True
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=config.command_prefix)
|
bot = commands.Bot(command_prefix=config.command_prefix, intents=intents)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
@ -66,7 +81,8 @@ async def on_ready():
|
|||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Uptime", value=subprocess.check_output(["uptime", "-p"]).decode("utf-8")
|
name="Uptime", value=subprocess.check_output(["uptime", "-p"]).decode("utf-8")
|
||||||
)
|
)
|
||||||
for discord_id in config.allowed_users:
|
for discord_id in config.allowed_users.keys():
|
||||||
|
if config.allowed_users[discord_id] == "*":
|
||||||
user = await bot.fetch_user(discord_id)
|
user = await bot.fetch_user(discord_id)
|
||||||
await user.send(embed=embed)
|
await user.send(embed=embed)
|
||||||
|
|
||||||
@ -81,7 +97,8 @@ async def on_ready():
|
|||||||
embed.add_field(
|
embed.add_field(
|
||||||
name=service, value=new_services_status[service], inline=True
|
name=service, value=new_services_status[service], inline=True
|
||||||
)
|
)
|
||||||
for discord_id in config.allowed_users:
|
for discord_id in config.allowed_users.keys():
|
||||||
|
if config.allowed_users[discord_id] == "*":
|
||||||
user = await bot.fetch_user(discord_id)
|
user = await bot.fetch_user(discord_id)
|
||||||
await user.send(embed=embed)
|
await user.send(embed=embed)
|
||||||
|
|
||||||
@ -94,18 +111,23 @@ async def on_ready():
|
|||||||
@login_required
|
@login_required
|
||||||
async def status(ctx, *args):
|
async def status(ctx, *args):
|
||||||
"""Renvoie le statut des différents services"""
|
"""Renvoie le statut des différents services"""
|
||||||
|
if config.allowed_users[str(ctx.author.id)] == "*":
|
||||||
|
user_services = config.services
|
||||||
|
else:
|
||||||
|
user_services = config.allowed_users[str(ctx.author.id)]
|
||||||
|
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
embed = discord.Embed(colour=discord.Colour.blue())
|
embed = discord.Embed(colour=discord.Colour.blue())
|
||||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||||
for service in config.services:
|
for service in user_services:
|
||||||
service_status = check_status(service)
|
service_status = check_status(service)
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name=service, value=service_status, inline=False
|
name=service, value=service_status, inline=False
|
||||||
)
|
)
|
||||||
await ctx.reply(embed=embed)
|
await ctx.reply(embed=embed)
|
||||||
else:
|
else:
|
||||||
if args[0] not in config.services:
|
if args[0] not in user_services:
|
||||||
await ctx.reply(f"Service inconnu `{args[0]}`")
|
await ctx.reply(f"Service inconnu ou inaccessible `{args[0]}`")
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
out = subprocess.check_output(["systemctl", "status", args[0]], stderr=subprocess.STDOUT)
|
out = subprocess.check_output(["systemctl", "status", args[0]], stderr=subprocess.STDOUT)
|
||||||
@ -125,14 +147,19 @@ async def status(ctx, *args):
|
|||||||
@login_required
|
@login_required
|
||||||
async def service(ctx, *args):
|
async def service(ctx, *args):
|
||||||
"""Exécute une commande systemd"""
|
"""Exécute une commande systemd"""
|
||||||
|
if config.allowed_users[str(ctx.author.id)] == "*":
|
||||||
|
user_services = config.services
|
||||||
|
else:
|
||||||
|
user_services = config.allowed_users[str(ctx.author.id)]
|
||||||
|
|
||||||
error = None
|
error = None
|
||||||
commands = ["start", "stop", "enable", "disable", "restart"]
|
commands = ["start", "stop", "enable", "disable", "restart"]
|
||||||
if len(args) < 2:
|
if len(args) < 2:
|
||||||
error = "Paramètres manquants."
|
error = "Paramètres manquants."
|
||||||
elif args[0] not in commands:
|
elif args[0] not in commands:
|
||||||
error = f"Commande invalide `{args[0]}`"
|
error = f"Commande invalide `{args[0]}`"
|
||||||
elif args[1] not in config.services:
|
elif args[1] not in user_services:
|
||||||
error= f"Service inconnu `{args[1]}`"
|
error= f"Service inconnu ou inaccessible `{args[1]}`"
|
||||||
|
|
||||||
if error is not None:
|
if error is not None:
|
||||||
await ctx.reply(error)
|
await ctx.reply(error)
|
||||||
@ -142,7 +169,7 @@ async def service(ctx, *args):
|
|||||||
|
|
||||||
|
|
||||||
@bot.command(name="logs")
|
@bot.command(name="logs")
|
||||||
@login_required
|
@root_required
|
||||||
async def logs(ctx, *args):
|
async def logs(ctx, *args):
|
||||||
"""Renvoie des fichier logs"""
|
"""Renvoie des fichier logs"""
|
||||||
logs_files = [
|
logs_files = [
|
||||||
@ -168,7 +195,7 @@ async def logs(ctx, *args):
|
|||||||
|
|
||||||
|
|
||||||
@bot.command(name="temperature")
|
@bot.command(name="temperature")
|
||||||
@login_required
|
@root_required
|
||||||
async def temperature(ctx):
|
async def temperature(ctx):
|
||||||
embed = discord.Embed(colour=discord.Colour.blue())
|
embed = discord.Embed(colour=discord.Colour.blue())
|
||||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||||
|
Loading…
Reference in New Issue
Block a user