Skip to content

Commit

Permalink
ban is now more in sync with mute, merged trackerstatus files into one
Browse files Browse the repository at this point in the history
  • Loading branch information
Snaacky committed Dec 24, 2024
1 parent 33e8d4c commit 9b4710e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 196 deletions.
23 changes: 8 additions & 15 deletions chiya/cogs/ban.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
import arrow

import discord
from discord import app_commands
Expand Down Expand Up @@ -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(
Expand All @@ -72,25 +72,18 @@ 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},
],
)

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,
Expand Down Expand Up @@ -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(),
)

Expand Down Expand Up @@ -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",
)
Expand Down
1 change: 0 additions & 1 deletion chiya/cogs/mute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
178 changes: 177 additions & 1 deletion chiya/cogs/trackerstatus.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
Loading

0 comments on commit 9b4710e

Please sign in to comment.