Upgrade discord.py to v1.0.1 (#23523)

This commit is contained in:
Cyro 2019-04-30 22:12:39 +02:00 committed by Fabian Affolter
parent 603e2cd961
commit 75a2c057f2
3 changed files with 34 additions and 11 deletions

View File

@ -3,7 +3,7 @@
"name": "Discord", "name": "Discord",
"documentation": "https://www.home-assistant.io/components/discord", "documentation": "https://www.home-assistant.io/components/discord",
"requirements": [ "requirements": [
"discord.py==0.16.12" "discord.py==1.0.1"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [] "codeowners": []

View File

@ -39,30 +39,53 @@ class DiscordNotificationService(BaseNotificationService):
discord.VoiceClient.warn_nacl = False discord.VoiceClient.warn_nacl = False
discord_bot = discord.Client(loop=self.hass.loop) discord_bot = discord.Client(loop=self.hass.loop)
images = None
if ATTR_TARGET not in kwargs: if ATTR_TARGET not in kwargs:
_LOGGER.error("No target specified") _LOGGER.error("No target specified")
return None return None
if ATTR_DATA in kwargs:
data = kwargs.get(ATTR_DATA)
if ATTR_IMAGES in data:
import os.path
images = list()
for image in data.get(ATTR_IMAGES):
if os.path.isfile(image):
images.append(image)
else:
_LOGGER.warning("Image not found: %s", image)
# pylint: disable=unused-variable # pylint: disable=unused-variable
@discord_bot.event @discord_bot.event
async def on_ready(): async def on_ready():
"""Send the messages when the bot is ready.""" """Send the messages when the bot is ready."""
try: try:
data = kwargs.get(ATTR_DATA)
images = None
if data:
images = data.get(ATTR_IMAGES)
for channelid in kwargs[ATTR_TARGET]: for channelid in kwargs[ATTR_TARGET]:
channel = discord.Object(id=channelid) channelid = int(channelid)
await discord_bot.send_message(channel, message) channel = discord_bot.get_channel(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: if images:
for anum, f_name in enumerate(images): files = list()
await discord_bot.send_file(channel, f_name) for image in images:
files.append(discord.File(image))
await channel.send(message, files=files)
except (discord.errors.HTTPException, except (discord.errors.HTTPException,
discord.errors.NotFound) as error: discord.errors.NotFound) as error:
_LOGGER.warning("Communication error: %s", error) _LOGGER.warning("Communication error: %s", error)
await discord_bot.logout() await discord_bot.logout()
await discord_bot.close() await discord_bot.close()
await discord_bot.start(self.token) # Using reconnect=False prevents multiple ready events to be fired.
await discord_bot.start(self.token, reconnect=False)

View File

@ -344,7 +344,7 @@ directpy==0.5
discogs_client==2.2.1 discogs_client==2.2.1
# homeassistant.components.discord # homeassistant.components.discord
discord.py==0.16.12 discord.py==1.0.1
# homeassistant.components.updater # homeassistant.components.updater
distro==1.4.0 distro==1.4.0