FunguyBot/plugins/isup.py
2024-02-12 06:20:31 -07:00

38 lines
1.3 KiB
Python

# plugins/isup.py
import os
import logging
import simplematrixbotlib as botlib
async def handle_command(room, message, bot, PREFIX):
"""
Function to handle the !isup command.
Args:
room (Room): The Matrix room where the command was invoked.
message (RoomMessage): The message object containing the command.
Returns:
None
"""
match = botlib.MessageMatch(room, message, bot, PREFIX)
if match.is_not_from_this_bot() and match.prefix() and match.command("isup"):
logging.info("Received !isup command")
args = match.args()
if len(args) != 1:
await bot.api.send_text_message(room.room_id, "Usage: !isup <ipv4/ipv6/domain>")
logging.info("Sent usage message to the room")
return
target = args[0]
try:
response = os.system(f"ping -c 1 {target}")
if response == 0:
await bot.api.send_text_message(room.room_id, f"{target} is up")
else:
await bot.api.send_text_message(room.room_id, f"{target} is down")
logging.info(f"Sent status of {target} to the room")
except Exception as e:
await bot.api.send_text_message(room.room_id, f"Error: {e}")
logging.error(f"Error occurred while checking {target}: {e}")