From e772367f2ee5cd4537a707a944bdbf7459df11c5 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 26 Aug 2022 17:39:38 +0200 Subject: [PATCH] Add files --- .gitignore | 2 ++ config_example.py | 5 +++ main.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 config_example.py create mode 100755 main.py diff --git a/.gitignore b/.gitignore index b6e4761..be1d91c 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ + +config.py \ No newline at end of file diff --git a/config_example.py b/config_example.py new file mode 100644 index 0000000..51ccc62 --- /dev/null +++ b/config_example.py @@ -0,0 +1,5 @@ +TOKEN="YOUR-DISCORD-BOT-TOKEN" + +allowed_users = ["discorduserid"] +services = ["services", "you", "want", "to", "monitor"] +command_prefix = ":" \ No newline at end of file diff --git a/main.py b/main.py new file mode 100755 index 0000000..7347434 --- /dev/null +++ b/main.py @@ -0,0 +1,77 @@ +#!/usr/bin/python3 +import asyncio +import time +import os + +import requests +import discord +from discord.ext import commands + +import config + + +def login_required(fn): + async def decorated_fn(ctx, *args): + if str(ctx.author.id) not in config.allowed_users: + await ctx.reply("Vous n'êtes pas autorisé à exécuter cette fonction.") + return None + return await fn(ctx, *args) + + return decorated_fn + +def check_status(service): + """Renvoie le statut d'un service""" + service_status = os.system(f"systemctl is-active --quiet {service}") + if service_status == 0: + return "✅ Running" + return "❌ Stopped" + +bot = commands.Bot(command_prefix=config.command_prefix) + + +@bot.event +async def on_ready(): + """On ready""" + print("Connecté en tant que:") + print(bot.user.name) + print(bot.user.id) + print("------") + + game = discord.Game(f"Disponible ✅ | {bot.command_prefix}help") + await bot.change_presence(status=discord.Status.idle, activity=game) + + services_status = {service:check_status(service) for service in config.services} + while True: + new_services_status = {service:check_status(service) for service in config.services} + for service in new_services_status.keys(): + if new_services_status[service] != services_status[service]: + + embed = discord.Embed(colour=discord.Colour.blue()) + embed.set_author(name=time.strftime("%m-%d-%Y %H:%M")) + embed.add_field( + name=service, value=new_services_status[service], inline=False + ) + for discord_id in config.allowed_users: + user = await bot.fetch_user(discord_id) + await user.send(embed=embed) + + services_status = new_services_status + await asyncio.sleep(300) + + + +@bot.command(name="status") +@login_required +async def status(ctx): + """Renvoie le statut des différents services""" + embed = discord.Embed(colour=discord.Colour.blue()) + embed.set_author(name=time.strftime("%m-%d-%Y %H:%M")) + for service in config.services: + service_status = check_status(service) + embed.add_field( + name=service, value=service_status, inline=False + ) + await ctx.reply(embed=embed) + + +bot.run(config.TOKEN)