From 9b4710e976e1c31f20f084ea2b0677f48ce09627 Mon Sep 17 00:00:00 2001 From: Snaacky Date: Tue, 24 Dec 2024 06:13:33 -0500 Subject: [PATCH] ban is now more in sync with mute, merged trackerstatus files into one --- chiya/cogs/ban.py | 23 ++--- chiya/cogs/mute.py | 1 - chiya/cogs/trackerstatus.py | 178 +++++++++++++++++++++++++++++++++- chiya/utils/trackerstatus.py | 179 ----------------------------------- 4 files changed, 185 insertions(+), 196 deletions(-) delete mode 100644 chiya/utils/trackerstatus.py diff --git a/chiya/cogs/ban.py b/chiya/cogs/ban.py index 2d4c6c30..d9e98cad 100644 --- a/chiya/cogs/ban.py +++ b/chiya/cogs/ban.py @@ -1,4 +1,4 @@ -import time +import arrow import discord from discord import app_commands @@ -61,8 +61,8 @@ async def ban( author=True, title=f"Banning user: {user}", description=f"{user.mention} was banned by {ctx.user.mention} for: {reason}", - thumbnail_url="https://i.imgur.com/l0jyxkz.png", - color=discord.Color.red(), + thumbnail_url="https://files.catbox.moe/6hd0uw.png", + color=0xCD6D6D, ) user_embed = embeds.make_embed( @@ -72,10 +72,10 @@ async def ban( "You can submit a ban appeal on our subreddit [here]" "(https://www.reddit.com/message/compose/?to=/r/snackbox)." ), - image_url="https://i.imgur.com/CglQwK5.gif", + image_url="https://files.catbox.moe/jp1wmf.gif", color=discord.Color.blurple(), fields=[ - {"name": "Server:", "value": f"[{ctx.guild.name}]({await ctx.guild.vanity_invite()})", "inline": True}, + {"name": "Server:", "value": ctx.guild.name, "inline": True}, {"name": "Reason:", "value": reason, "inline": False}, ], ) @@ -83,14 +83,7 @@ async def ban( try: await user.send(embed=user_embed) except (discord.Forbidden, discord.HTTPException): - mod_embed.add_field( - name="Notice:", - value=( - f"Unable to message {user.mention} about this action. " - "This can be caused by the user not being in the server, " - "having DMs disabled, or having the bot blocked." - ), - ) + mod_embed.set_footer(text="⚠️ Unable to message user about this action.") ModLog( user_id=user.id, @@ -130,7 +123,7 @@ async def unban(self, ctx: discord.Interaction, user: discord.Member | discord.U author=True, title=f"Unbanning user: {user}", description=f"{user.mention} was unbanned by {ctx.user.mention} for: {reason}", - thumbnail_url="https://i.imgur.com/4H0IYJH.png", + thumbnail_url="https://files.catbox.moe/qhc82k.png", color=discord.Color.green(), ) @@ -158,7 +151,7 @@ async def on_member_ban(self, guild: discord.Guild, user: discord.Member | disco ModLog( user_id=user.id, mod_log=logs[0].user.id, - timestamp=int(time.time()), + timestamp=arrow.utcnow().int_timestamp, reason=ban_entry.reason, type="ban", ) diff --git a/chiya/cogs/mute.py b/chiya/cogs/mute.py index 5e609d76..1a71bd02 100644 --- a/chiya/cogs/mute.py +++ b/chiya/cogs/mute.py @@ -2,7 +2,6 @@ import discord from discord import app_commands from discord.ext import commands -from loguru import logger from parsedatetime import Calendar from chiya.config import config diff --git a/chiya/cogs/trackerstatus.py b/chiya/cogs/trackerstatus.py index 4e8942da..1e310c2a 100644 --- a/chiya/cogs/trackerstatus.py +++ b/chiya/cogs/trackerstatus.py @@ -1,11 +1,187 @@ +import time import aiohttp import discord from discord import app_commands from discord.ext import commands, tasks +from loguru import logger from chiya.config import config +from chiya.utils import embeds from chiya.utils.embeds import error_embed -from chiya.utils.trackerstatus import TrackerStatus, TrackerStatusAB, TrackerStatusInfo, TrackerStatusMAM + + +class TrackerStatus: + def __init__(self, tracker: str, url: str) -> None: + self.tracker = tracker + self.cache_data: dict = None + self.url = url + + def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: + pass + + async def do_refresh(self, session: aiohttp.ClientSession) -> None: + try: + async with session.get(self.url, timeout=10) as response: + response.raise_for_status() + self.cache_data = await response.json() + except Exception: + logger.debug(f"Unable to refresh {self.tracker} tracker status") + pass + + def get_embed_color(self, embed: discord.Embed): + status = list(set([field.value for field in embed.fields])) + if len(status) == 1: + if status[0] == "🟢 Online": + return discord.Color.green() + elif status[0] == "🟠 Unstable": + return discord.Color.orange() + elif status[0] == "🔴 Offline": + return discord.Color.red() + else: + if "🟢 Online" not in status: + return discord.Color.red() + else: + return discord.Color.orange() + + return discord.Color.red() + + +class TrackerStatusInfo(TrackerStatus): + """ + Gets status of a tracker from trackerstatus.info + """ + + last_update = 0 + global_data: dict = None + + def __init__(self, tracker: str) -> None: + super().__init__(tracker, "https://trackerstatus.info/api/list/") + + async def do_refresh(self, session: aiohttp.ClientSession) -> None: + if time.time() - self.last_update > 10: + await super().do_refresh(session) + self.global_data = self.cache_data + self.last_update = time.time() + + def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: + embed = embeds.make_embed( + ctx=ctx, + title=f"Tracker Status: {self.tracker}", + ) + + if self.global_data is None: + self.last_update = 0 + self.do_refresh() + + for key, value in self.global_data[self.tracker.lower()]["Details"].items(): + # Skip over any keys that we don't want to return in the embed. + if key in ["tweet", "TrackerHTTPAddresses", "TrackerHTTPSAddresses"]: + continue + embed.add_field(name=key, value=self.normalize_value(value), inline=True) + + embed.color = self.get_embed_color(embed) + + return embed + + +def normalize_value(self, value): + """ + Converts API data values into user-friendly text with status availability icon. + """ + match value: + case "1": + return "🟢 Online" + case "2": + return "🟠 Unstable" + case "0": + return "🔴 Offline" + + +class TrackerStatusAB(TrackerStatus): + """ + Gets status of AB from API + """ + + def __init__(self) -> None: + super().__init__("AB", "https://status.animebytes.tv/api/status") + + def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: + embed = embeds.make_embed( + ctx=ctx, + title=f"Tracker Status: {self.tracker}", + ) + + if self.cache_data is None: + self.do_refresh() + + if not self.cache_data.get("status", False): + embed.set_footer("🔴 API Failed") + + for key, value in self.cache_data.get("status", {}).items(): + embed.add_field(name=key, value=self.normalize_value(value.get("status")), inline=True) + + embed.color = self.get_embed_color(embed) + + return embed + + def normalize_value(self, value): + """ + Converts API data values into user-friendly text with status availability icon. + """ + match value: + case 1: + return "🟢 Online" + case 2: + return "🟠 Unstable" + case 0: + return "🔴 Offline" + + +class TrackerStatusUptimeRobot(TrackerStatus): + """ + Gets status of a tracker from trackerstatus.info + """ + + def __init__(self, tracker: str, url: str) -> None: + super().__init__(tracker, url) + + def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: + embed = embeds.make_embed( + ctx=ctx, + title=f"Tracker Status: {self.tracker}", + ) + + if self.cache_data is None: + self.do_refresh() + + monitors: list[dict] = self.cache_data.get("psp", {}).get("monitors", []) + + for monitor in monitors: + dratio: dict = monitor.get("dailyRatios", [])[0] + embed.add_field(name=monitor.get("name", "UNKNOWN"), value=self.normalize_value(dratio), inline=True) + + embed.color = self.get_embed_color(embed) + + return embed + + def normalize_value(self, value: dict): + """ + Converts API data values into user-friendly text with status availability icon. + """ + if value.get("label") == "success": + return "🟢 Online" + ratio = float(value.get("ratio", "0")) + if float(value.get("ratio")) > 95: + return "🟠 Unstable" + elif ratio > 0: + return "🔴 Offline" + return "🔴 Unknown" + + +class TrackerStatusMAM(TrackerStatusUptimeRobot): + def __init__(self) -> None: + super().__init__("MAM", "https://status.myanonamouse.net/api/getMonitorList/vl59BTEJX") + trackers: list[TrackerStatus] = [ TrackerStatusInfo("AR"), diff --git a/chiya/utils/trackerstatus.py b/chiya/utils/trackerstatus.py deleted file mode 100644 index 57b6ed7f..00000000 --- a/chiya/utils/trackerstatus.py +++ /dev/null @@ -1,179 +0,0 @@ -import time - -import aiohttp -import discord -from loguru import logger - -from chiya.utils import embeds - - -class TrackerStatus: - def __init__(self, tracker: str, url: str) -> None: - self.tracker = tracker - self.cache_data: dict = None - self.url = url - - def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: - pass - - async def do_refresh(self, session: aiohttp.ClientSession) -> None: - try: - async with session.get(self.url, timeout=10) as response: - response.raise_for_status() - self.cache_data = await response.json() - except Exception: - logger.debug(f"Unable to refresh {self.tracker} tracker status") - pass - - def get_embed_color(self, embed: discord.Embed): - status = list(set([field.value for field in embed.fields])) - if len(status) == 1: - if status[0] == "🟢 Online": - return discord.Color.green() - elif status[0] == "🟠 Unstable": - return discord.Color.orange() - elif status[0] == "🔴 Offline": - return discord.Color.red() - else: - if "🟢 Online" not in status: - return discord.Color.red() - else: - return discord.Color.orange() - - return discord.Color.red() - - -class TrackerStatusInfo(TrackerStatus): - """ - Gets status of a tracker from trackerstatus.info - """ - - last_update = 0 - global_data: dict = None - - def __init__(self, tracker: str) -> None: - super().__init__(tracker, "https://trackerstatus.info/api/list/") - - async def do_refresh(self, session: aiohttp.ClientSession) -> None: - if time.time() - self.last_update > 10: - await super().do_refresh(session) - self.global_data = self.cache_data - self.last_update = time.time() - - def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: - embed = embeds.make_embed( - ctx=ctx, - title=f"Tracker Status: {self.tracker}", - ) - - if self.global_data is None: - self.last_update = 0 - self.do_refresh() - - for key, value in self.global_data[self.tracker.lower()]["Details"].items(): - # Skip over any keys that we don't want to return in the embed. - if key in ["tweet", "TrackerHTTPAddresses", "TrackerHTTPSAddresses"]: - continue - embed.add_field(name=key, value=self.normalize_value(value), inline=True) - - embed.color = self.get_embed_color(embed) - - return embed - - def normalize_value(self, value): - """ - Converts API data values into user-friendly text with status availability icon. - """ - match value: - case "1": - return "🟢 Online" - case "2": - return "🟠 Unstable" - case "0": - return "🔴 Offline" - - -class TrackerStatusAB(TrackerStatus): - """ - Gets status of AB from API - """ - - def __init__(self) -> None: - super().__init__("AB", "https://status.animebytes.tv/api/status") - - def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: - embed = embeds.make_embed( - ctx=ctx, - title=f"Tracker Status: {self.tracker}", - ) - - if self.cache_data is None: - self.do_refresh() - - if not self.cache_data.get("status", False): - embed.set_footer("🔴 API Failed") - - for key, value in self.cache_data.get("status", {}).items(): - embed.add_field(name=key, value=self.normalize_value(value.get("status")), inline=True) - - embed.color = self.get_embed_color(embed) - - return embed - - def normalize_value(self, value): - """ - Converts API data values into user-friendly text with status availability icon. - """ - match value: - case 1: - return "🟢 Online" - case 2: - return "🟠 Unstable" - case 0: - return "🔴 Offline" - - -class TrackerStatusUptimeRobot(TrackerStatus): - """ - Gets status of a tracker from trackerstatus.info - """ - - def __init__(self, tracker: str, url: str) -> None: - super().__init__(tracker, url) - - def get_status_embed(self, ctx: discord.Interaction = None) -> discord.Embed: - embed = embeds.make_embed( - ctx=ctx, - title=f"Tracker Status: {self.tracker}", - ) - - if self.cache_data is None: - self.do_refresh() - - monitors: list[dict] = self.cache_data.get("psp", {}).get("monitors", []) - - for monitor in monitors: - dratio: dict = monitor.get("dailyRatios", [])[0] - embed.add_field(name=monitor.get("name", "UNKNOWN"), value=self.normalize_value(dratio), inline=True) - - embed.color = self.get_embed_color(embed) - - return embed - - def normalize_value(self, value: dict): - """ - Converts API data values into user-friendly text with status availability icon. - """ - if value.get("label") == "success": - return "🟢 Online" - ratio = float(value.get("ratio", "0")) - if float(value.get("ratio")) > 95: - return "🟠 Unstable" - elif ratio > 0: - return "🔴 Offline" - return "🔴 Unknown" - - -class TrackerStatusMAM(TrackerStatusUptimeRobot): - def __init__(self) -> None: - super().__init__("MAM", "https://status.myanonamouse.net/api/getMonitorList/vl59BTEJX")