feat(speedtest): added the cog

This commit is contained in:
Seaswimmer 2024-05-25 17:09:07 -04:00
parent ee9b62db5b
commit ef591224cd
Signed by untrusted user: cswimr
GPG key ID: 5D671B5D03D65A7F
3 changed files with 70 additions and 0 deletions

5
speedtest/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from .speedtest import Speedtest
async def setup(bot):
await bot.add_cog(Speedtest(bot))

12
speedtest/info.json Normal file
View file

@ -0,0 +1,12 @@
{
"author" : ["SeaswimmerTheFsh (seasw.)"],
"install_msg" : "Thank you for installing SeaUtils!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).",
"name" : "SeaUtils",
"short" : "A collection of useful utilities.",
"description" : "A collection of useful utilities.",
"end_user_data_statement" : "This cog does not store end user data.",
"hidden": true,
"disabled": false,
"min_bot_version": "3.5.0",
"min_python_version": [3, 8, 0]
}

53
speedtest/speedtest.py Normal file
View file

@ -0,0 +1,53 @@
# _____ _
# / ____| (_)
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
import json
import subprocess
import discord
from redbot.core import commands
from redbot.core.bot import Red
from redbot.core.utils import chat_formatting as cf
class Speedtest(commands.Cog):
"""A collection of random utilities."""
__author__ = ["SeaswimmerTheFsh"]
__version__ = "1.0.0"
def __init__(self, bot: Red):
self.bot = bot
def format_help_for_context(self, ctx: commands.Context) -> str:
pre_processed = super().format_help_for_context(ctx) or ""
n = "\n" if "\n\n" not in pre_processed else ""
text = [
f"{pre_processed}{n}",
f"Cog Version: **{self.__version__}**",
f"Author: {cf.humanize_list(self.__author__)}"
]
return "\n".join(text)
@commands.command()
@commands.is_owner()
async def speedtest(self, ctx: commands.Context):
"""Run a speedtest."""
msg = await ctx.maybe_send_embed("Running speedtest...")
result = subprocess.run(["speedtest", "-f json", "--accept-license"], stdout=subprocess.PIPE)
r = json.loads(result.stdout.decode("utf-8"))
if await ctx.embed_requested():
embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color())
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")
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")
embed.set_image(url=r["result"]["image"] + ".png")
await msg.edit(embed=embed)
else:
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']}")