[V3 Away] Discord Status based responses added

One improvement in the future would be respecting Red's embed toggle for making embeds instead of using permissions, but it's not something that would stop me from merging this. Thanks to TrustyJAID for spending time on adding this feature.
This commit is contained in:
aikaterna
2018-10-27 12:10:08 -07:00
committed by GitHub

View File

@@ -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
)
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,
)
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)
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)
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)
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):
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):