diff --git a/main.py b/main.py index f98767f..fdc00bb 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,12 @@ import discord -import requests import dotenv +import asyncio from discord import app_commands import psutil import time from other.ikavoidping import get_api_ping from other.almteshdotnetping import get_api_ping as get_almtesh_ping +from other.snartfrping import get_api_ping as get_snartfr_ping dotenv.load_dotenv() TOKEN = dotenv.get_key(".env", "DISCORD_TOKEN") @@ -16,6 +17,7 @@ class Bot(discord.Client): self.startup_timestamp = time.time() async def setup_hook(self): + await asyncio.sleep(1) await self.tree.sync() bot = Bot() @@ -25,20 +27,27 @@ async def on_ready(): @bot.tree.command(name="status", description="Get the bot's current status") async def status(interaction: discord.Interaction): - cpu_usage = psutil.cpu_percent(interval=1, percpu=True) + cpu_usage = await asyncio.to_thread(psutil.cpu_percent, interval=1, percpu=True) avg_cpu_usage = sum(cpu_usage) / len(cpu_usage) memory_info = psutil.virtual_memory() used_memory = memory_info.used total_memory = memory_info.total discord_latency = bot.latency uptime = time.time() - bot.startup_timestamp + server_uptime = psutil.boot_time() + ikavoid_ping, almtesh_ping, snartfr_ping = await asyncio.gather( + get_api_ping(), get_almtesh_ping(), get_snartfr_ping() + ) await interaction.response.send_message( f"CPU Usage: {avg_cpu_usage:.2f}%\n" f"Per-Core CPU Usage: {', '.join(f'{usage}%' for usage in cpu_usage)}\n" f"Memory Usage: {memory_info.percent}% ({used_memory / (1024**2):.2f} MB / {total_memory / (1024**2):.2f} MB)\n" f"Bot Latency: {discord_latency*1000:.2f} ms\n" f"Uptime: {int(uptime // 86400)}d {int((uptime % 86400) // 3600)}h {int((uptime % 3600) // 60)}m {int(uptime % 60)}s\n" - f"API Ping: {await get_api_ping():.2f} ms" + f"ikavoid.fr API Ping: {ikavoid_ping:.2f} ms\n" + f"almtesh.net Ping: {almtesh_ping:.2f} ms\n" + f"snart.fr Ping: {snartfr_ping:.2f} ms\n" + f"Server Uptime: {int((time.time() - server_uptime) // 86400)}d {int(((time.time() - server_uptime) % 86400) // 3600)}h {int(((time.time() - server_uptime) % 3600) // 60)}m {int((time.time() - server_uptime) % 60)}s" ) diff --git a/other/__pycache__/almteshdotnetping.cpython-313.pyc b/other/__pycache__/almteshdotnetping.cpython-313.pyc new file mode 100644 index 0000000..3642436 Binary files /dev/null and b/other/__pycache__/almteshdotnetping.cpython-313.pyc differ diff --git a/other/__pycache__/ikavoidping.cpython-313.pyc b/other/__pycache__/ikavoidping.cpython-313.pyc index ed5616d..b8a9304 100644 Binary files a/other/__pycache__/ikavoidping.cpython-313.pyc and b/other/__pycache__/ikavoidping.cpython-313.pyc differ diff --git a/other/__pycache__/snartfrping.cpython-313.pyc b/other/__pycache__/snartfrping.cpython-313.pyc new file mode 100644 index 0000000..558a53a Binary files /dev/null and b/other/__pycache__/snartfrping.cpython-313.pyc differ diff --git a/other/almteshdotnetping b/other/almteshdotnetping deleted file mode 100644 index 305106c..0000000 --- a/other/almteshdotnetping +++ /dev/null @@ -1,11 +0,0 @@ -import requests -import time - -url = "https://almtesh.net/" - -async def get_api_ping(): - start_time = time.time() - requests.get(url) - end_time = time.time() - ping = (end_time - start_time) * 1000 - return ping \ No newline at end of file diff --git a/other/almteshdotnetping.py b/other/almteshdotnetping.py new file mode 100644 index 0000000..07f002e --- /dev/null +++ b/other/almteshdotnetping.py @@ -0,0 +1,14 @@ +import aiohttp +import time + +url = "https://almtesh.net/" + +async def get_api_ping(): + start_time = time.time() + timeout = aiohttp.ClientTimeout(total=10) + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.get(url): + pass + end_time = time.time() + ping = (end_time - start_time) * 1000 + return ping \ No newline at end of file diff --git a/other/ikavoidping.py b/other/ikavoidping.py index ad1f5dc..b20fd47 100644 --- a/other/ikavoidping.py +++ b/other/ikavoidping.py @@ -1,11 +1,14 @@ -import requests +import aiohttp import time url = "https://api.ikavoid.fr/" async def get_api_ping(): start_time = time.time() - requests.get(url) + timeout = aiohttp.ClientTimeout(total=10) + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.get(url): + pass end_time = time.time() - ping = (end_time - start_time) * 1000 + ping = (end_time - start_time) * 1000 return ping \ No newline at end of file diff --git a/other/snartfrping.py b/other/snartfrping.py new file mode 100644 index 0000000..ff5f2fb --- /dev/null +++ b/other/snartfrping.py @@ -0,0 +1,14 @@ +import aiohttp +import time + +url = "https://snart.fr/" + +async def get_api_ping(): + start_time = time.time() + timeout = aiohttp.ClientTimeout(total=10) + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.get(url): + pass + end_time = time.time() + ping = (end_time - start_time) * 1000 + return ping \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5b23e73..1a276d8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ discord.py -requests +aiohttp psutil \ No newline at end of file