[Streams] Toggle mentions on for roles being alerted (#2252)

Resolves #1831.
This commit is contained in:
palmtree5 2018-11-23 14:46:38 -09:00 committed by Toby Harradine
parent 6435f6b882
commit 7546c50226

View File

@ -28,7 +28,7 @@ from . import streamtypes as _streamtypes
from collections import defaultdict from collections import defaultdict
import asyncio import asyncio
import re import re
from typing import Optional, List from typing import Optional, List, Tuple
CHECK_DELAY = 60 CHECK_DELAY = 60
@ -397,9 +397,6 @@ class Streams(commands.Cog):
async def role(self, ctx: commands.Context, *, role: discord.Role): async def role(self, ctx: commands.Context, *, role: discord.Role):
"""Toggle a role mention.""" """Toggle a role mention."""
current_setting = await self.db.role(role).mention() current_setting = await self.db.role(role).mention()
if not role.mentionable:
await ctx.send("That role is not mentionable!")
return
if current_setting: if current_setting:
await self.db.role(role).mention.set(False) await self.db.role(role).mention.set(False)
await ctx.send( await ctx.send(
@ -409,11 +406,17 @@ class Streams(commands.Cog):
) )
else: else:
await self.db.role(role).mention.set(True) await self.db.role(role).mention.set(True)
await ctx.send( msg = _(
_( "When a stream or community is live, `@\u200b{role.name}` will be mentioned."
"When a stream or community is live, `@\u200b{role.name}` will be mentioned." ).format(role=role)
).format(role=role) if not role.mentionable:
) msg += " " + _(
"Since the role is not mentionable, it will be momentarily made mentionable "
"when announcing a streamalert. Please make sure I have the correct "
"permissions to manage this role, or else members of this role won't receive "
"a notification."
)
await ctx.send(msg)
@streamset.command() @streamset.command()
@commands.guild_only() @commands.guild_only()
@ -536,7 +539,7 @@ class Streams(commands.Cog):
continue continue
for channel_id in stream.channels: for channel_id in stream.channels:
channel = self.bot.get_channel(channel_id) channel = self.bot.get_channel(channel_id)
mention_str = await self._get_mention_str(channel.guild) mention_str, edited_roles = await self._get_mention_str(channel.guild)
if mention_str: if mention_str:
content = _("{mention}, {stream.name} is live!").format( content = _("{mention}, {stream.name} is live!").format(
@ -547,19 +550,35 @@ class Streams(commands.Cog):
m = await channel.send(content, embed=embed) m = await channel.send(content, embed=embed)
stream._messages_cache.append(m) stream._messages_cache.append(m)
if edited_roles:
for role in edited_roles:
await role.edit(mentionable=False)
await self.save_streams() await self.save_streams()
async def _get_mention_str(self, guild: discord.Guild): async def _get_mention_str(self, guild: discord.Guild) -> Tuple[str, List[discord.Role]]:
"""Returns a 2-tuple with the string containing the mentions, and a list of
all roles which need to have their `mentionable` property set back to False.
"""
settings = self.db.guild(guild) settings = self.db.guild(guild)
mentions = [] mentions = []
edited_roles = []
if await settings.mention_everyone(): if await settings.mention_everyone():
mentions.append("@everyone") mentions.append("@everyone")
if await settings.mention_here(): if await settings.mention_here():
mentions.append("@here") mentions.append("@here")
can_manage_roles = guild.me.guild_permissions.manage_roles
for role in guild.roles: for role in guild.roles:
if await self.db.role(role).mention(): if await self.db.role(role).mention():
if can_manage_roles and not role.mentionable:
try:
await role.edit(mentionable=True)
except discord.Forbidden:
# Might still be unable to edit role based on hierarchy
pass
else:
edited_roles.append(role)
mentions.append(role.mention) mentions.append(role.mention)
return " ".join(mentions) return " ".join(mentions), edited_roles
async def check_communities(self): async def check_communities(self):
for community in self.communities: for community in self.communities:
@ -590,12 +609,15 @@ class Streams(commands.Cog):
emb = await community.make_embed(streams) emb = await community.make_embed(streams)
chn_msg = [m for m in community._messages_cache if m.channel == chn] chn_msg = [m for m in community._messages_cache if m.channel == chn]
if not chn_msg: if not chn_msg:
mentions = await self._get_mention_str(chn.guild) mentions, roles = await self._get_mention_str(chn.guild)
if mentions: if mentions:
msg = await chn.send(mentions, embed=emb) msg = await chn.send(mentions, embed=emb)
else: else:
msg = await chn.send(embed=emb) msg = await chn.send(embed=emb)
community._messages_cache.append(msg) community._messages_cache.append(msg)
if roles:
for role in roles:
await role.edit(mentionable=False)
await self.save_communities() await self.save_communities()
else: else:
chn_msg = sorted(chn_msg, key=lambda x: x.created_at, reverse=True)[0] chn_msg = sorted(chn_msg, key=lambda x: x.created_at, reverse=True)[0]