Add files

This commit is contained in:
augustin64 2022-08-26 17:39:38 +02:00
parent 30e84b657b
commit e772367f2e
3 changed files with 84 additions and 0 deletions

2
.gitignore vendored
View File

@ -127,3 +127,5 @@ dmypy.json
# Pyre type checker
.pyre/
config.py

5
config_example.py Normal file
View File

@ -0,0 +1,5 @@
TOKEN="YOUR-DISCORD-BOT-TOKEN"
allowed_users = ["discorduserid"]
services = ["services", "you", "want", "to", "monitor"]
command_prefix = ":"

77
main.py Executable file
View File

@ -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)