From 7df5595190135d81a37d81166998c2a9a39ad0a2 Mon Sep 17 00:00:00 2001 From: Puncher <65789180+Puncher1@users.noreply.github.com> Date: Sat, 18 Mar 2023 00:00:54 +0100 Subject: [PATCH 1/5] Add support for widget image --- discord/enums.py | 9 +++++++++ discord/guild.py | 32 ++++++++++++++++++++++++++++++++ docs/api.rst | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/discord/enums.py b/discord/enums.py index 94ca8c726589..be69fb0269d8 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -68,6 +68,7 @@ 'AutoModRuleActionType', 'ForumLayoutType', 'ForumOrderType', + 'WidgetStyle', ) if TYPE_CHECKING: @@ -757,6 +758,14 @@ class ForumOrderType(Enum): creation_date = 1 +class WidgetStyle(Enum): + shield = 'shield' + banner_medium_powered_by = "banner1" + banner_small = "banner2" + banner_medium_chat_now = "banner3" + banner_large_logo = "banner4" + + def create_unknown_value(cls: Type[E], val: Any) -> E: value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below name = f'unknown_{val}' diff --git a/discord/guild.py b/discord/guild.py index 0f91eebe1c45..b9f35c8729e3 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -75,6 +75,7 @@ AutoModRuleEventType, ForumOrderType, ForumLayoutType, + WidgetStyle, ) from .mixins import Hashable from .user import User @@ -93,6 +94,7 @@ from .welcome_screen import WelcomeScreen, WelcomeChannel from .automod import AutoModRule, AutoModTrigger, AutoModRuleAction from .partial_emoji import _EmojiTag, PartialEmoji +from .http import Route __all__ = ( @@ -3931,6 +3933,36 @@ async def widget(self) -> Widget: return Widget(state=self._state, data=data) + def widget_image_url(self, style: WidgetStyle = WidgetStyle.shield) -> Optional[str]: + """ + + Returns the widget image url of the guild. + + .. note:: + + The guild must have the widget enabled to get this information. + + Parameters + ----------- + style: :class:`WidgetStyle` + The style which should be applied for the widget. + Default to :attr:`~WidgetStyle.shield`. Returns ``None`` if widget is not enabled. + + Raises + ------- + HTTPException + Retrieving the widget image url failed. + + Returns + -------- + Optional[:class:`str`] + The widget image url in the given style. + """ + if not self.widget_enabled: + return None + + return f"{Route.BASE}/guilds/{self.id}/widget.png?style={style.value}" + async def edit_widget( self, *, diff --git a/docs/api.rst b/docs/api.rst index 316dbda14c48..62c2df26a9d9 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3325,6 +3325,43 @@ of :class:`enum.Enum`. Sort forum posts by creation time (from most recent to oldest). +.. class:: WidgetStyle + + Represents how a widget image is styled. + + .. versionadded:: 2.3 + + .. attribute:: shield + + Shield style widget with Discord icon and guild members online count. + + `Example `_ + + .. attribute:: banner_medium_powered_by + + Large image with guild icon, name and online count. "POWERED BY DISCORD" as the footer of the widget. + + `Example `_ + + .. attribute:: banner_small + + Smaller widget style with guild icon, name and online count. Split on the right with Discord logo. + + `Example `_ + + .. attribute:: banner_medium_chat_now + + Large image with guild icon, name and online count. In the footer, Discord logo on the left and "Chat Now" on the right. + + `Example `_ + + .. attribute:: banner_large_logo + + Large Discord logo at the top of the widget. Guild icon, name and online count in the middle portion of the widget and a "JOIN MY SERVER" button at the bottom + + `Example `_ + + .. _discord-api-audit-logs: Audit Log Data From f01087333a48d257a351b1da03f1909391434040 Mon Sep 17 00:00:00 2001 From: Puncher <65789180+Puncher1@users.noreply.github.com> Date: Sat, 18 Mar 2023 00:09:51 +0100 Subject: [PATCH 2/5] Removed links --- docs/api.rst | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 62c2df26a9d9..9920b5c1b2ed 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3335,32 +3335,22 @@ of :class:`enum.Enum`. Shield style widget with Discord icon and guild members online count. - `Example `_ - .. attribute:: banner_medium_powered_by Large image with guild icon, name and online count. "POWERED BY DISCORD" as the footer of the widget. - `Example `_ - .. attribute:: banner_small Smaller widget style with guild icon, name and online count. Split on the right with Discord logo. - `Example `_ - .. attribute:: banner_medium_chat_now Large image with guild icon, name and online count. In the footer, Discord logo on the left and "Chat Now" on the right. - `Example `_ - .. attribute:: banner_large_logo Large Discord logo at the top of the widget. Guild icon, name and online count in the middle portion of the widget and a "JOIN MY SERVER" button at the bottom - `Example `_ - .. _discord-api-audit-logs: From 241ffa2a4fbb2e5c93d6087c0c4670fdde0bb785 Mon Sep 17 00:00:00 2001 From: Puncher <65789180+Puncher1@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:11:40 +0100 Subject: [PATCH 3/5] Removed enum WidgetStyle --- discord/enums.py | 9 --------- discord/guild.py | 11 ++++++----- docs/api.rst | 27 --------------------------- 3 files changed, 6 insertions(+), 41 deletions(-) diff --git a/discord/enums.py b/discord/enums.py index be69fb0269d8..94ca8c726589 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -68,7 +68,6 @@ 'AutoModRuleActionType', 'ForumLayoutType', 'ForumOrderType', - 'WidgetStyle', ) if TYPE_CHECKING: @@ -758,14 +757,6 @@ class ForumOrderType(Enum): creation_date = 1 -class WidgetStyle(Enum): - shield = 'shield' - banner_medium_powered_by = "banner1" - banner_small = "banner2" - banner_medium_chat_now = "banner3" - banner_large_logo = "banner4" - - def create_unknown_value(cls: Type[E], val: Any) -> E: value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below name = f'unknown_{val}' diff --git a/discord/guild.py b/discord/guild.py index b9f35c8729e3..bbe0d287deb1 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -75,7 +75,6 @@ AutoModRuleEventType, ForumOrderType, ForumLayoutType, - WidgetStyle, ) from .mixins import Hashable from .user import User @@ -3933,7 +3932,9 @@ async def widget(self) -> Widget: return Widget(state=self._state, data=data) - def widget_image_url(self, style: WidgetStyle = WidgetStyle.shield) -> Optional[str]: + WidgetStyle = Literal['shield', 'banner1', 'banner2', 'banner3', 'banner4'] + + def widget_image_url(self, style: WidgetStyle = 'shield') -> Optional[str]: """ Returns the widget image url of the guild. @@ -3944,9 +3945,9 @@ def widget_image_url(self, style: WidgetStyle = WidgetStyle.shield) -> Optional[ Parameters ----------- - style: :class:`WidgetStyle` + style: :class:`str` The style which should be applied for the widget. - Default to :attr:`~WidgetStyle.shield`. Returns ``None`` if widget is not enabled. + Default to ``shield``. Returns ``None`` if widget is not enabled. Raises ------- @@ -3961,7 +3962,7 @@ def widget_image_url(self, style: WidgetStyle = WidgetStyle.shield) -> Optional[ if not self.widget_enabled: return None - return f"{Route.BASE}/guilds/{self.id}/widget.png?style={style.value}" + return f"{Route.BASE}/guilds/{self.id}/widget.png?style={style}" async def edit_widget( self, diff --git a/docs/api.rst b/docs/api.rst index 9920b5c1b2ed..316dbda14c48 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3325,33 +3325,6 @@ of :class:`enum.Enum`. Sort forum posts by creation time (from most recent to oldest). -.. class:: WidgetStyle - - Represents how a widget image is styled. - - .. versionadded:: 2.3 - - .. attribute:: shield - - Shield style widget with Discord icon and guild members online count. - - .. attribute:: banner_medium_powered_by - - Large image with guild icon, name and online count. "POWERED BY DISCORD" as the footer of the widget. - - .. attribute:: banner_small - - Smaller widget style with guild icon, name and online count. Split on the right with Discord logo. - - .. attribute:: banner_medium_chat_now - - Large image with guild icon, name and online count. In the footer, Discord logo on the left and "Chat Now" on the right. - - .. attribute:: banner_large_logo - - Large Discord logo at the top of the widget. Guild icon, name and online count in the middle portion of the widget and a "JOIN MY SERVER" button at the bottom - - .. _discord-api-audit-logs: Audit Log Data From 5c409ec34761b8eb98670f668b433e2ce37c9190 Mon Sep 17 00:00:00 2001 From: Puncher <65789180+Puncher1@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:50:17 +0100 Subject: [PATCH 4/5] Moved WidgetStyle out of namespace --- discord/guild.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index bbe0d287deb1..c6b38742d14c 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -138,6 +138,7 @@ VocalGuildChannel = Union[VoiceChannel, StageChannel] GuildChannel = Union[VocalGuildChannel, ForumChannel, TextChannel, CategoryChannel] ByCategoryItem = Tuple[Optional[CategoryChannel], List[GuildChannel]] + WidgetStyle = Literal['shield', 'banner1', 'banner2', 'banner3', 'banner4'] class BanEntry(NamedTuple): @@ -3932,8 +3933,6 @@ async def widget(self) -> Widget: return Widget(state=self._state, data=data) - WidgetStyle = Literal['shield', 'banner1', 'banner2', 'banner3', 'banner4'] - def widget_image_url(self, style: WidgetStyle = 'shield') -> Optional[str]: """ From a27959edc9a7f2386c05105c154b98a51b46acbd Mon Sep 17 00:00:00 2001 From: Andrin S <65789180+Puncher1@users.noreply.github.com> Date: Wed, 29 Mar 2023 06:15:32 +0200 Subject: [PATCH 5/5] [docs] Remove errors --- discord/guild.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index c6b38742d14c..23e5bc03b046 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -3948,11 +3948,6 @@ def widget_image_url(self, style: WidgetStyle = 'shield') -> Optional[str]: The style which should be applied for the widget. Default to ``shield``. Returns ``None`` if widget is not enabled. - Raises - ------- - HTTPException - Retrieving the widget image url failed. - Returns -------- Optional[:class:`str`]