aboutsummaryrefslogtreecommitdiff
path: root/discord/template.py
blob: 70d4fd2a14aa5aa0cb2ede0d11e483f0948be7ae (plain) (blame)
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
from .utils import parse_time, _get_as_snowflake
from .enums import VoiceRegion
from .guild import Guild

__all__ = (
    'Template'
)

class _FriendlyHttpAttributeErrorHelper:
    __slots__ = ()

    def __getattr__(self, attr):
        raise AttributeError('PartialTemplateState does not support http methods.')

class _PartialTemplateState:
    def __init__(self, *, state):
        self.__state = state
        self.http = _FriendlyHttpAttributeErrorHelper()
    
    @property
    def is_bot(self):
        return self.__state.is_bot
    
    @property
    def shard_count(self):
        return self.__state.shard_count

    @property
    def user(self):
        return self.__state.user
    
    @property
    def self_id(self):
        return self.__state.user.id
    
    def store_emoji(self, guild, packet):
        return None
    
    def _get_voice_client(self, id):
        return None

    def _get_message(self, id):
        return None

    async def query_members(self, **kwargs):
        return []

    def __getattr__(self, attr):
        raise AttributeError('PartialTemplateState does not support {0!r}.'.format(attr))

class Template:
    """Represents a Discord template.

    .. versionadded:: 1.4

    Attributes
    -----------
    code: :class:`str`
        The template code.
    uses: :class:`int`
        How many time the template has been used.
    name: :class:`str`
        The name of the template.
    description: :class:`str`
        The description of the template.
    creator: :class:`User`
        The creator of the template.
    created_at: :class:`datetime.datetime`
        When the template was created.
    updated_at: :class:`datetime.datetime`
        When the template was last updated (referred to as "last synced" in the client).
    source_guild: :class:`TemplateGuild`
        The source guild.
    """

    def __init__(self, *, state, data):
        self._state = state

        self.code = data['code']
        self.uses = data['usage_count']
        self.name =  data['name']
        self.description = data['description']
        creator_data = data.get('creator')
        self.creator = None if creator_data is None else self._state.store_user(creator_data)

        self.created_at = parse_time(data.get('created_at'))
        self.updated_at = parse_time(data.get('updated_at'))

        id = _get_as_snowflake(data, 'source_guild_id')
        source_serialised = data['serialized_source_guild']
        source_serialised['id'] = id
        state = _PartialTemplateState(state=self._state)

        self.source_guild = Guild(data=source_serialised, state=state)

    def __repr__(self):
        return '<Template code={0.code!r} uses={0.uses} name={0.name!r}' \
               ' creator={0.creator!r} source_guild={0.source_guild!r}>'.format(self)

    async def create_guild(self, name, region=None, icon=None):
        """|coro|

        Creates a :class:`.Guild` using the template.

        Bot accounts in more than 10 guilds are not allowed to create guilds.

        Parameters
        ----------
        name: :class:`str`
            The name of the guild.
        region: :class:`.VoiceRegion`
            The region for the voice communication server.
            Defaults to :attr:`.VoiceRegion.us_west`.
        icon: :class:`bytes`
            The :term:`py:bytes-like object` representing the icon. See :meth:`.ClientUser.edit`
            for more details on what is expected.

        Raises
        ------
        :exc:`.HTTPException`
            Guild creation failed.
        :exc:`.InvalidArgument`
            Invalid icon image format given. Must be PNG or JPG.

        Returns
        -------
        :class:`.Guild`
            The guild created. This is not the same guild that is
            added to cache.
        """
        if icon is not None:
            icon = _bytes_to_base64_data(icon)

        if region is None:
            region = VoiceRegion.us_west.value
        else:
            region = region.value

        data = await self._state.http.create_from_template(self.code, name, region, icon)
        return Guild(data=data, state=self._sate)