From 59a22ce65c67b28f235ba98ff4c2d728d24471ff Mon Sep 17 00:00:00 2001 From: TrustyJAID Date: Thu, 18 Oct 2018 14:17:15 -0600 Subject: [PATCH 1/2] [Away] Add multiple away response toggles based off discord status. --- away/away.py | 204 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 166 insertions(+), 38 deletions(-) diff --git a/away/away.py b/away/away.py index 9bd5c4c..65c469a 100644 --- a/away/away.py +++ b/away/away.py @@ -8,7 +8,8 @@ class Away(BaseCog): """Le away cog""" default_global_settings = {"ign_servers": []} - default_user_settings = {"MESSAGE": False} + default_user_settings = {"MESSAGE": False, "IDLE_MESSAGE": False, + "DND_MESSAGE": False, "OFFLINE_MESSAGE": False} def __init__(self, bot): self.bot = bot @@ -17,65 +18,192 @@ class Away(BaseCog): self._away.register_global(**self.default_global_settings) self._away.register_user(**self.default_user_settings) + async def make_embed_message(self, author, message, state=None): + """ + Makes the embed reply + """ + avatar = author.avatar_url_as() # This will return default avatar if no avatar is present + color = author.color + + if state == "away": + em = discord.Embed(description=message, color=color) + em.set_author( + name="{} is currently away".format(author.display_name), + icon_url=avatar + ) + elif state == "idle": + em = discord.Embed(description=message, color=color) + em.set_author( + name="{} is currently idle".format(author.display_name), + icon_url=avatar + ) + elif state == "dnd": + em = discord.Embed(description=message, color=color) + em.set_author( + name="{} is currently do not disturb".format(author.display_name), + icon_url=avatar + ) + elif state == "offline": + em = discord.Embed(description=message, color=color) + em.set_author( + name="{} is currently offline".format(author.display_name), + icon_url=avatar + ) + else: + em = discord.Embed(color=color) + em.set_author( + name="{} is currently away".format(author.display_name), + icon_url=avatar + ) + return em + + async def find_user_mention(self, message): + """ + Replaces user mentions with their username + """ + for word in message.split(): + if word.startswith("<@") and word.endswith(">"): + mention = word.replace("<@", "").replace(">", "").replace("!", "") + user = await self.bot.get_user_info(int(mention)) + message = message.replace(word, "@" + user.name) + return message + + async def make_text_message(self, author, message, state=None): + """ + Makes the message to display if embeds aren't available + """ + message = await self.find_user_mention(message) + if state == "away": + msg = "{} is currently away and has set the following message: `{}`".format( + author.display_name, message + ) + elif state == "idle": + msg = "{} is currently away and has set the following message: `{}`".format( + author.display_name, message + ) + elif state == "dnd": + msg = "{} is currently away and has set the following message: `{}`".format( + author.display_name, message + ) + elif state == "offline": + msg = "{} is currently away and has set the following message: `{}`".format( + author.display_name, message + ) + else: + msg = "{} is currently away".format(author.display_name) + return msg + async def on_message(self, message): tmp = {} guild = message.guild list_of_guilds_ign = await self._away.ign_servers() if not guild: return + if not message.channel.permissions_for(guild.me).send_messages: + return if guild.id not in list_of_guilds_ign: for mention in message.mentions: tmp[mention] = True - if message.author.id != guild.me.id: - for author in tmp: - test = await self._away.user(author).MESSAGE() - if test: - try: - avatar = ( - author.avatar_url if author.avatar else author.default_avatar_url - ) + for author in tmp: + away_msg = await self._away.user(author).MESSAGE() + if away_msg: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, away_msg, "away") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, away_msg, "away") + await message.channel.send(msg) + idle_msg = await self._away.user(author).IDLE_MESSAGE() + if idle_msg and author.status == discord.Status.idle: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, idle_msg, "idle") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, idle_msg, "idle") + await message.channel.send(msg) + dnd_msg = await self._away.user(author).DND_MESSAGE() + if dnd_msg and author.status == discord.Status.dnd: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, dnd_msg, "dnd") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, dnd_msg, "dnd") + await message.channel.send(msg) + offline_msg = await self._away.user(author).OFFLINE_MESSAGE() + if offline_msg and author.status == discord.Status.offline: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, offline_msg, "offline") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, offline_msg, "offline") + await message.channel.send(msg) - if test: - em = discord.Embed(description=test, color=discord.Color.blue()) - em.set_author( - name="{} is currently away".format(author.display_name), - icon_url=avatar, - ) - else: - em = discord.Embed(color=discord.Color.blue()) - em.set_author( - name="{} is currently away".format(author.display_name), - icon_url=avatar, - ) - await message.channel.send(embed=em) - except: - if test: - msg = "{} is currently away and has set the following message: `{}`".format( - author.display_name, test - ) - else: - msg = "{} is currently away".format(author.display_name) - await message.channel.send(msg) @commands.command(name="away") - async def away_(self, ctx, *message): + async def away_(self, ctx, *, message:str=None): """Tell the bot you're away or back.""" author = ctx.message.author + print(message) mess = await self._away.user(author).MESSAGE() if mess: - await self._away.user(author).clear() + await self._away.user(author).MESSAGE.set(False) msg = "You're now back." else: - length = len(str(message)) - if length < 256 and length > 2: - await self._away.user(author).MESSAGE.set( - " ".join(ctx.message.clean_content.split()[1:]) - ) - else: + if message is None: await self._away.user(author).MESSAGE.set(" ") + else: + await self._away.user(author).MESSAGE.set(message) msg = "You're now set as away." await ctx.send(msg) + @commands.command(name="idle") + async def idle_(self, ctx, *, message:str=None): + """Set an automatic reply when you're idle""" + author = ctx.message.author + mess = await self._away.user(author).IDLE_MESSAGE() + if mess: + await self._away.user(author).IDLE_MESSAGE.set(False) + msg = "The bot will no longer reply for you when you're idle." + else: + if message is None: + await self._away.user(author).IDLE_MESSAGE.set(" ") + else: + await self._away.user(author).IDLE_MESSAGE.set(message) + msg = "The bot will now reply for you when you're idle." + await ctx.send(msg) + + @commands.command(name="offline") + async def offline_(self, ctx, *, message:str=None): + """Set an automatic reply when you're offline""" + author = ctx.message.author + mess = await self._away.user(author).OFFLINE_MESSAGE() + if mess: + await self._away.user(author).OFFLINE_MESSAGE.set(False) + msg = "The bot will no longer reply for you when you're offline." + else: + if message is None: + await self._away.user(author).OFFLINE_MESSAGE.set(" ") + else: + await self._away.user(author).OFFLINE_MESSAGE.set(message) + msg = "The bot will now reply for you when you're offline." + await ctx.send(msg) + + @commands.command(name="dnd", aliases=["donotdisturb"]) + async def donotdisturb_(self, ctx, *, message:str=None): + """Set an automatic reply when you're dnd""" + author = ctx.message.author + mess = await self._away.user(author).DND_MESSAGE() + if mess: + await self._away.user(author).DND_MESSAGE.set(False) + msg = "The bot will no longer reply for you when you're set to do not disturb." + else: + if message is None: + await self._away.user(author).DND_MESSAGE.set(" ") + else: + await self._away.user(author).DND_MESSAGE.set(message) + msg = "The bot will now reply for you when you're set to do not disturb." + await ctx.send(msg) + @commands.command(name="toggleaway") @checks.admin_or_permissions(administrator=True) async def _ignore(self, ctx): From 81055f38cd7b1d91ee4a8b6682f1dd8ddd10fc65 Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Sat, 27 Oct 2018 12:08:22 -0700 Subject: [PATCH 2/2] Only reply with status messages if no away message set --- away/away.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/away/away.py b/away/away.py index 65c469a..81bae19 100644 --- a/away/away.py +++ b/away/away.py @@ -113,31 +113,31 @@ class Away(BaseCog): else: msg = await self.make_text_message(author, away_msg, "away") await message.channel.send(msg) - idle_msg = await self._away.user(author).IDLE_MESSAGE() - if idle_msg and author.status == discord.Status.idle: - if message.channel.permissions_for(guild.me).embed_links: - em = await self.make_embed_message(author, idle_msg, "idle") - await message.channel.send(embed=em) - else: - msg = await self.make_text_message(author, idle_msg, "idle") - await message.channel.send(msg) - dnd_msg = await self._away.user(author).DND_MESSAGE() - if dnd_msg and author.status == discord.Status.dnd: - if message.channel.permissions_for(guild.me).embed_links: - em = await self.make_embed_message(author, dnd_msg, "dnd") - await message.channel.send(embed=em) - else: - msg = await self.make_text_message(author, dnd_msg, "dnd") - await message.channel.send(msg) - offline_msg = await self._away.user(author).OFFLINE_MESSAGE() - if offline_msg and author.status == discord.Status.offline: - if message.channel.permissions_for(guild.me).embed_links: - em = await self.make_embed_message(author, offline_msg, "offline") - await message.channel.send(embed=em) - else: - msg = await self.make_text_message(author, offline_msg, "offline") - await message.channel.send(msg) - + else: + idle_msg = await self._away.user(author).IDLE_MESSAGE() + if idle_msg and author.status == discord.Status.idle: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, idle_msg, "idle") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, idle_msg, "idle") + await message.channel.send(msg) + dnd_msg = await self._away.user(author).DND_MESSAGE() + if dnd_msg and author.status == discord.Status.dnd: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, dnd_msg, "dnd") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, dnd_msg, "dnd") + await message.channel.send(msg) + offline_msg = await self._away.user(author).OFFLINE_MESSAGE() + if offline_msg and author.status == discord.Status.offline: + if message.channel.permissions_for(guild.me).embed_links: + em = await self.make_embed_message(author, offline_msg, "offline") + await message.channel.send(embed=em) + else: + msg = await self.make_text_message(author, offline_msg, "offline") + await message.channel.send(msg) @commands.command(name="away") async def away_(self, ctx, *, message:str=None):