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 {}
|
data = kwargs.get(ATTR_DATA) or {}
|
||||||
|
|
||||||
|
embed = None
|
||||||
if ATTR_EMBED in data:
|
if ATTR_EMBED in data:
|
||||||
embedding = data[ATTR_EMBED]
|
embedding = data[ATTR_EMBED]
|
||||||
fields = embedding.get(ATTR_EMBED_FIELDS)
|
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:
|
if ATTR_IMAGES in data:
|
||||||
images = []
|
images = []
|
||||||
|
|
||||||
|
@ -76,43 +88,21 @@ class DiscordNotificationService(BaseNotificationService):
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Image not found: %s", image)
|
_LOGGER.warning("Image not found: %s", image)
|
||||||
|
|
||||||
# pylint: disable=unused-variable
|
await discord_bot.login(self.token)
|
||||||
@discord_bot.event
|
|
||||||
async def on_ready():
|
try:
|
||||||
"""Send the messages when the bot is ready."""
|
for channelid in kwargs[ATTR_TARGET]:
|
||||||
try:
|
channelid = int(channelid)
|
||||||
for channelid in kwargs[ATTR_TARGET]:
|
try:
|
||||||
channelid = int(channelid)
|
channel = discord_bot.fetch_channel(
|
||||||
channel = discord_bot.get_channel(
|
|
||||||
channelid
|
channelid
|
||||||
) or discord_bot.get_user(channelid)
|
) or discord_bot.fetch_user(channelid)
|
||||||
|
except discord.NotFound:
|
||||||
if channel is None:
|
_LOGGER.warning("Channel not found for ID: %s", channelid)
|
||||||
_LOGGER.warning("Channel not found for ID: %s", channelid)
|
continue
|
||||||
continue
|
# Must create new instances of File for each channel.
|
||||||
# Must create new instances of File for each channel.
|
files = [discord.File(image) for image in images] if images else None
|
||||||
files = None
|
await channel.send(message, files=files, embed=embed)
|
||||||
if images:
|
except (discord.HTTPException, discord.NotFound) as error:
|
||||||
files = []
|
_LOGGER.warning("Communication error: %s", error)
|
||||||
for image in images:
|
await discord_bot.close()
|
||||||
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)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue