import discord from redbot.core import commands, Config import aiohttp from datetime import datetime class PlayitStatus(commands.Cog): """Monitor playit.gg status and alert when it goes down.""" def __init__(self, bot): self.bot = bot self.config = Config.get_conf(self, identifier=1234567890) self.config.register_global(channel_id=None, last_status={}) self.session = None self.api_url = "https://status.playit.gg/api/getMonitorList/AxwIw5PSiW" async def red_delete_data_for_user(self, **kwargs): return async def init_session(self): if self.session is None or self.session.closed: self.session = aiohttp.ClientSession() async def fetch_status(self): await self.init_session() try: async with self.session.get(self.api_url) as resp: if resp.status == 200: data = await resp.json() return data.get("data", []) except Exception: pass return [] async def send_status(self, channel, monitors, changed_only=False): last_status = await self.config.last_status() changed = False for monitor in monitors: name = monitor.get("name", "Unknown") status_class = monitor.get("statusClass", "unknown") is_up = status_class == "success" was_up = last_status.get(name, True) if changed_only and is_up == was_up: continue changed = True if not is_up and was_up: embed = discord.Embed( title="playit.gg DOWN", description=f"**{name}** is now **DOWN**", color=discord.Color.red(), timestamp=datetime.utcnow(), ) elif is_up and not was_up: embed = discord.Embed( title="playit.gg UP", description=f"**{name}** is back **UP**", color=discord.Color.green(), timestamp=datetime.utcnow(), ) else: continue embed.set_footer(text="playit.gg Status Monitor") await channel.send(embed=embed) last_status[name] = is_up await self.config.last_status.set(last_status) return changed @commands.group() @commands.admin_or_permissions(manage_guild=True) async def playit(self, ctx): """playit.gg status monitor.""" pass @playit.command() async def channel(self, ctx, channel_id: int): """Set the channel ID for status updates.""" channel = self.bot.get_channel(channel_id) if not channel: await ctx.send("Channel not found.") return await self.config.channel_id.set(channel_id) monitors = await self.fetch_status() if monitors: await self.send_status(channel, monitors) await ctx.send(f"Monitoring set to {channel.mention}. Status posted.") else: await ctx.send("Channel set but failed to fetch status.") @playit.command() async def status(self, ctx): """Check playit.gg status now.""" channel_id = await self.config.channel_id() if not channel_id: await ctx.send("No channel set. Use `[p]playit channel ` first.") return channel = self.bot.get_channel(channel_id) if not channel: await ctx.send("Channel not found.") return monitors = await self.fetch_status() if not monitors: await ctx.send("Failed to fetch status.") return changed = await self.send_status(channel, monitors, changed_only=True) if not changed: await ctx.send("No changes. All services stable.")