import asyncio from redbot.core import commands from redbot.core import Config, checks from discord import TextChannel import traceback import requests class WynncraftCog(commands.Cog): def __init__(self, bot): self.bot = bot self.config = Config.get_conf(self, identifier=48775419874) default_guild = { "tracked": [], "channel": None } self.config.register_guild(**default_guild) @commands.command() @checks.admin_or_permissions(manage_guild=True) async def track(self, ctx, username: str): tracked = await self.config.guild(ctx.guild).tracked() if username in tracked: await ctx.send(":x: **%s** est déjà tracké" % username) else: tracked.append(username) await self.config.guild(ctx.guild).tracked.set(tracked) await ctx.send(":white_check_mark: **%s** est désormais tracké" % username) @commands.command() @checks.admin_or_permissions(manage_guild=True) async def untrack(self, ctx, username: str): tracked = await self.config.guild(ctx.guild).tracked() if username in tracked: tracked.remove(username) await self.config.guild(ctx.guild).tracked.set(tracked) await ctx.send(":white_check_mark: **%s** ne sera plus tracké" % username) else: await ctx.send(":x: **%s** n'était pas tracké" % username) @commands.command() @checks.admin_or_permissions(manage_guild=True) async def tracked(self, ctx): tracked = await self.config.guild(ctx.guild).tracked() await ctx.send("Les joueurs trackés sont: %s" % ",".join(tracked)) @commands.command() @checks.admin_or_permissions(manage_guild=True) async def set_channel(self, ctx, channel: TextChannel): i = channel.id await self.config.guild(ctx.guild).channel.set(i) await ctx.send("Les messages seront désormais envoyés dans %s" % channel.mention) async def loop(self): await self.bot.wait_until_ready() last_status = {} while self is self.bot.get_cog("WynncraftCog"): print("Looping guilds") async for guild in self.bot.fetch_guilds(limit=150): guild_id = guild.id print("Loop id %s" % guild_id) try: channel_id = await self.config.guild(guild).channel() if channel_id: print("Channel set") for tracked in await self.config.guild(guild).tracked(): print("calling API for player %s" % tracked) r = requests.get("https://api.wynncraft.com/v2/player/%s/stats" % tracked).json() for cl in r["data"][0]["classes"]: if guild_id not in last_status: last_status[guild_id] = {} if tracked not in last_status[guild_id]: last_status[guild_id][tracked] = {} if cl["name"] not in last_status[guild_id][tracked]: last_status[guild_id][tracked][cl["name"]] = cl if cl["professions"]["combat"]["level"] > last_status[guild_id][tracked][cl["name"]]["professions"]["combat"]["level"]: channel = self.bot.get_channel(channel_id) await channel.send(":high_brightness: %s a level up au niveau %s! GG!" % (tracked, cl["professions"]["combat"]["level"])) else: print("Player %s had last level %s and current %s" % (tracked, last_status[guild_id][tracked][cl["name"]]["professions"]["combat"]["level"], cl["professions"]["combat"]["level"])) last_status[guild_id][tracked][cl["name"]] = cl else: print("Guild has no channel set") except: traceback.print_exc() await asyncio.sleep(30)