From d98b46816d41b52aa89012381b81da3c776dcde4 Mon Sep 17 00:00:00 2001 From: Puncher <65789180+Puncher1@users.noreply.github.com> Date: Thu, 16 Mar 2023 03:53:31 +0100 Subject: [PATCH] Add widget_channel and widget_enabled parameters in Guild.edit --- discord/channel.py | 4 ++-- discord/guild.py | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/discord/channel.py b/discord/channel.py index c276f54d2..70f7c14a6 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -165,7 +165,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): default_thread_slowmode_delay: :class:`int` The default slowmode delay in seconds for threads created in this channel. - .. versionadded:: 2.3 + .. versionadded:: 2.0 """ __slots__ = ( @@ -368,7 +368,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): default_thread_slowmode_delay: :class:`int` The new default slowmode delay in seconds for threads created in this channel. - .. versionadded:: 2.3 + .. versionadded:: 2.0 Raises ------ ValueError diff --git a/discord/guild.py b/discord/guild.py index 452c306f1..66db5358d 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -304,6 +304,7 @@ class Guild(Hashable): 'owner_application_id', 'command_counts', 'widget_enabled', + '_widget_channel_id', '_members', '_channels', '_icon', @@ -315,7 +316,6 @@ class Guild(Hashable): '_splash', '_voice_states', '_afk_channel_id', - '_widget_channel_id', '_system_channel_id', '_system_channel_flags', '_discovery_splash', @@ -525,13 +525,13 @@ class Guild(Hashable): self.premium_subscription_count: int = guild.get('premium_subscription_count') or 0 self.vanity_url_code: Optional[str] = guild.get('vanity_url_code') self.widget_enabled: bool = guild.get('widget_enabled', False) + self._widget_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'widget_channel_id') self._system_channel_flags: int = guild.get('system_channel_flags', 0) self.preferred_locale: Locale = try_enum(Locale, guild.get('preferred_locale', 'en-US')) self._discovery_splash: Optional[str] = guild.get('discovery_splash') self._rules_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'rules_channel_id') self._public_updates_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'public_updates_channel_id') self._afk_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'afk_channel_id') - self._widget_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'widget_channel_id') self.nsfw_level: NSFWLevel = try_enum(NSFWLevel, guild.get('nsfw_level', 0)) self.mfa_level: MFALevel = try_enum(MFALevel, guild.get('mfa_level', 0)) self.approximate_presence_count: Optional[int] = guild.get('approximate_presence_count') @@ -801,7 +801,7 @@ class Guild(Hashable): def get_emoji(self, emoji_id: int, /) -> Optional[Emoji]: """Returns an emoji with the given ID. - .. versionadded:: 2.3 + .. versionadded:: 2.0 Parameters ---------- @@ -867,11 +867,13 @@ class Guild(Hashable): return channel_id and self._channels.get(channel_id) # type: ignore @property - def widget_channel(self) -> Optional[GuildChannel]: - """Optional[:class:`TextChannel`]: Returns the channel the - widget will generate an invite to by default. + def widget_channel(self) -> Optional[Union[TextChannel, ForumChannel, VoiceChannel, StageChannel]]: + """Optional[Union[:class:`TextChannel`, :class:`ForumChannel`, :class:`VoiceChannel`, :class:`StageChannel`]]: Returns + the widget channel of the guild. If no channel is set, then this returns ``None``. + + .. versionadded:: 2.0 """ channel_id = self._widget_channel_id return channel_id and self._channels.get(channel_id) # type: ignore @@ -1366,7 +1368,7 @@ class Guild(Hashable): default_thread_slowmode_delay: :class:`int` The default slowmode delay in seconds for threads created in the text channel. - .. versionadded:: 2.3 + .. versionadded:: 2.0 reason: Optional[:class:`str`] The reason for creating this channel. Shows up on the audit log. @@ -1859,6 +1861,8 @@ class Guild(Hashable): premium_progress_bar_enabled: bool = MISSING, discoverable: bool = MISSING, invites_disabled: bool = MISSING, + widget_enabled: bool = MISSING, + widget_channel: Optional[Snowflake] = MISSING, ) -> Guild: r"""|coro| @@ -1891,6 +1895,9 @@ class Guild(Hashable): .. versionchanged:: 2.0 The ``discoverable`` and ``invites_disabled`` keyword parameters were added. + .. versionchanged:: 2.0 + The ``widget_enabled`` and ``widget_channel`` keyword parameters were added. + Parameters ---------- name: :class:`str` @@ -1954,6 +1961,10 @@ class Guild(Hashable): Whether server discovery is enabled for this guild. invites_disabled: :class:`bool` Whether joining via invites should be disabled for the guild. + widget_enabled: :class:`bool` + Whether to enable the widget for the guild. + widget_channel: Optional[:class:`abc.Snowflake`] + The new widget channel. ``None`` removes the widget channel. reason: Optional[:class:`str`] The reason for editing this guild. Shows up on the audit log. @@ -2105,6 +2116,15 @@ class Guild(Hashable): if premium_progress_bar_enabled is not MISSING: fields['premium_progress_bar_enabled'] = premium_progress_bar_enabled + widget_payload: EditWidgetSettings = {} + if widget_channel is not MISSING: + widget_payload['channel_id'] = None if widget_channel is None else widget_channel.id + if widget_enabled is not MISSING: + widget_payload['enabled'] = widget_enabled + + if widget_payload: + await self._state.http.edit_widget(self.id, payload=widget_payload, reason=reason) + data = await http.edit_guild(self.id, reason=reason, **fields) return Guild(data=data, state=self._state) @@ -3890,7 +3910,7 @@ class Guild(Hashable): ) -> None: """|coro| - Edits the widget of the guild. + Edits the widget of the guild. This can also be done with :attr:`~Guild.edit`. You must have :attr:`~Permissions.manage_guild` to do this. @@ -3918,7 +3938,8 @@ class Guild(Hashable): if enabled is not MISSING: payload['enabled'] = enabled - await self._state.http.edit_widget(self.id, payload=payload, reason=reason) + if payload: + await self._state.http.edit_widget(self.id, payload=payload, reason=reason) async def welcome_screen(self) -> WelcomeScreen: """|coro|