From 0b05407ea03629b83b314dab3dd4f7572563af66 Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Mon, 31 Aug 2020 20:25:45 +0200 Subject: [PATCH] configure verbose --- cogs/wynncraft/wynncraftcog.py | 36 +++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/cogs/wynncraft/wynncraftcog.py b/cogs/wynncraft/wynncraftcog.py index d4e0c7a..06f13df 100644 --- a/cogs/wynncraft/wynncraftcog.py +++ b/cogs/wynncraft/wynncraftcog.py @@ -14,7 +14,11 @@ class WynncraftCog(commands.Cog): "tracked": [], "channel": None } + default_global = { + "log": True + } self.config.register_guild(**default_guild) + self.config.register_global(**default_global) @commands.command() @checks.admin_or_permissions(manage_guild=True) @@ -51,20 +55,37 @@ class WynncraftCog(commands.Cog): await self.config.guild(ctx.guild).channel.set(i) await ctx.send("Les messages seront désormais envoyés dans %s" % channel.mention) + @commands.command() + @checks.is_owner() + async def log(self, ctx, state): + state = state.lower() + if state == "true" or state == "on": + await self.config.log.set(True) + await ctx.send(":white_check_mark:") + elif state == "false" or state == "off": + await self.config.log.set(False) + await ctx.send(":white_check_mark:") + else: + await ctx.send(":x:") + + def _log(self, s): + if await self.config.log(): + print(s) + async def loop(self): await self.bot.wait_until_ready() last_status = {} while self is self.bot.get_cog("WynncraftCog"): - print("Looping guilds") + self._log("Looping guilds") async for guild in self.bot.fetch_guilds(limit=150): guild_id = guild.id - print("Loop id %s" % guild_id) + self._log("Loop id %s" % guild_id) try: channel_id = await self.config.guild(guild).channel() if channel_id: - print("Channel set") + self._log("Channel set") for tracked in await self.config.guild(guild).tracked(): - print("calling API for player %s" % tracked) + self._log("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: @@ -80,11 +101,12 @@ class WynncraftCog(commands.Cog): 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"])) + self._log("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") + self._log("Guild has no channel set") except: - traceback.print_exc() + if await self.config.log(): + traceback.print_exc() await asyncio.sleep(30)