|
| 1 | +from typing import Any, Dict, List, Optional, Union |
| 2 | + |
| 3 | +import attrs |
| 4 | + |
| 5 | +from interactions.client.const import MISSING, Absent |
| 6 | +from interactions.client.mixins.serialization import DictSerializationMixin |
| 7 | +from interactions.client.utils.attr_converters import optional |
| 8 | +from interactions.models.discord.base import ClientObject |
| 9 | +from interactions.models.discord.emoji import PartialEmoji, process_emoji |
| 10 | +from interactions.models.discord.enums import OnboardingMode, OnboardingPromptType |
| 11 | +from interactions.models.discord.snowflake import ( |
| 12 | + Snowflake, |
| 13 | + Snowflake_Type, |
| 14 | + SnowflakeObject, |
| 15 | + to_snowflake, |
| 16 | + to_snowflake_list, |
| 17 | +) |
| 18 | + |
| 19 | +__all__ = ("OnboardingPromptOption", "OnboardingPrompt", "Onboarding") |
| 20 | + |
| 21 | + |
| 22 | +@attrs.define(eq=False, order=False, hash=False, kw_only=True) |
| 23 | +class OnboardingPromptOption(SnowflakeObject, DictSerializationMixin): |
| 24 | + channel_ids: List["Snowflake"] = attrs.field(repr=False, converter=to_snowflake_list) |
| 25 | + """IDs for channels a member is added to when the option is selected""" |
| 26 | + role_ids: List["Snowflake"] = attrs.field(repr=False, converter=to_snowflake_list) |
| 27 | + """IDs for roles assigned to a member when the option is selected""" |
| 28 | + title: str = attrs.field(repr=False) |
| 29 | + """Title of the option""" |
| 30 | + description: Optional[str] = attrs.field(repr=False, default=None) |
| 31 | + """Description of the option""" |
| 32 | + emoji: Optional[PartialEmoji] = attrs.field(repr=False, default=None, converter=optional(PartialEmoji.from_dict)) |
| 33 | + """Emoji of the option""" |
| 34 | + |
| 35 | + # this method is here because Discord needs the id field to be present in the payload |
| 36 | + @classmethod |
| 37 | + def create( |
| 38 | + cls, |
| 39 | + title: str, |
| 40 | + *, |
| 41 | + channel_ids: Optional[List[Snowflake_Type]] = None, |
| 42 | + role_ids: Optional[List[Snowflake_Type]] = None, |
| 43 | + description: Optional[str] = None, |
| 44 | + emoji: Optional[Union[PartialEmoji, dict, str]] = None, |
| 45 | + ) -> "OnboardingPromptOption": |
| 46 | + """ |
| 47 | + Creates a new Onboarding prompt option object. |
| 48 | +
|
| 49 | + Args: |
| 50 | + title: Title of the option |
| 51 | + channel_ids: Channel IDs that this option represents |
| 52 | + role_ids: Role IDs that this option represents |
| 53 | + description: Description of the option |
| 54 | + emoji: Emoji of the option |
| 55 | +
|
| 56 | + Returns: |
| 57 | + The newly created OnboardingPromptOption object |
| 58 | +
|
| 59 | + """ |
| 60 | + return cls( |
| 61 | + id=0, |
| 62 | + channel_ids=channel_ids or [], |
| 63 | + role_ids=role_ids or [], |
| 64 | + title=title, |
| 65 | + description=description, |
| 66 | + emoji=process_emoji(emoji), |
| 67 | + ) |
| 68 | + |
| 69 | + def as_dict(self) -> Dict[str, Any]: |
| 70 | + data = { |
| 71 | + "id": self.id, |
| 72 | + "channel_ids": self.channel_ids, |
| 73 | + "role_ids": self.role_ids, |
| 74 | + "title": self.title, |
| 75 | + "description": self.description, |
| 76 | + } |
| 77 | + # use the separate fields when sending to Discord |
| 78 | + if self.emoji is not None: |
| 79 | + data["emoji_id"] = self.emoji.id |
| 80 | + data["emoji_name"] = self.emoji.name |
| 81 | + data["emoji_animated"] = self.emoji.animated |
| 82 | + return data |
| 83 | + |
| 84 | + |
| 85 | +@attrs.define(eq=False, order=False, hash=False, kw_only=False) |
| 86 | +class OnboardingPrompt(SnowflakeObject, DictSerializationMixin): |
| 87 | + type: OnboardingPromptType = attrs.field(repr=False, converter=OnboardingPromptType) |
| 88 | + """Type of the prompt""" |
| 89 | + options: List[OnboardingPromptOption] = attrs.field(repr=False, converter=OnboardingPromptOption.from_list) |
| 90 | + """Options available in the prompt""" |
| 91 | + title: str = attrs.field(repr=False) |
| 92 | + """Title of the prompt""" |
| 93 | + single_select: bool = attrs.field(repr=False) |
| 94 | + """Whether users are limited to selecting one option for the prompt""" |
| 95 | + required: bool = attrs.field(repr=False) |
| 96 | + """Whether users are required to complete this prompt""" |
| 97 | + in_onboarding: bool = attrs.field(repr=False) |
| 98 | + """Whether the prompt is present in the onboarding flow; otherwise it is only in the Channels & Roles tab""" |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def create( |
| 102 | + cls, |
| 103 | + *, |
| 104 | + type: Union[OnboardingPromptType, int] = OnboardingPromptType.MULTIPLE_CHOICE, |
| 105 | + options: List[OnboardingPromptOption], |
| 106 | + title: str, |
| 107 | + single_select: bool = False, |
| 108 | + required: bool = False, |
| 109 | + in_onboarding: bool = True, |
| 110 | + ) -> "OnboardingPrompt": |
| 111 | + """ |
| 112 | + Creates a new Onboarding prompt object. |
| 113 | +
|
| 114 | + Args: |
| 115 | + type: Type of the prompt |
| 116 | + options: Options available in the prompt |
| 117 | + title: Title of the prompt |
| 118 | + single_select: Whether users are limited to selecting one option for the prompt |
| 119 | + required: Whether users are required to complete this prompt |
| 120 | + in_onboarding: Whether the prompt is present in the onboarding flow; otherwise it is only in the Channels & Roles tab |
| 121 | +
|
| 122 | + Returns: |
| 123 | + The newly created OnboardingPrompt object |
| 124 | +
|
| 125 | + """ |
| 126 | + return cls( |
| 127 | + id=0, |
| 128 | + type=type, |
| 129 | + options=options, |
| 130 | + title=title, |
| 131 | + single_select=single_select, |
| 132 | + required=required, |
| 133 | + in_onboarding=in_onboarding, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@attrs.define(eq=False, order=False, hash=False, kw_only=False) |
| 138 | +class Onboarding(ClientObject): |
| 139 | + """Represents the onboarding flow for a guild.""" |
| 140 | + |
| 141 | + guild_id: Snowflake = attrs.field(repr=False, converter=to_snowflake) |
| 142 | + """ID of the guild this onboarding is part of""" |
| 143 | + prompts: list[OnboardingPrompt] = attrs.field(repr=False, converter=OnboardingPrompt.from_list) |
| 144 | + """Prompts shown during onboarding and in customize community""" |
| 145 | + default_channel_ids: list[Snowflake] = attrs.field(repr=False, converter=to_snowflake_list) |
| 146 | + """Channel IDs that members get opted into automatically""" |
| 147 | + enabled: bool = attrs.field(repr=False) |
| 148 | + """Whether onboarding is enabled in the guild""" |
| 149 | + mode: OnboardingMode = attrs.field(repr=False, converter=OnboardingMode) |
| 150 | + """Current mode of onboarding""" |
| 151 | + |
| 152 | + async def edit( |
| 153 | + self, |
| 154 | + *, |
| 155 | + prompts: Absent[List[OnboardingPrompt]] = MISSING, |
| 156 | + default_channel_ids: Absent[list[Snowflake_Type]] = MISSING, |
| 157 | + enabled: Absent[bool] = MISSING, |
| 158 | + mode: Absent[Union[OnboardingMode, int]] = MISSING, |
| 159 | + reason: Absent[str] = MISSING, |
| 160 | + ) -> None: |
| 161 | + """ |
| 162 | + Edits this Onboarding flow. |
| 163 | +
|
| 164 | + Args: |
| 165 | + prompts: Prompts shown during onboarding and in customize community |
| 166 | + default_channel_ids: Channel IDs that members get opted into automatically |
| 167 | + enabled: Whether onboarding is enabled in the guild |
| 168 | + mode: Current mode of onboarding |
| 169 | + reason: The reason for this change |
| 170 | +
|
| 171 | + """ |
| 172 | + payload = { |
| 173 | + "prompts": prompts, |
| 174 | + "default_channel_ids": default_channel_ids, |
| 175 | + "enabled": enabled, |
| 176 | + "mode": mode, |
| 177 | + } |
| 178 | + data = await self._client.http.modify_guild_onboarding(self.guild_id, payload, reason) |
| 179 | + self.update_from_dict(data) |
0 commit comments