From cb03db8df4bf8b50945b36a4b0debcaaed1190a8 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 15 Feb 2022 10:37:41 +0100 Subject: [PATCH] Replace discord.py with nextcord (#66540) * Replace discord.py with nextcord * Typing tweak * Another pip check decrease :) --- .../components/discord/manifest.json | 2 +- homeassistant/components/discord/notify.py | 25 +++++++++++-------- requirements_all.txt | 6 ++--- script/pip_check | 2 +- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/discord/manifest.json b/homeassistant/components/discord/manifest.json index 40c176c5f82..02b31a3aa99 100644 --- a/homeassistant/components/discord/manifest.json +++ b/homeassistant/components/discord/manifest.json @@ -2,7 +2,7 @@ "domain": "discord", "name": "Discord", "documentation": "https://www.home-assistant.io/integrations/discord", - "requirements": ["discord.py==1.7.3"], + "requirements": ["nextcord==2.0.0a8"], "codeowners": [], "iot_class": "cloud_push", "loggers": ["discord"] diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index e8b084a01ab..41137e1a32c 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -1,8 +1,10 @@ """Discord platform for notify component.""" +from __future__ import annotations + import logging import os.path -import discord +import nextcord import voluptuous as vol from homeassistant.components.notify import ( @@ -48,8 +50,8 @@ class DiscordNotificationService(BaseNotificationService): async def async_send_message(self, message, **kwargs): """Login to Discord, send message to channel(s) and log out.""" - discord.VoiceClient.warn_nacl = False - discord_bot = discord.Client() + nextcord.VoiceClient.warn_nacl = False + discord_bot = nextcord.Client() images = None embedding = None @@ -59,13 +61,13 @@ class DiscordNotificationService(BaseNotificationService): data = kwargs.get(ATTR_DATA) or {} - embed = None + embeds: list[nextcord.Embed] = [] if ATTR_EMBED in data: embedding = data[ATTR_EMBED] fields = embedding.get(ATTR_EMBED_FIELDS) or [] if embedding: - embed = discord.Embed(**embedding) + embed = nextcord.Embed(**embedding) for field in fields: embed.add_field(**field) if ATTR_EMBED_FOOTER in embedding: @@ -74,11 +76,12 @@ class DiscordNotificationService(BaseNotificationService): embed.set_author(**embedding[ATTR_EMBED_AUTHOR]) if ATTR_EMBED_THUMBNAIL in embedding: embed.set_thumbnail(**embedding[ATTR_EMBED_THUMBNAIL]) + embeds.append(embed) if ATTR_IMAGES in data: images = [] - for image in data.get(ATTR_IMAGES): + for image in data.get(ATTR_IMAGES, []): image_exists = await self.hass.async_add_executor_job( self.file_exists, image ) @@ -95,15 +98,15 @@ class DiscordNotificationService(BaseNotificationService): channelid = int(channelid) try: channel = await discord_bot.fetch_channel(channelid) - except discord.NotFound: + except nextcord.NotFound: try: channel = await discord_bot.fetch_user(channelid) - except discord.NotFound: + except nextcord.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: + files = [nextcord.File(image) for image in images] if images else [] + await channel.send(message, files=files, embeds=embeds) + except (nextcord.HTTPException, nextcord.NotFound) as error: _LOGGER.warning("Communication error: %s", error) await discord_bot.close() diff --git a/requirements_all.txt b/requirements_all.txt index 29248f86cbc..b8a86347aa1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -565,9 +565,6 @@ directv==0.4.0 # homeassistant.components.discogs discogs_client==2.3.0 -# homeassistant.components.discord -discord.py==1.7.3 - # homeassistant.components.steamist discovery30303==0.2.1 @@ -1099,6 +1096,9 @@ nexia==0.9.13 # homeassistant.components.nextcloud nextcloudmonitor==1.1.0 +# homeassistant.components.discord +nextcord==2.0.0a8 + # homeassistant.components.niko_home_control niko-home-control==0.2.1 diff --git a/script/pip_check b/script/pip_check index c30a7382f27..af47f101fbb 100755 --- a/script/pip_check +++ b/script/pip_check @@ -3,7 +3,7 @@ PIP_CACHE=$1 # Number of existing dependency conflicts # Update if a PR resolve one! -DEPENDENCY_CONFLICTS=10 +DEPENDENCY_CONFLICTS=9 PIP_CHECK=$(pip check --cache-dir=$PIP_CACHE) LINE_COUNT=$(echo "$PIP_CHECK" | wc -l)