jarvis-bot/jarvis/utils/__init__.py

63 lines
1.5 KiB
Python

from discord.ext import commands
from discord import Embed, Color
from pkgutil import iter_modules
import jarvis.cogs
import git
def convert_bytesize(bytes: int) -> str:
bytes = float(bytes)
sizes = ["B", "KB", "MB", "GB"]
size = 0
while bytes >= 1024 and size < len(sizes) - 1:
bytes = bytes / 1024
size += 1
return f"{bytes:0.2f}{sizes[size]}"
def get_prefix(bot, message):
prefixes = ["$", ">", "?", "!"]
if not message.guild:
return "?"
return commands.when_mentioned_or(*prefixes)(bot, message)
def get_extensions(path=jarvis.cogs.__path__) -> list:
return ["jarvis.cogs.{}".format(x.name) for x in iter_modules(path)]
def parse_color_hex(hex: str) -> Color:
hex = hex.lstrip("#")
rgb = tuple(int(hex[i : i + 2], 16) for i in (0, 2, 4)) # noqa: E203
return Color.from_rgb(*rgb)
def build_embed(
title: str, desc: str, fields: list, color: str = "#FF0000", **kwargs
) -> Embed:
embed = Embed(
title=title, desc=desc, color=parse_color_hex(color), **kwargs
)
for field in fields:
embed.add_field(**field.to_dict())
return embed
def update():
repo = git.Repo(".")
dirty = repo.is_dirty()
current_hash = repo.head.object.hexsha
origin = repo.remotes.origin
origin.fetch()
if current_hash != origin.refs["main"].object.hexsha:
if dirty:
return 2
origin.pull()
return 0
return 1
def get_repo_hash():
repo = git.Repo(".")
return repo.head.object.hexsha