-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·92 lines (74 loc) · 2.43 KB
/
bot.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
85
86
87
88
89
90
91
92
#!/usr/bin/python
import discord
import psutil
import asyncio
from datetime import datetime
from gpustat.core import GPUStatCollection, GPUStat
# Your bot token
TOKEN = open("token.txt").read()
intents = discord.Intents.default()
# Discord client
client = discord.Client(intents=intents)
gpu_on = False
last_start = None
# Function to get GPU usage
def get_gpu_stats() -> GPUStat:
# This is a placeholder function to simulate getting GPU usage
# You may need to use a library like pynvml for Nvidia GPUs or similar for AMD
stats = GPUStatCollection.new_query()
gpu0_stats = stats[0]
return gpu0_stats
# Function to get memory usage
def get_memory_usage():
memory = psutil.virtual_memory()
return memory.percent
# Get CPU utiliation
def get_cpu_usage():
cpu_percent = psutil.cpu_percent(interval=1)
return cpu_percent
# Function to update bot's status
async def update_status():
global gpu_on, last_start
while True:
mem_percent = get_memory_usage()
cpu_percent = get_cpu_usage()
# unused because discord won't display multiple lines of `state`
gpu_stats = get_gpu_stats()
vram_used_gb = gpu_stats.memory_used/1000
vram_total_gb = gpu_stats.memory_total/1000
if vram_used_gb > 5 or gpu_stats.utilization > 30:
if not gpu_on:
last_start = datetime.now()
name = "GPU go brrr 🚀"
status = discord.Status.online
gpu_on = True
else:
name = "dust collect on the fans"
status = discord.Status.do_not_disturb
gpu_on = False
details = f'''
GPU: {gpu_stats.utilization}% | {vram_used_gb:.1f}/{vram_total_gb:.1f} GB
'''
# Update bot's status with GPU and memory usage
await client.change_presence(
activity=discord.Activity(
type = discord.ActivityType.watching,
name = name,
state = details,
assets = {},
party = {},
start = last_start,
buttons = []
),
status=status
)
# Wait for 5 seconds before updating again
await asyncio.sleep(5)
# Event: Bot is ready
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
# Start updating bot's status
client.loop.create_task(update_status())
# Run the bot
client.run(TOKEN)