File: installs.py

package info (click to toggle)
python-discord 2.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,180 kB
  • sloc: python: 46,013; javascript: 363; makefile: 154
file content (207 lines) | stat: -rw-r--r-- 7,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
The MIT License (MIT)

Copyright (c) 2015-present Rapptz

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations
from typing import TYPE_CHECKING, ClassVar, List, Optional, Sequence

__all__ = (
    'AppInstallationType',
    'AppCommandContext',
)

if TYPE_CHECKING:
    from typing_extensions import Self
    from ..types.interactions import InteractionContextType, InteractionInstallationType


class AppInstallationType:
    r"""Represents the installation location of an application command.

    .. versionadded:: 2.4

    Parameters
    -----------
    guild: Optional[:class:`bool`]
        Whether the integration is a guild install.
    user: Optional[:class:`bool`]
        Whether the integration is a user install.
    """

    __slots__ = ('_guild', '_user')

    GUILD: ClassVar[int] = 0
    USER: ClassVar[int] = 1

    def __init__(self, *, guild: Optional[bool] = None, user: Optional[bool] = None):
        self._guild: Optional[bool] = guild
        self._user: Optional[bool] = user

    @property
    def guild(self) -> bool:
        """:class:`bool`: Whether the integration is a guild install."""
        return bool(self._guild)

    @guild.setter
    def guild(self, value: bool) -> None:
        self._guild = bool(value)

    @property
    def user(self) -> bool:
        """:class:`bool`: Whether the integration is a user install."""
        return bool(self._user)

    @user.setter
    def user(self, value: bool) -> None:
        self._user = bool(value)

    def merge(self, other: AppInstallationType) -> AppInstallationType:
        # Merging is similar to AllowedMentions where `self` is the base
        # and the `other` is the override preference
        guild = self._guild if other._guild is None else other._guild
        user = self._user if other._user is None else other._user
        return AppInstallationType(guild=guild, user=user)

    def _is_unset(self) -> bool:
        return all(x is None for x in (self._guild, self._user))

    def _merge_to_array(self, other: Optional[AppInstallationType]) -> Optional[List[InteractionInstallationType]]:
        result = self.merge(other) if other is not None else self
        if result._is_unset():
            return None
        return result.to_array()

    @classmethod
    def _from_value(cls, value: Sequence[InteractionInstallationType]) -> Self:
        self = cls()
        for x in value:
            if x == cls.GUILD:
                self._guild = True
            elif x == cls.USER:
                self._user = True
        return self

    def to_array(self) -> List[InteractionInstallationType]:
        values = []
        if self._guild:
            values.append(self.GUILD)
        if self._user:
            values.append(self.USER)
        return values


class AppCommandContext:
    r"""Wraps up the Discord :class:`~discord.app_commands.Command` execution context.

    .. versionadded:: 2.4

    Parameters
    -----------
    guild: Optional[:class:`bool`]
        Whether the context allows usage in a guild.
    dm_channel: Optional[:class:`bool`]
        Whether the context allows usage in a DM channel.
    private_channel: Optional[:class:`bool`]
        Whether the context allows usage in a DM or a GDM channel.
    """

    GUILD: ClassVar[int] = 0
    DM_CHANNEL: ClassVar[int] = 1
    PRIVATE_CHANNEL: ClassVar[int] = 2

    __slots__ = ('_guild', '_dm_channel', '_private_channel')

    def __init__(
        self,
        *,
        guild: Optional[bool] = None,
        dm_channel: Optional[bool] = None,
        private_channel: Optional[bool] = None,
    ):
        self._guild: Optional[bool] = guild
        self._dm_channel: Optional[bool] = dm_channel
        self._private_channel: Optional[bool] = private_channel

    @property
    def guild(self) -> bool:
        """:class:`bool`: Whether the context allows usage in a guild."""
        return bool(self._guild)

    @guild.setter
    def guild(self, value: bool) -> None:
        self._guild = bool(value)

    @property
    def dm_channel(self) -> bool:
        """:class:`bool`: Whether the context allows usage in a DM channel."""
        return bool(self._dm_channel)

    @dm_channel.setter
    def dm_channel(self, value: bool) -> None:
        self._dm_channel = bool(value)

    @property
    def private_channel(self) -> bool:
        """:class:`bool`: Whether the context allows usage in a DM or a GDM channel."""
        return bool(self._private_channel)

    @private_channel.setter
    def private_channel(self, value: bool) -> None:
        self._private_channel = bool(value)

    def merge(self, other: AppCommandContext) -> AppCommandContext:
        guild = self._guild if other._guild is None else other._guild
        dm_channel = self._dm_channel if other._dm_channel is None else other._dm_channel
        private_channel = self._private_channel if other._private_channel is None else other._private_channel
        return AppCommandContext(guild=guild, dm_channel=dm_channel, private_channel=private_channel)

    def _is_unset(self) -> bool:
        return all(x is None for x in (self._guild, self._dm_channel, self._private_channel))

    def _merge_to_array(self, other: Optional[AppCommandContext]) -> Optional[List[InteractionContextType]]:
        result = self.merge(other) if other is not None else self
        if result._is_unset():
            return None
        return result.to_array()

    @classmethod
    def _from_value(cls, value: Sequence[InteractionContextType]) -> Self:
        self = cls()
        for x in value:
            if x == cls.GUILD:
                self._guild = True
            elif x == cls.DM_CHANNEL:
                self._dm_channel = True
            elif x == cls.PRIVATE_CHANNEL:
                self._private_channel = True
        return self

    def to_array(self) -> List[InteractionContextType]:
        values = []
        if self._guild:
            values.append(self.GUILD)
        if self._dm_channel:
            values.append(self.DM_CHANNEL)
        if self._private_channel:
            values.append(self.PRIVATE_CHANNEL)
        return values