You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Enso-Bot/venv/Lib/site-packages/discord/permissions.py

557 lines
18 KiB
Python

4 years ago
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)
Copyright (c) 2015-2020 Rapptz
4 years ago
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 .flags import BaseFlags, flag_value, fill_with_flags
__all__ = (
'Permissions',
'PermissionOverwrite',
)
# A permission alias works like a regular flag but is marked
# So the PermissionOverwrite knows to work with it
class permission_alias(flag_value):
pass
def make_permission_alias(alias):
def decorator(func):
ret = permission_alias(func)
ret.alias = alias
return ret
return decorator
@fill_with_flags()
class Permissions(BaseFlags):
4 years ago
"""Wraps up the Discord permission value.
The properties provided are two way. You can set and retrieve individual
bits using the properties as if they were regular bools. This allows
you to edit permissions.
.. versionchanged:: 1.3
You can now use keyword arguments to initialize :class:`Permissions`
similar to :meth:`update`.
.. container:: operations
.. describe:: x == y
Checks if two permissions are equal.
.. describe:: x != y
Checks if two permissions are not equal.
.. describe:: x <= y
Checks if a permission is a subset of another permission.
.. describe:: x >= y
Checks if a permission is a superset of another permission.
.. describe:: x < y
Checks if a permission is a strict subset of another permission.
.. describe:: x > y
Checks if a permission is a strict superset of another permission.
.. describe:: hash(x)
Return the permission's hash.
.. describe:: iter(x)
Returns an iterator of ``(perm, value)`` pairs. This allows it
to be, for example, constructed as a dict or a list of pairs.
Note that aliases are not shown.
4 years ago
Attributes
-----------
value
The raw value. This value is a bit array field of a 53-bit integer
4 years ago
representing the currently available permissions. You should query
permissions via the properties rather than using this raw value.
"""
__slots__ = ()
def __init__(self, permissions=0, **kwargs):
if not isinstance(permissions, int):
raise TypeError('Expected int parameter, received %s instead.' % permissions.__class__.__name__)
self.value = permissions
for key, value in kwargs.items():
if key not in self.VALID_FLAGS:
raise TypeError('%r is not a valid permission name.' % key)
setattr(self, key, value)
4 years ago
def is_subset(self, other):
"""Returns ``True`` if self has the same or fewer permissions as other."""
4 years ago
if isinstance(other, Permissions):
return (self.value & other.value) == self.value
else:
raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__.__name__))
4 years ago
def is_superset(self, other):
"""Returns ``True`` if self has the same or more permissions as other."""
4 years ago
if isinstance(other, Permissions):
return (self.value | other.value) == self.value
else:
raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__.__name__))
4 years ago
def is_strict_subset(self, other):
"""Returns ``True`` if the permissions on other are a strict subset of those on self."""
4 years ago
return self.is_subset(other) and self != other
def is_strict_superset(self, other):
"""Returns ``True`` if the permissions on other are a strict superset of those on self."""
4 years ago
return self.is_superset(other) and self != other
__le__ = is_subset
__ge__ = is_superset
__lt__ = is_strict_subset
__gt__ = is_strict_superset
def __iter__(self):
for name, value in self.__class__.__dict__.items():
if isinstance(value, permission_alias):
continue
if isinstance(value, flag_value):
yield (name, self._has_flag(value.flag))
4 years ago
@classmethod
def none(cls):
"""A factory method that creates a :class:`Permissions` with all
permissions set to ``False``."""
4 years ago
return cls(0)
@classmethod
def all(cls):
"""A factory method that creates a :class:`Permissions` with all
permissions set to True."""
return cls(0b01111111111111111111111111111111)
4 years ago
@classmethod
def all_channel(cls):
"""A :class:`Permissions` with all channel-specific permissions set to
``True`` and the guild-specific ones set to ``False``. The guild-specific
4 years ago
permissions are currently:
- manage_guild
4 years ago
- kick_members
- ban_members
- administrator
- change_nickname
4 years ago
- manage_nicknames
"""
return cls(0b00110011111101111111110001010001)
@classmethod
def general(cls):
"""A factory method that creates a :class:`Permissions` with all
"General" permissions from the official Discord UI set to ``True``."""
return cls(0b01111100000010000000000010111111)
4 years ago
@classmethod
def text(cls):
"""A factory method that creates a :class:`Permissions` with all
"Text" permissions from the official Discord UI set to ``True``."""
4 years ago
return cls(0b00000000000001111111110001000000)
@classmethod
def voice(cls):
"""A factory method that creates a :class:`Permissions` with all
"Voice" permissions from the official Discord UI set to ``True``."""
return cls(0b00000011111100000000001100000000)
4 years ago
def update(self, **kwargs):
r"""Bulk updates this permission object.
4 years ago
Allows you to set multiple attributes by using keyword
arguments. The names must be equivalent to the properties
listed. Extraneous key/value pairs will be silently ignored.
Parameters
------------
\*\*kwargs
A list of key/value pairs to bulk update permissions with.
"""
for key, value in kwargs.items():
if key in self.VALID_FLAGS:
4 years ago
setattr(self, key, value)
def handle_overwrite(self, allow, deny):
# Basically this is what's happening here.
# We have an original bit array, e.g. 1010
# Then we have another bit array that is 'denied', e.g. 1111
# And then we have the last one which is 'allowed', e.g. 0101
# We want original OP denied to end up resulting in
# whatever is in denied to be set to 0.
# So 1010 OP 1111 -> 0000
# Then we take this value and look at the allowed values.
# And whatever is allowed is set to 1.
# So 0000 OP2 0101 -> 0101
# The OP is base & ~denied.
# The OP2 is base | allowed.
self.value = (self.value & ~deny) | allow
@flag_value
4 years ago
def create_instant_invite(self):
""":class:`bool`: Returns ``True`` if the user can create instant invites."""
return 1 << 0
4 years ago
@flag_value
4 years ago
def kick_members(self):
""":class:`bool`: Returns ``True`` if the user can kick users from the guild."""
return 1 << 1
4 years ago
@flag_value
4 years ago
def ban_members(self):
""":class:`bool`: Returns ``True`` if a user can ban users from the guild."""
return 1 << 2
4 years ago
@flag_value
4 years ago
def administrator(self):
""":class:`bool`: Returns ``True`` if a user is an administrator. This role overrides all other permissions.
4 years ago
This also bypasses all channel-specific overrides.
"""
return 1 << 3
4 years ago
@flag_value
4 years ago
def manage_channels(self):
""":class:`bool`: Returns ``True`` if a user can edit, delete, or create channels in the guild.
4 years ago
This also corresponds to the "Manage Channel" channel-specific override."""
return 1 << 4
4 years ago
@flag_value
def manage_guild(self):
""":class:`bool`: Returns ``True`` if a user can edit guild properties."""
return 1 << 5
4 years ago
@flag_value
4 years ago
def add_reactions(self):
""":class:`bool`: Returns ``True`` if a user can add reactions to messages."""
return 1 << 6
4 years ago
@flag_value
def view_audit_log(self):
""":class:`bool`: Returns ``True`` if a user can view the guild's audit log."""
return 1 << 7
4 years ago
@flag_value
def priority_speaker(self):
""":class:`bool`: Returns ``True`` if a user can be more easily heard while talking."""
return 1 << 8
4 years ago
@flag_value
def stream(self):
""":class:`bool`: Returns ``True`` if a user can stream in a voice channel."""
return 1 << 9
4 years ago
@flag_value
def read_messages(self):
""":class:`bool`: Returns ``True`` if a user can read messages from all or specific text channels."""
return 1 << 10
4 years ago
@make_permission_alias('read_messages')
def view_channel(self):
""":class:`bool`: An alias for :attr:`read_messages`.
4 years ago
.. versionadded:: 1.3
"""
return 1 << 10
@flag_value
def send_messages(self):
""":class:`bool`: Returns ``True`` if a user can send messages from all or specific text channels."""
return 1 << 11
4 years ago
@flag_value
4 years ago
def send_tts_messages(self):
""":class:`bool`: Returns ``True`` if a user can send TTS messages from all or specific text channels."""
return 1 << 12
4 years ago
@flag_value
def manage_messages(self):
""":class:`bool`: Returns ``True`` if a user can delete or pin messages in a text channel.
4 years ago
.. note::
4 years ago
Note that there are currently no ways to edit other people's messages.
"""
return 1 << 13
4 years ago
@flag_value
def embed_links(self):
""":class:`bool`: Returns ``True`` if a user's messages will automatically be embedded by Discord."""
return 1 << 14
@flag_value
4 years ago
def attach_files(self):
""":class:`bool`: Returns ``True`` if a user can send files in their messages."""
return 1 << 15
4 years ago
@flag_value
4 years ago
def read_message_history(self):
""":class:`bool`: Returns ``True`` if a user can read a text channel's previous messages."""
return 1 << 16
4 years ago
@flag_value
def mention_everyone(self):
""":class:`bool`: Returns ``True`` if a user's @everyone or @here will mention everyone in the text channel."""
return 1 << 17
4 years ago
@flag_value
def external_emojis(self):
""":class:`bool`: Returns ``True`` if a user can use emojis from other guilds."""
return 1 << 18
4 years ago
@make_permission_alias('external_emojis')
def use_external_emojis(self):
""":class:`bool`: An alias for :attr:`external_emojis`.
4 years ago
.. versionadded:: 1.3
"""
return 1 << 18
4 years ago
@flag_value
def view_guild_insights(self):
""":class:`bool`: Returns ``True`` if a user can view the guild's insights.
.. versionadded:: 1.3
"""
return 1 << 19
4 years ago
@flag_value
def connect(self):
""":class:`bool`: Returns ``True`` if a user can connect to a voice channel."""
return 1 << 20
4 years ago
@flag_value
def speak(self):
""":class:`bool`: Returns ``True`` if a user can speak in a voice channel."""
return 1 << 21
@flag_value
4 years ago
def mute_members(self):
""":class:`bool`: Returns ``True`` if a user can mute other users."""
return 1 << 22
4 years ago
@flag_value
4 years ago
def deafen_members(self):
""":class:`bool`: Returns ``True`` if a user can deafen other users."""
return 1 << 23
4 years ago
@flag_value
4 years ago
def move_members(self):
""":class:`bool`: Returns ``True`` if a user can move users between other voice channels."""
return 1 << 24
4 years ago
@flag_value
4 years ago
def use_voice_activation(self):
""":class:`bool`: Returns ``True`` if a user can use voice activation in voice channels."""
return 1 << 25
4 years ago
@flag_value
4 years ago
def change_nickname(self):
""":class:`bool`: Returns ``True`` if a user can change their nickname in the guild."""
return 1 << 26
4 years ago
@flag_value
4 years ago
def manage_nicknames(self):
""":class:`bool`: Returns ``True`` if a user can change other user's nickname in the guild."""
return 1 << 27
4 years ago
@flag_value
4 years ago
def manage_roles(self):
""":class:`bool`: Returns ``True`` if a user can create or edit roles less than their role's position.
4 years ago
This also corresponds to the "Manage Permissions" channel-specific override.
4 years ago
"""
return 1 << 28
4 years ago
@make_permission_alias('manage_roles')
def manage_permissions(self):
""":class:`bool`: An alias for :attr:`manage_roles`.
4 years ago
.. versionadded:: 1.3
"""
return 1 << 28
@flag_value
def manage_webhooks(self):
""":class:`bool`: Returns ``True`` if a user can create, edit, or delete webhooks."""
return 1 << 29
4 years ago
@flag_value
4 years ago
def manage_emojis(self):
""":class:`bool`: Returns ``True`` if a user can create, edit, or delete emojis."""
return 1 << 30
4 years ago
# 1 unused
# after these 32 bits, there's 21 more unused ones technically
def augment_from_permissions(cls):
cls.VALID_NAMES = set(Permissions.VALID_FLAGS)
aliases = set()
# make descriptors for all the valid names and aliases
for name, value in Permissions.__dict__.items():
if isinstance(value, permission_alias):
key = value.alias
aliases.add(name)
elif isinstance(value, flag_value):
key = name
else:
continue
4 years ago
# god bless Python
def getter(self, x=key):
4 years ago
return self._values.get(x)
def setter(self, value, x=key):
4 years ago
self._set(x, value)
prop = property(getter, setter)
setattr(cls, name, prop)
cls.PURE_FLAGS = cls.VALID_NAMES - aliases
4 years ago
return cls
@augment_from_permissions
class PermissionOverwrite:
r"""A type that is used to represent a channel specific permission.
4 years ago
Unlike a regular :class:`Permissions`\, the default value of a
permission is equivalent to ``None`` and not ``False``. Setting
a value to ``False`` is **explicitly** denying that permission,
while setting a value to ``True`` is **explicitly** allowing
that permission.
The values supported by this are the same as :class:`Permissions`
with the added possibility of it being set to ``None``.
.. container:: operations
.. describe:: x == y
Checks if two overwrites are equal.
.. describe:: x != y
Checks if two overwrites are not equal.
.. describe:: iter(x)
4 years ago
Returns an iterator of ``(perm, value)`` pairs. This allows it
to be, for example, constructed as a dict or a list of pairs.
Note that aliases are not shown.
4 years ago
Parameters
-----------
\*\*kwargs
Set the value of permissions by their name.
"""
__slots__ = ('_values',)
4 years ago
def __init__(self, **kwargs):
self._values = {}
for key, value in kwargs.items():
if key not in self.VALID_NAMES:
raise ValueError('no permission called {0}.'.format(key))
4 years ago
setattr(self, key, value)
def __eq__(self, other):
return isinstance(other, PermissionOverwrite) and self._values == other._values
4 years ago
def _set(self, key, value):
if value not in (True, None, False):
raise TypeError('Expected bool or NoneType, received {0.__class__.__name__}'.format(value))
self._values[key] = value
def pair(self):
"""Returns the (allow, deny) pair from this overwrite.
The value of these pairs is :class:`Permissions`.
"""
allow = Permissions.none()
deny = Permissions.none()
4 years ago
for key, value in self._values.items():
if value is True:
setattr(allow, key, True)
elif value is False:
setattr(deny, key, True)
return allow, deny
@classmethod
def from_pair(cls, allow, deny):
"""Creates an overwrite from an allow/deny pair of :class:`Permissions`."""
ret = cls()
for key, value in allow:
if value is True:
setattr(ret, key, True)
for key, value in deny:
if value is True:
setattr(ret, key, False)
return ret
def is_empty(self):
"""Checks if the permission overwrite is currently empty.
An empty permission overwrite is one that has no overwrites set
to ``True`` or ``False``.
4 years ago
"""
return all(x is None for x in self._values.values())
def update(self, **kwargs):
r"""Bulk updates this permission overwrite object.
4 years ago
Allows you to set multiple attributes by using keyword
arguments. The names must be equivalent to the properties
listed. Extraneous key/value pairs will be silently ignored.
Parameters
------------
\*\*kwargs
A list of key/value pairs to bulk update with.
"""
for key, value in kwargs.items():
if key not in self.VALID_NAMES:
continue
setattr(self, key, value)
def __iter__(self):
for key in self.PURE_FLAGS:
4 years ago
yield key, self._values.get(key)