aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/models/bot/role.py
blob: e37f3ccd29d07d627660cebde3b1e53a5ad7bd37 (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
from __future__ import annotations

from django.core.validators import MinValueValidator
from django.db import models

from pydis_site.apps.api.models.mixins import ModelReprMixin


class Role(ModelReprMixin, models.Model):
    """
    A role on our Discord server.

    The comparison operators <, <=, >, >=, ==, != act the same as they do with Role-objects of the
    discord.py library, see https://discordpy.readthedocs.io/en/latest/api.html#discord.Role
    """

    id = models.BigIntegerField(
        primary_key=True,
        validators=(
            MinValueValidator(
                limit_value=0,
                message="Role IDs cannot be negative."
            ),
        ),
        help_text="The role ID, taken from Discord.",
        verbose_name="ID"
    )
    name = models.CharField(
        max_length=100,
        help_text="The role name, taken from Discord."
    )
    colour = models.IntegerField(
        validators=(
            MinValueValidator(
                limit_value=0,
                message="Colour hex cannot be negative."
            ),
        ),
        help_text="The integer value of the colour of this role from Discord."
    )
    permissions = models.BigIntegerField(
        validators=(
            MinValueValidator(
                limit_value=0,
                message="Role permissions cannot be negative."
            ),
        ),
        help_text="The integer value of the permission bitset of this role from Discord."
    )
    position = models.IntegerField(
        help_text="The position of the role in the role hierarchy of the Discord Guild."
    )

    class Meta:
        """Set role ordering from highest to lowest position."""

        ordering = ("-position",)

    def __str__(self) -> str:
        """Returns the name of the current role, for display purposes."""
        return self.name

    def __lt__(self, other: Role) -> bool:
        """Compares the roles based on their position in the role hierarchy of the guild."""
        return self.position < other.position

    def __le__(self, other: Role) -> bool:
        """Compares the roles based on their position in the role hierarchy of the guild."""
        return self.position <= other.position