-
Notifications
You must be signed in to change notification settings - Fork 135
/
mentions.py
157 lines (122 loc) · 5.51 KB
/
mentions.py
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
# SPDX-License-Identifier: MIT
from __future__ import annotations
from typing import TYPE_CHECKING, Any, List, Union
from .enums import MessageType
__all__ = ("AllowedMentions",)
if TYPE_CHECKING:
from typing_extensions import Self
from .abc import Snowflake
from .message import Message
from .types.message import AllowedMentions as AllowedMentionsPayload
class _FakeBool:
def __repr__(self):
return "True"
def __eq__(self, other):
return other is True
def __bool__(self):
return True
default: Any = _FakeBool()
class AllowedMentions:
"""A class that represents what mentions are allowed in a message.
This class can be set during :class:`Client` initialisation to apply
to every message sent. It can also be applied on a per message basis
via :meth:`abc.Messageable.send` for more fine-grained control.
Attributes
----------
everyone: :class:`bool`
Whether to allow everyone and here mentions. Defaults to ``True``.
users: Union[:class:`bool`, List[:class:`abc.Snowflake`]]
Controls the users being mentioned. If ``True`` (the default) then
users are mentioned based on the message content. If ``False`` then
users are not mentioned at all. If a list of :class:`abc.Snowflake`
is given then only the users provided will be mentioned, provided those
users are in the message content.
roles: Union[:class:`bool`, List[:class:`abc.Snowflake`]]
Controls the roles being mentioned. If ``True`` (the default) then
roles are mentioned based on the message content. If ``False`` then
roles are not mentioned at all. If a list of :class:`abc.Snowflake`
is given then only the roles provided will be mentioned, provided those
roles are in the message content.
replied_user: :class:`bool`
Whether to mention the author of the message being replied to. Defaults
to ``True``.
.. versionadded:: 1.6
"""
__slots__ = ("everyone", "users", "roles", "replied_user")
def __init__(
self,
*,
everyone: bool = default,
users: Union[bool, List[Snowflake]] = default,
roles: Union[bool, List[Snowflake]] = default,
replied_user: bool = default,
):
self.everyone = everyone
self.users = users
self.roles = roles
self.replied_user = replied_user
@classmethod
def all(cls) -> Self:
"""A factory method that returns a :class:`AllowedMentions` with all fields explicitly set to ``True``
.. versionadded:: 1.5
"""
return cls(everyone=True, users=True, roles=True, replied_user=True)
@classmethod
def none(cls) -> Self:
"""A factory method that returns a :class:`AllowedMentions` with all fields set to ``False``
.. versionadded:: 1.5
"""
return cls(everyone=False, users=False, roles=False, replied_user=False)
@classmethod
def from_message(cls, message: Message) -> Self:
"""A factory method that returns a :class:`AllowedMentions` derived from the current :class:`.Message` state.
Note that this is not what AllowedMentions the message was sent with, but what the message actually mentioned.
For example, a message that successfully mentioned everyone will have :attr:`~AllowedMentions.everyone` set to ``True``.
.. versionadded:: 2.6
"""
# circular import
from .message import Message
return cls(
everyone=message.mention_everyone,
users=message.mentions.copy(), # type: ignore # mentions is a list of Snowflakes
roles=message.role_mentions.copy(), # type: ignore # mentions is a list of Snowflakes
replied_user=bool(
message.type is MessageType.reply
and message.reference
and isinstance(message.reference.resolved, Message)
and message.reference.resolved.author in message.mentions
),
)
def to_dict(self) -> AllowedMentionsPayload:
parse = []
data = {}
if self.everyone:
parse.append("everyone")
if self.users == True: # noqa: E712
parse.append("users")
elif self.users != False: # noqa: E712
data["users"] = [x.id for x in self.users]
if self.roles == True: # noqa: E712
parse.append("roles")
elif self.roles != False: # noqa: E712
data["roles"] = [x.id for x in self.roles]
if self.replied_user:
data["replied_user"] = True
data["parse"] = parse
return data # type: ignore
def merge(self, other: AllowedMentions) -> AllowedMentions:
# Creates a new AllowedMentions by merging from another one.
# Merge is done by using the 'self' values unless explicitly
# overridden by the 'other' values.
everyone = self.everyone if other.everyone is default else other.everyone
users = self.users if other.users is default else other.users
roles = self.roles if other.roles is default else other.roles
replied_user = self.replied_user if other.replied_user is default else other.replied_user
return AllowedMentions(
everyone=everyone, roles=roles, users=users, replied_user=replied_user
)
def __repr__(self) -> str:
return (
f"{self.__class__.__name__}(everyone={self.everyone}, "
f"users={self.users}, roles={self.roles}, replied_user={self.replied_user})"
)