Add Speedtest cog #28
1 changed files with 29 additions and 14 deletions
|
@ -5,6 +5,7 @@
|
||||||
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
||||||
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
@ -33,21 +34,35 @@ class Speedtest(commands.Cog):
|
||||||
]
|
]
|
||||||
return "\n".join(text)
|
return "\n".join(text)
|
||||||
|
|
||||||
|
async def run_speedtest(self) -> str | asyncio.Any:
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
"speedtest", "-f json", "--accept-license",
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE
|
||||||
|
)
|
||||||
|
stdout, stderr = await process.communicate()
|
||||||
|
if process.returncode != 0:
|
||||||
|
return stderr.decode("utf-8")
|
||||||
|
return json.loads(stdout.decode("utf-8"))
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def speedtest(self, ctx: commands.Context):
|
async def speedtest(self, ctx: commands.Context) -> None:
|
||||||
"""Run a speedtest."""
|
"""Run a speedtest."""
|
||||||
msg = await ctx.maybe_send_embed("Running speedtest...")
|
msg = await ctx.maybe_send_embed("Running speedtest...")
|
||||||
result = subprocess.run(["speedtest", "-f json", "--accept-license"], stdout=subprocess.PIPE)
|
async with ctx.typing():
|
||||||
r = json.loads(result.stdout.decode("utf-8"))
|
r = await self.run_speedtest()
|
||||||
if await ctx.embed_requested():
|
if isinstance(r, str):
|
||||||
embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color())
|
await msg.edit(content=f"An error occurred: {r}")
|
||||||
embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up")
|
return
|
||||||
embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms")
|
if await ctx.embed_requested():
|
||||||
embed.set_image(url=r["result"]["image"] + ".png")
|
embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color())
|
||||||
await msg.edit(embed=embed)
|
embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up")
|
||||||
else:
|
embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms")
|
||||||
await msg.edit(content=f"**Speedtest Results**\n"
|
embed.set_image(url=r["result"]["image"] + ".png")
|
||||||
f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n"
|
await msg.edit(embed=embed)
|
||||||
f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n"
|
else:
|
||||||
f"**Results**: {r['result']['url']}")
|
await msg.edit(content=f"**Speedtest Results**\n"
|
||||||
|
f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n"
|
||||||
|
f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n"
|
||||||
|
f"**Results**: {r['result']['url']}")
|
||||||
|
|
Loading…
Reference in a new issue