[V3 Streams] Fix preceding comma in streamalert messages (#1224)

* Refactor getting mentions for streamalerts

* Add missing await

Fixes #1192
This commit is contained in:
Tobotimus 2018-01-01 18:24:36 +11:00 committed by GitHub
parent 0a705071c2
commit d83f14d1b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -416,48 +416,30 @@ class Streams:
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_everyone = await self.db.guild(channel.guild).mention_everyone() mention_str = await self._get_mention_str(channel.guild)
mention_here = await self.db.guild(channel.guild).mention_here()
mention_roles = [] if mention_str:
for r in channel.guild.roles: content = "{}, {} is online!".format(mention_str, stream.name)
to_append = {
"role": r,
"enabled": await self.db.role(r).mention()
}
mention_roles.append(to_append)
mention = None
if mention_everyone or mention_here or any(mention_roles):
mention = True
if mention:
mention_str = ""
if mention_everyone:
mention_str += "@everyone "
if mention_here:
mention_str += "@here "
if any(mention_roles):
mention_str += " ".join(
[
r["role"].mention for r in mention_roles
if r["role"].mentionable and r["enabled"]
]
)
mention_str = mention_str.strip()
try:
m = await channel.send(
"{}, {} is online!".format(
mention_str, stream.name
), embed=embed
)
stream._messages_cache.append(m)
except:
pass
else: else:
try: content = "{} is online!".format(stream.name)
m = await channel.send("%s is online!" % stream.name,
embed=embed) try:
stream._messages_cache.append(m) m = await channel.send(content, embed=embed)
except: stream._messages_cache.append(m)
pass except:
pass
async def _get_mention_str(self, guild: discord.Guild):
settings = self.db.guild(guild)
mentions = []
if await settings.mention_everyone():
mentions.append('@everyone')
if await settings.mention_here():
mentions.append('@here')
for role in guild.roles:
if await self.db.role(role).mention():
mentions.append(role.mention)
return ' '.join(mentions)
async def check_communities(self): async def check_communities(self):
for community in self.communities: for community in self.communities: