diff --git a/rndstatus/rndstatus.py b/rndstatus/rndstatus.py index dac922b..aaab660 100644 --- a/rndstatus/rndstatus.py +++ b/rndstatus/rndstatus.py @@ -9,7 +9,7 @@ import asyncio import logging -log = logging.getLogger("red.aikaterna-cogs.rndstatus") +log = logging.getLogger("red.aikaterna.rndstatus") class RndStatus(commands.Cog): @@ -33,16 +33,10 @@ class RndStatus(commands.Cog): default_global = { "botstats": False, "delay": 300, - "statuses": [ - "her Turn()", - "Tomb Raider II", - "Transistor", - "NEO Scavenger", - "Python", - "with your heart.", - ], + "statuses": ["her Turn()", "Tomb Raider II", "Transistor", "NEO Scavenger", "Python", "with your heart.",], "streamer": "rndstatusstreamer", - "type": 1, + "type": 0, + "status": 0, } self.config.register_global(**default_global) @@ -81,15 +75,18 @@ class RndStatus(commands.Cog): Shows current list if empty.""" saved_status = await self.config.statuses() if statuses == () or "" in statuses: - return await ctx.send("Current statuses: " + " | ".join(saved_status)) + msg = ( + f"Current statuses: {(' | ').join(saved_status)}\n" + f"To set new statuses, use the instructions in `{ctx.prefix}help rndstatus set`." + ) + return await ctx.send(msg) await self.config.statuses.set(list(statuses)) + await self.presence_updater() await ctx.send("Done. Redo this command with no parameters to see the current list of statuses.") @rndstatus.command(name="streamer") async def _streamer(self, ctx: commands.Context, *, streamer=None): - """Set the streamer needed for streaming statuses. - """ - + """Set the streamer name needed for streaming statuses.""" saved_streamer = await self.config.streamer() if streamer is None: return await ctx.send(f"Current Streamer: {saved_streamer}") @@ -102,8 +99,7 @@ class RndStatus(commands.Cog): botstats = await self.config.botstats() await self.config.botstats.set(not botstats) await ctx.send(f"Botstats toggle: {not botstats}.") - if botstats is not False: - await self.bot.change_presence(activity=None) + await self.presence_updater() @rndstatus.command() async def delay(self, ctx, seconds: int): @@ -114,71 +110,107 @@ class RndStatus(commands.Cog): await self.config.delay.set(seconds) await ctx.send(f"Interval set to {seconds} seconds.") - @rndstatus.command() - async def type(self, ctx, type: int): - """Define the rndstatus type. + @rndstatus.command(name="type") + async def _rndstatus_type(self, ctx, status_type: int): + """Define the rndstatus game type. Type list: 0 = Playing 1 = Streaming 2 = Listening 3 = Watching""" - if 0 <= type <= 3: - await self.config.type.set(type) - await ctx.send("Rndstatus type set.") + if 0 <= status_type <= 3: + rnd_type = {0: "playing", 1: "streaming", 2: "listening", 3: "watching"} + await self.config.type.set(status_type) + await self.presence_updater() + await ctx.send(f"Rndstatus activity type set to {rnd_type[status_type]}.") else: - await ctx.send("Type must be between 0 and 3.") + await ctx.send( + f"Status activity type must be between 0 and 3. " + f"See `{ctx.prefix}help rndstatus type` for more information." + ) + + @rndstatus.command() + async def status(self, ctx, status: int): + """Define the rndstatus presence status. + + Status list: + 0 = Online + 1 = Idle + 2 = DND + 3 = Invisible""" + if 0 <= status <= 3: + rnd_status = {0: "online", 1: "idle", 2: "DND", 3: "invisible"} + await self.config.status.set(status) + await self.presence_updater() + await ctx.send(f"Rndstatus presence status set to {rnd_status[status]}.") + else: + await ctx.send( + f"Status presence type must be between 0 and 3. " + f"See `{ctx.prefix}help rndstatus status` for more information." + ) async def maybe_update_presence(self): - await self.bot.wait_until_ready() - pattern = re.compile(rf"<@!?{self.bot.user.id}>") - delay = 0 + await self.bot.wait_until_red_ready() + delay = await self.config.delay() while True: try: - cog_settings = await self.config.all() - guilds = self.bot.guilds - guild = next(g for g in guilds if not g.unavailable) - try: - current_game = str(guild.me.activity.name) - except AttributeError: - current_game = None - statuses = cog_settings["statuses"] - botstats = cog_settings["botstats"] - streamer = cog_settings["streamer"] - _type = cog_settings["type"] - delay = cog_settings["delay"] - - url = f"https://www.twitch.tv/{streamer}" - prefix = await self.bot.get_valid_prefixes() - - if botstats: - me = self.bot.user - clean_prefix = pattern.sub(f"@{me.name}", prefix[0]) - total_users = self._user_count - servers = str(len(self.bot.guilds)) - botstatus = f"{clean_prefix}help | {total_users} users | {servers} servers" - if (current_game != str(botstatus)) or current_game is None: - if _type == 1: - await self.bot.change_presence(activity=discord.Streaming(name=botstatus, url=url)) - else: - await self.bot.change_presence(activity=discord.Activity(name=botstatus, type=_type)) - else: - if len(statuses) > 0: - new_status = self.random_status(guild, statuses) - if current_game != new_status: - if (current_game != new_status) or current_game is None: - if _type == 1: - await self.bot.change_presence(activity=discord.Streaming(name=new_status, url=url)) - else: - await self.bot.change_presence( - activity=discord.Activity(name=new_status, type=_type) - ) + await self.presence_updater() + await asyncio.sleep(int(delay)) except asyncio.CancelledError: break except Exception as e: log.exception(e, exc_info=e) - await asyncio.sleep(int(delay)) - + + async def presence_updater(self): + pattern = re.compile(rf"<@!?{self.bot.user.id}>") + cog_settings = await self.config.all() + guilds = self.bot.guilds + guild = next(g for g in guilds if not g.unavailable) + try: + current_game = str(guild.me.activity.name) + except AttributeError: + current_game = None + statuses = cog_settings["statuses"] + botstats = cog_settings["botstats"] + streamer = cog_settings["streamer"] + _type = cog_settings["type"] + _status = cog_settings["status"] + + url = f"https://www.twitch.tv/{streamer}" + prefix = await self.bot.get_valid_prefixes() + + if _status == 0: + status = discord.Status.online + elif _status == 1: + status = discord.Status.idle + elif _status == 2: + status = discord.Status.dnd + elif _status == 3: + status = discord.Status.offline + + if botstats: + me = self.bot.user + clean_prefix = pattern.sub(f"@{me.name}", prefix[0]) + total_users = self._user_count + servers = str(len(self.bot.guilds)) + botstatus = f"{clean_prefix}help | {total_users} users | {servers} servers" + if (current_game != str(botstatus)) or current_game is None: + if _type == 1: + await self.bot.change_presence(activity=discord.Streaming(name=botstatus, url=url)) + else: + await self.bot.change_presence(activity=discord.Activity(name=botstatus, type=_type), status=status) + else: + if len(statuses) > 0: + new_status = self.random_status(guild, statuses) + if current_game != new_status: + if (current_game != new_status) or current_game is None: + if _type == 1: + await self.bot.change_presence(activity=discord.Streaming(name=new_status, url=url)) + else: + await self.bot.change_presence( + activity=discord.Activity(name=new_status, type=_type), status=status + ) def random_status(self, guild, statuses): try: