Improve Discord notifier (#52523)
This commit is contained in:
parent
ce1eda9809
commit
fe0151491e
1 changed files with 29 additions and 39 deletions
|
@ -59,10 +59,22 @@ class DiscordNotificationService(BaseNotificationService):
|
|||
|
||||
data = kwargs.get(ATTR_DATA) or {}
|
||||
|
||||
embed = None
|
||||
if ATTR_EMBED in data:
|
||||
embedding = data[ATTR_EMBED]
|
||||
fields = embedding.get(ATTR_EMBED_FIELDS)
|
||||
|
||||
if embedding:
|
||||
embed = discord.Embed(**embedding)
|
||||
for field in fields:
|
||||
embed.add_field(**field)
|
||||
if ATTR_EMBED_FOOTER in embedding:
|
||||
embed.set_footer(**embedding[ATTR_EMBED_FOOTER])
|
||||
if ATTR_EMBED_AUTHOR in embedding:
|
||||
embed.set_author(**embedding[ATTR_EMBED_AUTHOR])
|
||||
if ATTR_EMBED_THUMBNAIL in embedding:
|
||||
embed.set_thumbnail(**embedding[ATTR_EMBED_THUMBNAIL])
|
||||
|
||||
if ATTR_IMAGES in data:
|
||||
images = []
|
||||
|
||||
|
@ -76,43 +88,21 @@ class DiscordNotificationService(BaseNotificationService):
|
|||
else:
|
||||
_LOGGER.warning("Image not found: %s", image)
|
||||
|
||||
# pylint: disable=unused-variable
|
||||
@discord_bot.event
|
||||
async def on_ready():
|
||||
"""Send the messages when the bot is ready."""
|
||||
try:
|
||||
for channelid in kwargs[ATTR_TARGET]:
|
||||
channelid = int(channelid)
|
||||
channel = discord_bot.get_channel(
|
||||
await discord_bot.login(self.token)
|
||||
|
||||
try:
|
||||
for channelid in kwargs[ATTR_TARGET]:
|
||||
channelid = int(channelid)
|
||||
try:
|
||||
channel = discord_bot.fetch_channel(
|
||||
channelid
|
||||
) or discord_bot.get_user(channelid)
|
||||
|
||||
if channel is None:
|
||||
_LOGGER.warning("Channel not found for ID: %s", channelid)
|
||||
continue
|
||||
# Must create new instances of File for each channel.
|
||||
files = None
|
||||
if images:
|
||||
files = []
|
||||
for image in images:
|
||||
files.append(discord.File(image))
|
||||
if embedding:
|
||||
embed = discord.Embed(**embedding)
|
||||
if fields:
|
||||
for field_num, field_name in enumerate(fields):
|
||||
embed.add_field(**fields[field_num])
|
||||
if ATTR_EMBED_FOOTER in embedding:
|
||||
embed.set_footer(**embedding[ATTR_EMBED_FOOTER])
|
||||
if ATTR_EMBED_AUTHOR in embedding:
|
||||
embed.set_author(**embedding[ATTR_EMBED_AUTHOR])
|
||||
if ATTR_EMBED_THUMBNAIL in embedding:
|
||||
embed.set_thumbnail(**embedding[ATTR_EMBED_THUMBNAIL])
|
||||
await channel.send(message, files=files, embed=embed)
|
||||
else:
|
||||
await channel.send(message, files=files)
|
||||
except (discord.errors.HTTPException, discord.errors.NotFound) as error:
|
||||
_LOGGER.warning("Communication error: %s", error)
|
||||
await discord_bot.close()
|
||||
|
||||
# Using reconnect=False prevents multiple ready events to be fired.
|
||||
await discord_bot.start(self.token, reconnect=False)
|
||||
) or discord_bot.fetch_user(channelid)
|
||||
except discord.NotFound:
|
||||
_LOGGER.warning("Channel not found for ID: %s", channelid)
|
||||
continue
|
||||
# Must create new instances of File for each channel.
|
||||
files = [discord.File(image) for image in images] if images else None
|
||||
await channel.send(message, files=files, embed=embed)
|
||||
except (discord.HTTPException, discord.NotFound) as error:
|
||||
_LOGGER.warning("Communication error: %s", error)
|
||||
await discord_bot.close()
|
||||
|
|
Loading…
Add table
Reference in a new issue