-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
84 lines (70 loc) · 2.29 KB
/
run.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
## Version: 2.1.0
## License: https://github.com/redtrillix/DiscordComputerStatus/raw/main/LICENSE
## Git Repository: https://github.com/redtrillix/DiscordComputerStatus
## Changelog: https://github.com/redtrillix/DiscordComputerStatus/blob/main/CHANGELOG.txt
## Owner: Zachary G (redtrillix)
import discord
from discord import Intents
import os
from datetime import datetime
import socket
import psutil
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Discord bot token
TOKEN = os.getenv('TOKEN')
# Channel ID where you want to send the message
CHANNEL_ID = os.getenv('CHANNEL_ID')
# Retrieve services from .env and split them into a list
SERVICES = os.getenv('SERVICES').split(',')
# Get the current time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Get the hostname
hostname = socket.gethostname()
# Get the public IP address
try:
public_ip = requests.get('https://httpbin.org/ip').json()['origin']
except Exception as e:
public_ip = "Unavailable"
# Get CPU and memory usage
cpu_usage = psutil.cpu_percent()
memory_usage = psutil.virtual_memory().percent
# Create the detailed message
MESSAGE = f"**System Notification:**\n\n"
MESSAGE += f"🔔 System turned on at {current_time}.\n"
MESSAGE += f"🖥️ Hostname: {hostname}\n"
MESSAGE += f"🌐 Public IP Address: {public_ip}\n"
MESSAGE += f"💻 CPU Usage: {cpu_usage}%\n"
MESSAGE += f"🧠 Memory Usage: {memory_usage}%\n\n"
MESSAGE += "**Services Running:**\n\n"
MESSAGE += "```"
MESSAGE += f"{'Service':<20}{'Status':<10}\n"
MESSAGE += "-" * 30 + "\n"
for service in SERVICES:
MESSAGE += f"{service:<20}{'Running':<10}\n"
MESSAGE += "```"
# Define the intents
intents = Intents.default()
intents.typing = False
intents.presences = False
intents.messages = True # Enable receiving message events
# Create a Discord client with intents
client = discord.Client(intents=intents)
# Event called when the bot is ready
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
# Fetch the channel
channel = client.get_channel(int(CHANNEL_ID))
if channel:
# Send the message
await channel.send(MESSAGE)
else:
print("Channel not found.")
# Run the bot
client.run(TOKEN)