Skip to content

Commit

Permalink
feat: implement onboarding features (#2127)
Browse files Browse the repository at this point in the history
* initial onboarding impl.

* Implement main edit function

* implement shorthand edit

* typo

* better doc

* misc

* fix enums

* fix repr

* docs adjust

* fix edit args

* import fixes

* docs clarify

* final touches?

* randint...?

* style(pre-commit): auto fixes from pre-commit.com hooks

* typefix

* style(pre-commit): auto fixes from pre-commit.com hooks

* docs updates

* cleanup

* style(pre-commit): auto fixes from pre-commit.com hooks

* Update enum

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>

* Update enums

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>

* Replace _guild with guild

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>

* Add new PromptOptions fields

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>

* Update to_dict for emoji changes and exchange randint for generate_snowflake

* style(pre-commit): auto fixes from pre-commit.com hooks

* Final push

* style(pre-commit): auto fixes from pre-commit.com hooks

* remove debug lines

* thanku doruk

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>

* chore: minimal changes

* chore: finalize

* chore: promise this is the last

---------

Signed-off-by: Lala Sabathil <lala@pycord.dev>
Signed-off-by: UK <41271523+NeloBlivion@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lala Sabathil <lala@pycord.dev>
Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 31, 2024
1 parent 90f9023 commit 6ba54b4
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2322](https://github.com/Pycord-Development/pycord/pull/2322))
- Added `User.avatar_decoration`.
([#2131](https://github.com/Pycord-Development/pycord/pull/2131))
- Added support for guild onboarding related features.
([#2127](https://github.com/Pycord-Development/pycord/pull/2127))

### Changed

Expand Down
1 change: 1 addition & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from .mentions import *
from .message import *
from .object import *
from .onboarding import *
from .partial_emoji import *
from .permissions import *
from .player import *
Expand Down
32 changes: 32 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"AutoModActionType",
"AutoModKeywordPresetType",
"ApplicationRoleConnectionMetadataType",
"PromptType",
"OnboardingMode",
"ReactionType",
)

Expand Down Expand Up @@ -428,6 +430,11 @@ class AuditLogAction(Enum):
auto_moderation_user_communication_disabled = 145
creator_monetization_request_created = 150
creator_monetization_terms_accepted = 151
onboarding_question_create = 163
onboarding_question_update = 164
onboarding_update = 167
server_guide_create = 190
server_guide_update = 191

@property
def category(self) -> AuditLogActionCategory | None:
Expand Down Expand Up @@ -490,6 +497,11 @@ def category(self) -> AuditLogActionCategory | None:
AuditLogAction.auto_moderation_user_communication_disabled: None,
AuditLogAction.creator_monetization_request_created: None,
AuditLogAction.creator_monetization_terms_accepted: None,
AuditLogAction.onboarding_question_create: AuditLogActionCategory.create,
AuditLogAction.onboarding_question_update: AuditLogActionCategory.update,
AuditLogAction.onboarding_update: AuditLogActionCategory.update,
AuditLogAction.server_guide_create: AuditLogActionCategory.create,
AuditLogAction.server_guide_update: AuditLogActionCategory.update,
}
return lookup[self]

Expand Down Expand Up @@ -530,6 +542,12 @@ def target_type(self) -> str | None:
return "application_command_permission"
elif v < 146:
return "auto_moderation_rule"
elif v < 152:
return "monetization"
elif v < 168:
return "onboarding"
elif v < 192:
return "server_guide"


class UserFlags(Enum):
Expand Down Expand Up @@ -946,6 +964,20 @@ class ApplicationRoleConnectionMetadataType(Enum):
boolean_not_equal = 8


class PromptType(Enum):
"""Guild Onboarding Prompt Type"""

multiple_choice = 0
dropdown = 1


class OnboardingMode(Enum):
"""Guild Onboarding Mode"""

default = 0
advanced = 1


class ReactionType(Enum):
"""The reaction type"""

Expand Down
83 changes: 83 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from .iterators import AuditLogIterator, BanIterator, MemberIterator
from .member import Member, VoiceState
from .mixins import Hashable
from .onboarding import Onboarding
from .permissions import PermissionOverwrite
from .role import Role
from .scheduled_events import ScheduledEvent, ScheduledEventLocation
Expand Down Expand Up @@ -3842,6 +3843,88 @@ async def create_auto_moderation_rule(
)
return AutoModRule(state=self._state, data=data)

async def onboarding(self):
"""|coro|
Returns the :class:`Onboarding` flow for the guild.
.. versionadded:: 2.5
Returns
-------
:class:`Onboarding`
The onboarding flow for the guild.
Raises
------
HTTPException
Retrieving the onboarding flow failed somehow.
"""
data = await self._state.http.get_onboarding(self.id)
return Onboarding(data=data, guild=self)

async def edit_onboarding(
self,
*,
prompts: list[OnboardingPrompt] | None = MISSING,
default_channels: list[Snowflake] | None = MISSING,
enabled: bool | None = MISSING,
mode: OnboardingMode | None = MISSING,
reason: str | None = MISSING,
) -> Onboarding:
"""|coro|
A shorthand for :attr:`Onboarding.edit` without fetching the onboarding flow.
You must have the :attr:`~Permissions.manage_guild` and :attr:`~Permissions.manage_roles` permissions in the
guild to do this.
Parameters
----------
prompts: Optional[List[:class:`OnboardingPrompt`]]
The new list of prompts for this flow.
default_channels: Optional[List[:class:`Snowflake`]]
The new default channels that users are opted into.
enabled: Optional[:class:`bool`]
Whether onboarding should be enabled. Setting this to ``True`` requires
the guild to have ``COMMUNITY`` in :attr:`~Guild.features` and at
least 7 ``default_channels``.
mode: Optional[:class:`OnboardingMode`]
The new onboarding mode.
reason: Optional[:class:`str`]
The reason that shows up on Audit log.
Returns
-------
:class:`Onboarding`
The updated onboarding flow.
Raises
------
HTTPException
Editing the onboarding flow failed somehow.
Forbidden
You don't have permissions to edit the onboarding flow.
"""

fields: dict[str, Any] = {}
if prompts is not MISSING:
fields["prompts"] = [prompt.to_dict() for prompt in prompts]

if default_channels is not MISSING:
fields["default_channel_ids"] = [channel.id for channel in default_channels]

if enabled is not MISSING:
fields["enabled"] = enabled

if mode is not MISSING:
fields["mode"] = mode.value

new = await self._state.http.edit_onboarding(self.id, fields, reason=reason)
return Onboarding(data=new, guild=self)

async def delete_auto_moderation_rule(
self,
id: int,
Expand Down
24 changes: 24 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
invite,
member,
message,
onboarding,
role,
scheduled_events,
sticker,
Expand Down Expand Up @@ -2884,6 +2885,29 @@ def update_application_role_connection_metadata_records(
)
return self.request(r, json=payload)

# Onboarding

def get_onboarding(self, guild_id: Snowflake) -> Response[onboarding.Onboarding]:
return self.request(
Route("GET", "/guilds/{guild_id}/onboarding", guild_id=guild_id)
)

def edit_onboarding(
self, guild_id: Snowflake, payload: Any, *, reason: str | None = None
) -> Response[onboarding.Onboarding]:
keys = (
"prompts",
"default_channel_ids",
"enabled",
"mode",
)
payload = {key: val for key, val in payload.items() if key in keys}
return self.request(
Route("PUT", "/guilds/{guild_id}/onboarding", guild_id=guild_id),
json=payload,
reason=reason,
)

# Misc

def application_info(self) -> Response[appinfo.AppInfo]:
Expand Down

0 comments on commit 6ba54b4

Please sign in to comment.