Skip to content

User

attrs class BaseUser (DiscordObject, _SendDMMixin)

Base class for User, essentially partial user discord model.

Attr attributes:

Name Type Description
id int

Discord unique snowflake ID

username str

The user's username, not unique across the platform

discriminator int

The user's 4-digit discord-tag

avatar Asset

The user's default avatar

Source code in naff/models/discord/user.py
@define()
class BaseUser(DiscordObject, _SendDMMixin):
    """Base class for User, essentially partial user discord model."""

    username: str = field(repr=True, metadata=docs("The user's username, not unique across the platform"))
    discriminator: int = field(repr=True, metadata=docs("The user's 4-digit discord-tag"))
    avatar: "Asset" = field(metadata=docs("The user's default avatar"))

    def __str__(self) -> str:
        return self.tag

    @classmethod
    def _process_dict(cls, data: Dict[str, Any], client: "Client") -> Dict[str, Any]:
        if not isinstance(data["avatar"], Asset):
            if data["avatar"]:
                data["avatar"] = Asset.from_path_hash(client, f"avatars/{data['id']}/{{}}", data["avatar"])
            else:
                data["avatar"] = Asset(client, f"{Asset.BASE}/embed/avatars/{int(data['discriminator']) % 5}")
        return data

    @property
    def tag(self) -> str:
        """Returns the user's Discord tag."""
        return f"{self.username}#{self.discriminator}"

    @property
    def mention(self) -> str:
        """Returns a string that would mention the user."""
        return f"<@{self.id}>"

    @property
    def display_name(self) -> str:
        """The users display name, will return nickname if one is set, otherwise will return username."""
        return self.username  # for duck-typing compatibility with Member

    @property
    def display_avatar(self) -> "Asset":
        """The users displayed avatar, will return `guild_avatar` if one is set, otherwise will return user avatar."""
        return self.avatar

    async def fetch_dm(self) -> "DM":
        """Fetch the DM channel associated with this user."""
        return await self._client.cache.fetch_dm_channel(self.id)  # noqa

    def get_dm(self) -> Optional["DM"]:
        """Get the DM channel associated with this user."""
        return self._client.cache.get_dm_channel(self.id)  # noqa

    @property
    def mutual_guilds(self) -> List["Guild"]:
        """
        Get a list of mutual guilds shared between this user and the client.

        !!! Note
            This will only be accurate if the guild members are cached internally
        """
        return [
            guild for guild in self._client.guilds if self._client.cache.get_member(guild_id=guild.id, user_id=self.id)
        ]

async inherited method send(self, content, embeds, embed, components, stickers, allowed_mentions, reply_to, files, file, tts, suppress_embeds, flags, delete_after, **kwargs)

Send a message.

Parameters:

Name Type Description Default
content Optional[str]

Message text content.

None
embeds Union[List[Union[Embed, dict]], Embed, dict]

Embedded rich content (up to 6000 characters).

None
embed Union[Embed, dict]

Embedded rich content (up to 6000 characters).

None
components Union[List[List[Union[BaseComponent, dict]]], List[Union[BaseComponent, dict]], BaseComponent, dict]

The components to include with the message.

None
stickers Union[List[Union[Sticker, Snowflake_Type]], Sticker, Snowflake_Type]

IDs of up to 3 stickers in the server to send in the message.

None
allowed_mentions Union[AllowedMentions, dict]

Allowed mentions for the message.

None
reply_to Union[MessageReference, Message, dict, Snowflake_Type]

Message to reference, must be from the same channel.

None
files Union[UPLOADABLE_TYPE, List[UPLOADABLE_TYPE]]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
file Optional[UPLOADABLE_TYPE]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
tts bool

Should this message use Text To Speech.

False
suppress_embeds bool

Should embeds be suppressed on this send

False
flags Union[int, MessageFlags]

Message flags to apply.

None
delete_after Optional[float]

Delete message after this many seconds.

None

Returns:

Type Description
Message

New message object that was sent.

Source code in naff/models/discord/user.py
async def send(
    self,
    content: Optional[str] = None,
    embeds: Optional[Union[List[Union["Embed", dict]], Union["Embed", dict]]] = None,
    embed: Optional[Union["Embed", dict]] = None,
    components: Optional[
        Union[List[List[Union["BaseComponent", dict]]], List[Union["BaseComponent", dict]], "BaseComponent", dict]
    ] = None,
    stickers: Optional[Union[List[Union["Sticker", "Snowflake_Type"]], "Sticker", "Snowflake_Type"]] = None,
    allowed_mentions: Optional[Union["AllowedMentions", dict]] = None,
    reply_to: Optional[Union["MessageReference", "Message", dict, "Snowflake_Type"]] = None,
    files: Optional[Union["UPLOADABLE_TYPE", List["UPLOADABLE_TYPE"]]] = None,
    file: Optional["UPLOADABLE_TYPE"] = None,
    tts: bool = False,
    suppress_embeds: bool = False,
    flags: Optional[Union[int, "MessageFlags"]] = None,
    delete_after: Optional[float] = None,
    **kwargs,
) -> "Message":
    """
    Send a message.

    Args:
        content: Message text content.
        embeds: Embedded rich content (up to 6000 characters).
        embed: Embedded rich content (up to 6000 characters).
        components: The components to include with the message.
        stickers: IDs of up to 3 stickers in the server to send in the message.
        allowed_mentions: Allowed mentions for the message.
        reply_to: Message to reference, must be from the same channel.
        files: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        file: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        tts: Should this message use Text To Speech.
        suppress_embeds: Should embeds be suppressed on this send
        flags: Message flags to apply.
        delete_after: Delete message after this many seconds.

    Returns:
        New message object that was sent.

    """
    if not content and not (embeds or embed) and not (files or file) and not stickers:
        raise errors.EmptyMessageException(
            "You cannot send a message without any content, embeds, files, or stickers"
        )

    if suppress_embeds:
        if isinstance(flags, int):
            flags = MessageFlags(flags)
        flags = flags | MessageFlags.SUPPRESS_EMBEDS

    message_payload = models.discord.message.process_message_payload(
        content=content,
        embeds=embeds or embed,
        components=components,
        stickers=stickers,
        allowed_mentions=allowed_mentions,
        reply_to=reply_to,
        tts=tts,
        flags=flags,
        **kwargs,
    )

    message_data = await self._send_http_request(message_payload, files=files or file)
    if message_data:
        message = self._client.cache.place_message_data(message_data)
        if delete_after:
            await message.delete(delay=delete_after)
        return message

inherited method update_from_dict(self, data)

Updates object attribute(s) with new json data received from discord api.

Parameters:

Name Type Description Default
data

The json data received from discord api.

required

Returns:

Type Description
~T

The updated object class instance.

Source code in naff/models/discord/user.py
def update_from_dict(self, data) -> T:
    data = self._process_dict(data, self._client)
    for key, value in self._filter_kwargs(data, self._get_keys()).items():
        # todo improve
        setattr(self, key, value)

    return self

property readonly tag: str

Returns the user's Discord tag.

property readonly mention: str

Returns a string that would mention the user.

inherited property readonly created_at: models.Timestamp

Returns a timestamp representing the date-time this discord object was created.

:Returns:

property readonly display_name: str

The users display name, will return nickname if one is set, otherwise will return username.

property readonly display_avatar: Asset

The users displayed avatar, will return guild_avatar if one is set, otherwise will return user avatar.

async method fetch_dm(self)

Fetch the DM channel associated with this user.

Source code in naff/models/discord/user.py
async def fetch_dm(self) -> "DM":
    """Fetch the DM channel associated with this user."""
    return await self._client.cache.fetch_dm_channel(self.id)  # noqa

method get_dm(self)

Get the DM channel associated with this user.

Source code in naff/models/discord/user.py
def get_dm(self) -> Optional["DM"]:
    """Get the DM channel associated with this user."""
    return self._client.cache.get_dm_channel(self.id)  # noqa

property readonly mutual_guilds: List[Guild]

Get a list of mutual guilds shared between this user and the client.

Note

This will only be accurate if the guild members are cached internally

inherited method to_dict(self)

Exports object into dictionary representation, ready to be sent to discord api.

Returns:

Type Description
Dict[str, Any]

The exported dictionary.

Source code in naff/models/discord/user.py
def to_dict(self) -> Dict[str, Any]:
    """
    Exports object into dictionary representation, ready to be sent to discord api.

    Returns:
        The exported dictionary.

    """
    self._check_object()
    return serializer.to_dict(self)

attrs class User (BaseUser)

Attr attributes:

Name Type Description
id int

Discord unique snowflake ID

username str

The user's username, not unique across the platform

discriminator int

The user's 4-digit discord-tag

avatar Asset

The user's default avatar

bot bool

Is this user a bot?

system bool

whether the user is an Official Discord System user (part of the urgent message system)

public_flags UserFlags

The flags associated with this user

premium_type PremiumTypes

The type of nitro subscription on a user's account

banner Optional[Asset]

The user's banner

accent_color Optional[Color]

The user's banner color

activities list

A list of activities the user is in

status Union[naff.models.discord.enums.Status, naff.client.const.Missing]

The user's status

Source code in naff/models/discord/user.py
@define()
class User(BaseUser):
    bot: bool = field(repr=True, default=False, metadata=docs("Is this user a bot?"))
    system: bool = field(
        default=False,
        metadata=docs("whether the user is an Official Discord System user (part of the urgent message system)"),
    )
    public_flags: "UserFlags" = field(
        repr=True, default=0, converter=UserFlags, metadata=docs("The flags associated with this user")
    )
    premium_type: "PremiumTypes" = field(
        default=0, converter=PremiumTypes, metadata=docs("The type of nitro subscription on a user's account")
    )

    banner: Optional["Asset"] = field(default=None, metadata=docs("The user's banner"))
    accent_color: Optional["Color"] = field(
        default=None,
        converter=optional_c(Color),
        metadata=docs("The user's banner color"),
    )
    activities: list[Activity] = field(
        factory=list,
        converter=list_converter(optional(Activity.from_dict)),
        metadata=docs("A list of activities the user is in"),
    )
    status: Absent[Status] = field(default=MISSING, metadata=docs("The user's status"), converter=optional(Status))

    @classmethod
    def _process_dict(cls, data: Dict[str, Any], client: "Client") -> Dict[str, Any]:
        data = super()._process_dict(data, client)
        if "banner" in data:
            data["banner"] = Asset.from_path_hash(client, f"banners/{data['id']}/{{}}", data["banner"])

        if data.get("premium_type", None) is None:
            data["premium_type"] = 0

        return data

    @property
    def member_instances(self) -> List["Member"]:
        """
        Returns the member object for all guilds both the bot and the user are in.

        !!! Note
            This will only be accurate if the guild members are cached internally
        """
        member_objs = [
            self._client.cache.get_member(guild_id=guild.id, user_id=self.id) for guild in self._client.guilds
        ]
        return [member for member in member_objs if member]

async inherited method send(self, content, embeds, embed, components, stickers, allowed_mentions, reply_to, files, file, tts, suppress_embeds, flags, delete_after, **kwargs)

Send a message.

Parameters:

Name Type Description Default
content Optional[str]

Message text content.

None
embeds Union[List[Union[Embed, dict]], Embed, dict]

Embedded rich content (up to 6000 characters).

None
embed Union[Embed, dict]

Embedded rich content (up to 6000 characters).

None
components Union[List[List[Union[BaseComponent, dict]]], List[Union[BaseComponent, dict]], BaseComponent, dict]

The components to include with the message.

None
stickers Union[List[Union[Sticker, Snowflake_Type]], Sticker, Snowflake_Type]

IDs of up to 3 stickers in the server to send in the message.

None
allowed_mentions Union[AllowedMentions, dict]

Allowed mentions for the message.

None
reply_to Union[MessageReference, Message, dict, Snowflake_Type]

Message to reference, must be from the same channel.

None
files Union[UPLOADABLE_TYPE, List[UPLOADABLE_TYPE]]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
file Optional[UPLOADABLE_TYPE]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
tts bool

Should this message use Text To Speech.

False
suppress_embeds bool

Should embeds be suppressed on this send

False
flags Union[int, MessageFlags]

Message flags to apply.

None
delete_after Optional[float]

Delete message after this many seconds.

None

Returns:

Type Description
Message

New message object that was sent.

Source code in naff/models/discord/user.py
async def send(
    self,
    content: Optional[str] = None,
    embeds: Optional[Union[List[Union["Embed", dict]], Union["Embed", dict]]] = None,
    embed: Optional[Union["Embed", dict]] = None,
    components: Optional[
        Union[List[List[Union["BaseComponent", dict]]], List[Union["BaseComponent", dict]], "BaseComponent", dict]
    ] = None,
    stickers: Optional[Union[List[Union["Sticker", "Snowflake_Type"]], "Sticker", "Snowflake_Type"]] = None,
    allowed_mentions: Optional[Union["AllowedMentions", dict]] = None,
    reply_to: Optional[Union["MessageReference", "Message", dict, "Snowflake_Type"]] = None,
    files: Optional[Union["UPLOADABLE_TYPE", List["UPLOADABLE_TYPE"]]] = None,
    file: Optional["UPLOADABLE_TYPE"] = None,
    tts: bool = False,
    suppress_embeds: bool = False,
    flags: Optional[Union[int, "MessageFlags"]] = None,
    delete_after: Optional[float] = None,
    **kwargs,
) -> "Message":
    """
    Send a message.

    Args:
        content: Message text content.
        embeds: Embedded rich content (up to 6000 characters).
        embed: Embedded rich content (up to 6000 characters).
        components: The components to include with the message.
        stickers: IDs of up to 3 stickers in the server to send in the message.
        allowed_mentions: Allowed mentions for the message.
        reply_to: Message to reference, must be from the same channel.
        files: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        file: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        tts: Should this message use Text To Speech.
        suppress_embeds: Should embeds be suppressed on this send
        flags: Message flags to apply.
        delete_after: Delete message after this many seconds.

    Returns:
        New message object that was sent.

    """
    if not content and not (embeds or embed) and not (files or file) and not stickers:
        raise errors.EmptyMessageException(
            "You cannot send a message without any content, embeds, files, or stickers"
        )

    if suppress_embeds:
        if isinstance(flags, int):
            flags = MessageFlags(flags)
        flags = flags | MessageFlags.SUPPRESS_EMBEDS

    message_payload = models.discord.message.process_message_payload(
        content=content,
        embeds=embeds or embed,
        components=components,
        stickers=stickers,
        allowed_mentions=allowed_mentions,
        reply_to=reply_to,
        tts=tts,
        flags=flags,
        **kwargs,
    )

    message_data = await self._send_http_request(message_payload, files=files or file)
    if message_data:
        message = self._client.cache.place_message_data(message_data)
        if delete_after:
            await message.delete(delay=delete_after)
        return message

inherited method update_from_dict(self, data)

Updates object attribute(s) with new json data received from discord api.

Parameters:

Name Type Description Default
data

The json data received from discord api.

required

Returns:

Type Description
~T

The updated object class instance.

Source code in naff/models/discord/user.py
def update_from_dict(self, data) -> T:
    data = self._process_dict(data, self._client)
    for key, value in self._filter_kwargs(data, self._get_keys()).items():
        # todo improve
        setattr(self, key, value)

    return self

inherited property readonly tag: str

Returns the user's Discord tag.

inherited property readonly mention: str

Returns a string that would mention the user.

inherited property readonly created_at: models.Timestamp

Returns a timestamp representing the date-time this discord object was created.

:Returns:

inherited property readonly display_name: str

The users display name, will return nickname if one is set, otherwise will return username.

inherited property readonly display_avatar: Asset

The users displayed avatar, will return guild_avatar if one is set, otherwise will return user avatar.

async inherited method fetch_dm(self)

Fetch the DM channel associated with this user.

Source code in naff/models/discord/user.py
async def fetch_dm(self) -> "DM":
    """Fetch the DM channel associated with this user."""
    return await self._client.cache.fetch_dm_channel(self.id)  # noqa

inherited method get_dm(self)

Get the DM channel associated with this user.

Source code in naff/models/discord/user.py
def get_dm(self) -> Optional["DM"]:
    """Get the DM channel associated with this user."""
    return self._client.cache.get_dm_channel(self.id)  # noqa

inherited property readonly mutual_guilds: List[Guild]

Get a list of mutual guilds shared between this user and the client.

Note

This will only be accurate if the guild members are cached internally

inherited method to_dict(self)

Exports object into dictionary representation, ready to be sent to discord api.

Returns:

Type Description
Dict[str, Any]

The exported dictionary.

Source code in naff/models/discord/user.py
def to_dict(self) -> Dict[str, Any]:
    """
    Exports object into dictionary representation, ready to be sent to discord api.

    Returns:
        The exported dictionary.

    """
    self._check_object()
    return serializer.to_dict(self)

property readonly member_instances: List[Member]

Returns the member object for all guilds both the bot and the user are in.

Note

This will only be accurate if the guild members are cached internally

attrs class NaffUser (User)

Attr attributes:

Name Type Description
id int

Discord unique snowflake ID

username str

The user's username, not unique across the platform

discriminator int

The user's 4-digit discord-tag

avatar Asset

The user's default avatar

bot bool

Is this user a bot?

system bool

whether the user is an Official Discord System user (part of the urgent message system)

public_flags UserFlags

The flags associated with this user

premium_type PremiumTypes

The type of nitro subscription on a user's account

banner Optional[Asset]

The user's banner

accent_color Optional[Color]

The user's banner color

activities list

A list of activities the user is in

status Union[naff.models.discord.enums.Status, naff.client.const.Missing]

The user's status

verified bool

Whether the email on this account has been verified

mfa_enabled bool

Whether the user has two factor enabled on their account

email Optional[str]

the user's email

locale Optional[str]

the user's chosen language option

flags UserFlags

the flags on a user's account

Source code in naff/models/discord/user.py
@define()
class NaffUser(User):
    verified: bool = field(repr=True, metadata={"docs": "Whether the email on this account has been verified"})
    mfa_enabled: bool = field(
        default=False, metadata={"docs": "Whether the user has two factor enabled on their account"}
    )
    email: Optional[str] = field(default=None, metadata={"docs": "the user's email"})  # needs special permissions?
    locale: Optional[str] = field(default=None, metadata={"docs": "the user's chosen language option"})
    bio: Optional[str] = field(default=None, metadata={"docs": ""})
    flags: "UserFlags" = field(default=0, converter=UserFlags, metadata={"docs": "the flags on a user's account"})

    _guild_ids: Set["Snowflake_Type"] = field(factory=set, metadata={"docs": "All the guilds the user is in"})

    def _add_guilds(self, guild_ids: Set["Snowflake_Type"]) -> None:
        """
        Add the guilds that the user is in to the internal reference.

        Args:
            guild_ids: The guild ids to add

        """
        self._guild_ids |= guild_ids

    @property
    def guilds(self) -> List["Guild"]:
        """The guilds the user is in."""
        return [self._client.cache.get_guild(g_id) for g_id in self._guild_ids]

    async def edit(self, username: Absent[str] = MISSING, avatar: Absent[UPLOADABLE_TYPE] = MISSING) -> None:
        """
        Edit the client's user.

        You can either change the username, or avatar, or both at once.
        `avatar` may be set to `None` to remove your bot's avatar

        ??? Hint "Example Usage:"
            ```python
            await self.user.edit(avatar="path_to_file")
            ```
            or
            ```python
            await self.user.edit(username="hello world")
            ```

        Args:
            username: The username you want to use
            avatar: The avatar to use. Can be a image file, path, or `bytes` (see example)

        Raises:
            TooManyChanges: If you change the profile too many times

        """
        payload = {}
        if username:
            payload["username"] = username
        if avatar:
            payload["avatar"] = to_image_data(avatar)
        elif avatar is None:
            payload["avatar"] = None

        try:
            resp = await self._client.http.modify_client_user(payload)
        except HTTPException:
            raise TooManyChanges(
                "You have changed your profile too frequently, you need to wait a while before trying again."
            ) from None
        if resp:
            self._client.cache.place_user_data(resp)

async inherited method send(self, content, embeds, embed, components, stickers, allowed_mentions, reply_to, files, file, tts, suppress_embeds, flags, delete_after, **kwargs)

Send a message.

Parameters:

Name Type Description Default
content Optional[str]

Message text content.

None
embeds Union[List[Union[Embed, dict]], Embed, dict]

Embedded rich content (up to 6000 characters).

None
embed Union[Embed, dict]

Embedded rich content (up to 6000 characters).

None
components Union[List[List[Union[BaseComponent, dict]]], List[Union[BaseComponent, dict]], BaseComponent, dict]

The components to include with the message.

None
stickers Union[List[Union[Sticker, Snowflake_Type]], Sticker, Snowflake_Type]

IDs of up to 3 stickers in the server to send in the message.

None
allowed_mentions Union[AllowedMentions, dict]

Allowed mentions for the message.

None
reply_to Union[MessageReference, Message, dict, Snowflake_Type]

Message to reference, must be from the same channel.

None
files Union[UPLOADABLE_TYPE, List[UPLOADABLE_TYPE]]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
file Optional[UPLOADABLE_TYPE]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
tts bool

Should this message use Text To Speech.

False
suppress_embeds bool

Should embeds be suppressed on this send

False
flags Union[int, MessageFlags]

Message flags to apply.

None
delete_after Optional[float]

Delete message after this many seconds.

None

Returns:

Type Description
Message

New message object that was sent.

Source code in naff/models/discord/user.py
async def send(
    self,
    content: Optional[str] = None,
    embeds: Optional[Union[List[Union["Embed", dict]], Union["Embed", dict]]] = None,
    embed: Optional[Union["Embed", dict]] = None,
    components: Optional[
        Union[List[List[Union["BaseComponent", dict]]], List[Union["BaseComponent", dict]], "BaseComponent", dict]
    ] = None,
    stickers: Optional[Union[List[Union["Sticker", "Snowflake_Type"]], "Sticker", "Snowflake_Type"]] = None,
    allowed_mentions: Optional[Union["AllowedMentions", dict]] = None,
    reply_to: Optional[Union["MessageReference", "Message", dict, "Snowflake_Type"]] = None,
    files: Optional[Union["UPLOADABLE_TYPE", List["UPLOADABLE_TYPE"]]] = None,
    file: Optional["UPLOADABLE_TYPE"] = None,
    tts: bool = False,
    suppress_embeds: bool = False,
    flags: Optional[Union[int, "MessageFlags"]] = None,
    delete_after: Optional[float] = None,
    **kwargs,
) -> "Message":
    """
    Send a message.

    Args:
        content: Message text content.
        embeds: Embedded rich content (up to 6000 characters).
        embed: Embedded rich content (up to 6000 characters).
        components: The components to include with the message.
        stickers: IDs of up to 3 stickers in the server to send in the message.
        allowed_mentions: Allowed mentions for the message.
        reply_to: Message to reference, must be from the same channel.
        files: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        file: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        tts: Should this message use Text To Speech.
        suppress_embeds: Should embeds be suppressed on this send
        flags: Message flags to apply.
        delete_after: Delete message after this many seconds.

    Returns:
        New message object that was sent.

    """
    if not content and not (embeds or embed) and not (files or file) and not stickers:
        raise errors.EmptyMessageException(
            "You cannot send a message without any content, embeds, files, or stickers"
        )

    if suppress_embeds:
        if isinstance(flags, int):
            flags = MessageFlags(flags)
        flags = flags | MessageFlags.SUPPRESS_EMBEDS

    message_payload = models.discord.message.process_message_payload(
        content=content,
        embeds=embeds or embed,
        components=components,
        stickers=stickers,
        allowed_mentions=allowed_mentions,
        reply_to=reply_to,
        tts=tts,
        flags=flags,
        **kwargs,
    )

    message_data = await self._send_http_request(message_payload, files=files or file)
    if message_data:
        message = self._client.cache.place_message_data(message_data)
        if delete_after:
            await message.delete(delay=delete_after)
        return message

inherited method update_from_dict(self, data)

Updates object attribute(s) with new json data received from discord api.

Parameters:

Name Type Description Default
data

The json data received from discord api.

required

Returns:

Type Description
~T

The updated object class instance.

Source code in naff/models/discord/user.py
def update_from_dict(self, data) -> T:
    data = self._process_dict(data, self._client)
    for key, value in self._filter_kwargs(data, self._get_keys()).items():
        # todo improve
        setattr(self, key, value)

    return self

inherited property readonly tag: str

Returns the user's Discord tag.

inherited property readonly mention: str

Returns a string that would mention the user.

inherited property readonly created_at: models.Timestamp

Returns a timestamp representing the date-time this discord object was created.

:Returns:

inherited property readonly display_name: str

The users display name, will return nickname if one is set, otherwise will return username.

inherited property readonly display_avatar: Asset

The users displayed avatar, will return guild_avatar if one is set, otherwise will return user avatar.

async inherited method fetch_dm(self)

Fetch the DM channel associated with this user.

Source code in naff/models/discord/user.py
async def fetch_dm(self) -> "DM":
    """Fetch the DM channel associated with this user."""
    return await self._client.cache.fetch_dm_channel(self.id)  # noqa

inherited method get_dm(self)

Get the DM channel associated with this user.

Source code in naff/models/discord/user.py
def get_dm(self) -> Optional["DM"]:
    """Get the DM channel associated with this user."""
    return self._client.cache.get_dm_channel(self.id)  # noqa

inherited property readonly mutual_guilds: List[Guild]

Get a list of mutual guilds shared between this user and the client.

Note

This will only be accurate if the guild members are cached internally

inherited method to_dict(self)

Exports object into dictionary representation, ready to be sent to discord api.

Returns:

Type Description
Dict[str, Any]

The exported dictionary.

Source code in naff/models/discord/user.py
def to_dict(self) -> Dict[str, Any]:
    """
    Exports object into dictionary representation, ready to be sent to discord api.

    Returns:
        The exported dictionary.

    """
    self._check_object()
    return serializer.to_dict(self)

inherited property readonly member_instances: List[Member]

Returns the member object for all guilds both the bot and the user are in.

Note

This will only be accurate if the guild members are cached internally

property readonly guilds: List[Guild]

The guilds the user is in.

async method edit(self, username, avatar)

Edit the client's user.

You can either change the username, or avatar, or both at once. avatar may be set to None to remove your bot's avatar

Example Usage:

1
await self.user.edit(avatar="path_to_file")
or
1
await self.user.edit(username="hello world")

Parameters:

Name Type Description Default
username Union[str, naff.client.const.Missing]

The username you want to use

Missing
avatar Union[naff.models.discord.file.File, io.IOBase, BinaryIO, pathlib.Path, str, naff.client.const.Missing]

The avatar to use. Can be a image file, path, or bytes (see example)

Missing

Exceptions:

Type Description
TooManyChanges

If you change the profile too many times

Source code in naff/models/discord/user.py
async def edit(self, username: Absent[str] = MISSING, avatar: Absent[UPLOADABLE_TYPE] = MISSING) -> None:
    """
    Edit the client's user.

    You can either change the username, or avatar, or both at once.
    `avatar` may be set to `None` to remove your bot's avatar

    ??? Hint "Example Usage:"
        ```python
        await self.user.edit(avatar="path_to_file")
        ```
        or
        ```python
        await self.user.edit(username="hello world")
        ```

    Args:
        username: The username you want to use
        avatar: The avatar to use. Can be a image file, path, or `bytes` (see example)

    Raises:
        TooManyChanges: If you change the profile too many times

    """
    payload = {}
    if username:
        payload["username"] = username
    if avatar:
        payload["avatar"] = to_image_data(avatar)
    elif avatar is None:
        payload["avatar"] = None

    try:
        resp = await self._client.http.modify_client_user(payload)
    except HTTPException:
        raise TooManyChanges(
            "You have changed your profile too frequently, you need to wait a while before trying again."
        ) from None
    if resp:
        self._client.cache.place_user_data(resp)

attrs class Member (DiscordObject, _SendDMMixin)

Attr attributes:

Name Type Description
id int

Discord unique snowflake ID

bot bool

Is this user a bot?

nick Optional[str]

The user's nickname in this guild'

deaf bool

Has this user been deafened in voice channels?

mute bool

Has this user been muted in voice channels?

joined_at Timestamp

When the user joined this guild

premium_since Optional[Timestamp]

When the user started boosting the guild

pending Optional[bool]

Whether the user has not passed guild's membership screening requirements

guild_avatar Asset

The user's guild avatar

communication_disabled_until Optional[Timestamp]

When a member's timeout will expire, None or a time in the past if the user is not timed out

Source code in naff/models/discord/user.py
@define()
class Member(DiscordObject, _SendDMMixin):
    bot: bool = field(repr=True, default=False, metadata=docs("Is this user a bot?"))
    nick: Optional[str] = field(repr=True, default=None, metadata=docs("The user's nickname in this guild'"))
    deaf: bool = field(default=False, metadata=docs("Has this user been deafened in voice channels?"))
    mute: bool = field(default=False, metadata=docs("Has this user been muted in voice channels?"))
    joined_at: "Timestamp" = field(converter=timestamp_converter, metadata=docs("When the user joined this guild"))
    premium_since: Optional["Timestamp"] = field(
        default=None,
        converter=optional_c(timestamp_converter),
        metadata=docs("When the user started boosting the guild"),
    )
    pending: Optional[bool] = field(
        default=None, metadata=docs("Whether the user has **not** passed guild's membership screening requirements")
    )
    guild_avatar: "Asset" = field(default=None, metadata=docs("The user's guild avatar"))
    communication_disabled_until: Optional["Timestamp"] = field(
        default=None,
        converter=optional_c(timestamp_converter),
        metadata=docs("When a member's timeout will expire, `None` or a time in the past if the user is not timed out"),
    )

    _guild_id: "Snowflake_Type" = field(repr=True, metadata=docs("The ID of the guild"))
    _role_ids: List["Snowflake_Type"] = field(
        factory=list, converter=list_converter(to_snowflake), metadata=docs("The roles IDs this user has")
    )

    @classmethod
    def _process_dict(cls, data: Dict[str, Any], client: "Client") -> Dict[str, Any]:
        if "user" in data:
            user_data = data.pop("user")
            client.cache.place_user_data(user_data)
            data["id"] = user_data["id"]
            data["bot"] = user_data.get("bot", False)
        elif "member" in data:
            member_data = data.pop("member")
            client.cache.place_user_data(data)
            member_data["id"] = data["id"]
            member_data["bot"] = data.get("bot", False)
            if "guild_id" not in member_data:
                member_data["guild_id"] = data.get("guild_id")
            data = member_data
        if data.get("avatar"):
            try:
                data["guild_avatar"] = Asset.from_path_hash(
                    client, f"guilds/{data['guild_id']}/users/{data['id']}/avatars/{{}}", data.pop("avatar", None)
                )
            except Exception as e:
                logger.warning(
                    f"[DEBUG NEEDED - REPORT THIS] Incomplete dictionary has been passed to member object: {e}"
                )
                raise

        data["role_ids"] = data.pop("roles", [])

        return data

    def update_from_dict(self, data) -> None:
        if "guild_id" not in data:
            data["guild_id"] = self._guild_id
        data["_role_ids"] = data.pop("roles", [])
        return super().update_from_dict(data)

    @property
    def user(self) -> "User":
        """Returns this member's user object."""
        return self._client.cache.get_user(self.id)

    def __str__(self) -> str:
        return self.user.tag

    def __getattr__(self, name: str) -> Any:
        # this allows for transparent access to user attributes
        try:
            return getattr(self.user, name)
        except AttributeError as e:
            raise AttributeError(f"Neither `User` or `Member` have attribute {name}") from e

    @property
    def nickname(self) -> str:
        """Alias for nick."""
        return self.nick

    @nickname.setter
    def nickname(self, nickname: str) -> None:
        """Sets the member's nickname."""
        self.nick = nickname

    @property
    def guild(self) -> "Guild":
        """The guild object this member is from."""
        return self._client.cache.get_guild(self._guild_id)

    @property
    def roles(self) -> List["Role"]:
        """The roles this member has."""
        return [r for r in self.guild.roles if r.id in self._role_ids]

    @property
    def top_role(self) -> "Role":
        """The member's top most role."""
        return max(self.roles, key=lambda x: x.position) if self.roles else self.guild.default_role

    @property
    def display_name(self) -> str:
        """The users display name, will return nickname if one is set, otherwise will return username."""
        return self.nickname or self.username

    @property
    def display_avatar(self) -> "Asset":
        """The users displayed avatar, will return `guild_avatar` if one is set, otherwise will return user avatar."""
        return self.guild_avatar or self.user.avatar

    @property
    def premium(self) -> bool:
        """Is this member a server booster?"""
        return self.premium_since is not None

    @property
    def guild_permissions(self) -> Permissions:
        """
        Returns the permissions this member has in the guild.

        Returns:
            Permission data

        """
        guild = self.guild
        if guild.is_owner(self):
            return Permissions.ALL

        permissions = guild.default_role.permissions  # get @everyone role

        for role in self.roles:
            permissions |= role.permissions

        if Permissions.ADMINISTRATOR in permissions:
            return Permissions.ALL

        return permissions

    @property
    def voice(self) -> Optional["VoiceState"]:
        """Returns the voice state of this user if any."""
        return self._client.cache.get_voice_state(self.id)

    def has_permission(self, *permissions: Permissions) -> bool:
        """
        Checks if the member has all the given permission(s).

        ??? Hint "Example Usage:"
            Two different styles can be used to call this method.

            ```python
            member.has_permission(Permissions.KICK_MEMBERS, Permissions.BAN_MEMBERS)
            ```
            or
            ```python
            member.has_permission(Permissions.KICK_MEMBERS | Permissions.BAN_MEMBERS)
            ```

            If `member` has both permissions, `True` gets returned.

        Args:
            permissions: The permission(s) to check whether the user has it.

        """
        # Get the user's permissions
        guild_permissions = self.guild_permissions

        # Check all permissions separately
        for permission in permissions:
            if permission not in guild_permissions:
                return False
        return True

    def channel_permissions(self, channel: "TYPE_GUILD_CHANNEL") -> Permissions:
        """
        Returns the permissions this member has in a channel.

        Args:
            channel: The channel in question

        Returns:
            Permissions data

        ??? note
            This method is used in `Channel.permissions_for`

        """
        permissions = self.guild_permissions

        if Permissions.ADMINISTRATOR in permissions:
            return Permissions.ALL

        overwrites = tuple(
            filter(
                lambda overwrite: overwrite.id in (self._guild_id, self.id, *self._role_ids),
                channel.permission_overwrites,
            )
        )

        for everyone_overwrite in filter(lambda overwrite: overwrite.id == self._guild_id, overwrites):
            permissions &= ~everyone_overwrite.deny
            permissions |= everyone_overwrite.allow

        for role_overwrite in filter(lambda overwrite: overwrite.id not in (self._guild_id, self.id), overwrites):
            permissions &= ~role_overwrite.deny
            permissions |= role_overwrite.allow

        for member_overwrite in filter(lambda overwrite: overwrite.id == self.id, overwrites):
            permissions &= ~member_overwrite.deny
            permissions |= member_overwrite.allow

        return permissions

    async def edit_nickname(self, new_nickname: Absent[str] = MISSING, reason: Absent[str] = MISSING) -> None:
        """
        Change the user's nickname.

        Args:
            new_nickname: The new nickname to apply
            reason: The reason for this change

        Note:
            Leave `new_nickname` empty to clean user's nickname

        """
        if self.id == self._client.user.id:
            await self._client.http.modify_current_member(self._guild_id, nickname=new_nickname, reason=reason)
        else:
            await self._client.http.modify_guild_member(self._guild_id, self.id, nickname=new_nickname, reason=reason)

    async def add_role(self, role: Union[Snowflake_Type, Role], reason: Absent[str] = MISSING) -> None:
        """
        Add a role to this member.

        Args:
            role: The role to add
            reason: The reason for adding this role

        """
        role = to_snowflake(role)
        await self._client.http.add_guild_member_role(self._guild_id, self.id, role, reason=reason)
        self._role_ids.append(role)

    async def add_roles(self, roles: Iterable[Union[Snowflake_Type, Role]], reason: Absent[str] = MISSING) -> None:
        """
        Atomically add multiple roles to this member.

        Args:
            roles: The roles to add
            reason: The reason for adding the roles

        """
        new_roles = set(self._role_ids) | {to_snowflake(r) for r in roles}
        await self.edit(roles=new_roles, reason=reason)

    async def remove_role(self, role: Union[Snowflake_Type, Role], reason: Absent[str] = MISSING) -> None:
        """
        Remove a role from this user.

        Args:
            role: The role to remove
            reason: The reason for this removal

        """
        role = to_snowflake(role)
        await self._client.http.remove_guild_member_role(self._guild_id, self.id, role, reason=reason)
        try:
            self._role_ids.remove(role)
        except ValueError:
            pass

    async def remove_roles(self, roles: Iterable[Union[Snowflake_Type, Role]], reason: Absent[str] = MISSING) -> None:
        """
        Atomically remove multiple roles from this member.

        Args:
            roles: The roles to remove
            reason: The reason for removing the roles

        """
        new_roles = set(self._role_ids) - {to_snowflake(r) for r in roles}
        await self.edit(roles=new_roles, reason=reason)

    def has_role(self, *roles: Union[Snowflake_Type, Role]) -> bool:
        """
        Checks if the user has the given role(s).

        Args:
            roles: The role(s) to check whether the user has it.

        """
        return all(to_snowflake(role) in self._role_ids for role in roles)

    async def timeout(
        self,
        communication_disabled_until: Union["Timestamp", datetime, int, float, str, None],
        reason: Absent[str] = MISSING,
    ) -> dict:
        """
        Disable a members communication for a given time.

        Args:
            communication_disabled_until: The time until the user can communicate again
            reason: The reason for this timeout

        """
        if isinstance(communication_disabled_until, (datetime, int, float, str)):
            communication_disabled_until = timestamp_converter(communication_disabled_until)

        self.communication_disabled_until = communication_disabled_until

        return await self._client.http.modify_guild_member(
            self._guild_id,
            self.id,
            communication_disabled_until=communication_disabled_until,
            reason=reason,
        )

    async def move(self, channel_id: "Snowflake_Type") -> None:
        """
        Moves the member to a different voice channel.

        Args:
            channel_id: The voice channel to move the member to

        """
        await self._client.http.modify_guild_member(self._guild_id, self.id, channel_id=channel_id)

    async def edit(
        self,
        *,
        nickname: Absent[str] = MISSING,
        roles: Absent[Iterable["Snowflake_Type"]] = MISSING,
        mute: Absent[bool] = MISSING,
        deaf: Absent[bool] = MISSING,
        channel_id: Absent["Snowflake_Type"] = MISSING,
        communication_disabled_until: Absent[Union["Timestamp", None]] = MISSING,
        reason: Absent[str] = MISSING,
    ) -> None:
        """
        Modify attrbutes of this guild member.

        Args:
            nickname: Value to set users nickname to
            roles: Array of role ids the member is assigned
            mute: Whether the user is muted in voice channels. Will throw a 400 if the user is not in a voice channel
            deaf: Whether the user is deafened in voice channels
            channel_id: id of channel to move user to (if they are connected to voice)
            communication_disabled_until: 	when the user's timeout will expire and the user will be able to communicate in the guild again
            reason: An optional reason for the audit log
        """
        await self._client.http.modify_guild_member(
            self._guild_id,
            self.id,
            nickname=nickname,
            roles=roles,
            mute=mute,
            deaf=deaf,
            channel_id=channel_id,
            communication_disabled_until=communication_disabled_until,
            reason=reason,
        )

    async def kick(self, reason: Absent[str] = MISSING) -> None:
        """
        Remove a member from the guild.

        Args:
            reason: The reason for this removal

        """
        await self._client.http.remove_guild_member(self._guild_id, self.id, reason=reason)

    async def ban(self, delete_message_days: int = 0, reason: Absent[str] = MISSING) -> None:
        """
        Ban a member from the guild.

        Args:
            delete_message_days: The number of days of messages to delete
            reason: The reason for this ban

        """
        await self._client.http.create_guild_ban(self._guild_id, self.id, delete_message_days, reason=reason)

async inherited method send(self, content, embeds, embed, components, stickers, allowed_mentions, reply_to, files, file, tts, suppress_embeds, flags, delete_after, **kwargs)

Send a message.

Parameters:

Name Type Description Default
content Optional[str]

Message text content.

None
embeds Union[List[Union[Embed, dict]], Embed, dict]

Embedded rich content (up to 6000 characters).

None
embed Union[Embed, dict]

Embedded rich content (up to 6000 characters).

None
components Union[List[List[Union[BaseComponent, dict]]], List[Union[BaseComponent, dict]], BaseComponent, dict]

The components to include with the message.

None
stickers Union[List[Union[Sticker, Snowflake_Type]], Sticker, Snowflake_Type]

IDs of up to 3 stickers in the server to send in the message.

None
allowed_mentions Union[AllowedMentions, dict]

Allowed mentions for the message.

None
reply_to Union[MessageReference, Message, dict, Snowflake_Type]

Message to reference, must be from the same channel.

None
files Union[UPLOADABLE_TYPE, List[UPLOADABLE_TYPE]]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
file Optional[UPLOADABLE_TYPE]

Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.

None
tts bool

Should this message use Text To Speech.

False
suppress_embeds bool

Should embeds be suppressed on this send

False
flags Union[int, MessageFlags]

Message flags to apply.

None
delete_after Optional[float]

Delete message after this many seconds.

None

Returns:

Type Description
Message

New message object that was sent.

Source code in naff/models/discord/user.py
async def send(
    self,
    content: Optional[str] = None,
    embeds: Optional[Union[List[Union["Embed", dict]], Union["Embed", dict]]] = None,
    embed: Optional[Union["Embed", dict]] = None,
    components: Optional[
        Union[List[List[Union["BaseComponent", dict]]], List[Union["BaseComponent", dict]], "BaseComponent", dict]
    ] = None,
    stickers: Optional[Union[List[Union["Sticker", "Snowflake_Type"]], "Sticker", "Snowflake_Type"]] = None,
    allowed_mentions: Optional[Union["AllowedMentions", dict]] = None,
    reply_to: Optional[Union["MessageReference", "Message", dict, "Snowflake_Type"]] = None,
    files: Optional[Union["UPLOADABLE_TYPE", List["UPLOADABLE_TYPE"]]] = None,
    file: Optional["UPLOADABLE_TYPE"] = None,
    tts: bool = False,
    suppress_embeds: bool = False,
    flags: Optional[Union[int, "MessageFlags"]] = None,
    delete_after: Optional[float] = None,
    **kwargs,
) -> "Message":
    """
    Send a message.

    Args:
        content: Message text content.
        embeds: Embedded rich content (up to 6000 characters).
        embed: Embedded rich content (up to 6000 characters).
        components: The components to include with the message.
        stickers: IDs of up to 3 stickers in the server to send in the message.
        allowed_mentions: Allowed mentions for the message.
        reply_to: Message to reference, must be from the same channel.
        files: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        file: Files to send, the path, bytes or File() instance, defaults to None. You may have up to 10 files.
        tts: Should this message use Text To Speech.
        suppress_embeds: Should embeds be suppressed on this send
        flags: Message flags to apply.
        delete_after: Delete message after this many seconds.

    Returns:
        New message object that was sent.

    """
    if not content and not (embeds or embed) and not (files or file) and not stickers:
        raise errors.EmptyMessageException(
            "You cannot send a message without any content, embeds, files, or stickers"
        )

    if suppress_embeds:
        if isinstance(flags, int):
            flags = MessageFlags(flags)
        flags = flags | MessageFlags.SUPPRESS_EMBEDS

    message_payload = models.discord.message.process_message_payload(
        content=content,
        embeds=embeds or embed,
        components=components,
        stickers=stickers,
        allowed_mentions=allowed_mentions,
        reply_to=reply_to,
        tts=tts,
        flags=flags,
        **kwargs,
    )

    message_data = await self._send_http_request(message_payload, files=files or file)
    if message_data:
        message = self._client.cache.place_message_data(message_data)
        if delete_after:
            await message.delete(delay=delete_after)
        return message

inherited property readonly created_at: models.Timestamp

Returns a timestamp representing the date-time this discord object was created.

:Returns:

inherited method to_dict(self)

Exports object into dictionary representation, ready to be sent to discord api.

Returns:

Type Description
Dict[str, Any]

The exported dictionary.

Source code in naff/models/discord/user.py
def to_dict(self) -> Dict[str, Any]:
    """
    Exports object into dictionary representation, ready to be sent to discord api.

    Returns:
        The exported dictionary.

    """
    self._check_object()
    return serializer.to_dict(self)

method update_from_dict(self, data)

Updates object attribute(s) with new json data received from discord api.

Parameters:

Name Type Description Default
data

The json data received from discord api.

required

Returns:

Type Description
None

The updated object class instance.

Source code in naff/models/discord/user.py
def update_from_dict(self, data) -> None:
    if "guild_id" not in data:
        data["guild_id"] = self._guild_id
    data["_role_ids"] = data.pop("roles", [])
    return super().update_from_dict(data)

property readonly user: User

Returns this member's user object.

property writable nickname: str

Alias for nick.

property readonly guild: Guild

The guild object this member is from.

property readonly roles: List[Role]

The roles this member has.

property readonly top_role: Role

The member's top most role.

property readonly display_name: str

The users display name, will return nickname if one is set, otherwise will return username.

property readonly display_avatar: Asset

The users displayed avatar, will return guild_avatar if one is set, otherwise will return user avatar.

property readonly premium: bool

Is this member a server booster?

property readonly guild_permissions: Permissions

Returns the permissions this member has in the guild.

Returns:

Type Description
Permissions

Permission data

property readonly voice: Optional[VoiceState]

Returns the voice state of this user if any.

method has_permission(self, *permissions)

Checks if the member has all the given permission(s).

Example Usage:

Two different styles can be used to call this method.

1
member.has_permission(Permissions.KICK_MEMBERS, Permissions.BAN_MEMBERS)
or
1
member.has_permission(Permissions.KICK_MEMBERS | Permissions.BAN_MEMBERS)

If member has both permissions, True gets returned.

Parameters:

Name Type Description Default
permissions Permissions

The permission(s) to check whether the user has it.

()
Source code in naff/models/discord/user.py
def has_permission(self, *permissions: Permissions) -> bool:
    """
    Checks if the member has all the given permission(s).

    ??? Hint "Example Usage:"
        Two different styles can be used to call this method.

        ```python
        member.has_permission(Permissions.KICK_MEMBERS, Permissions.BAN_MEMBERS)
        ```
        or
        ```python
        member.has_permission(Permissions.KICK_MEMBERS | Permissions.BAN_MEMBERS)
        ```

        If `member` has both permissions, `True` gets returned.

    Args:
        permissions: The permission(s) to check whether the user has it.

    """
    # Get the user's permissions
    guild_permissions = self.guild_permissions

    # Check all permissions separately
    for permission in permissions:
        if permission not in guild_permissions:
            return False
    return True

method channel_permissions(self, channel)

Returns the permissions this member has in a channel.

Parameters:

Name Type Description Default
channel TYPE_GUILD_CHANNEL

The channel in question

required

Returns:

Type Description
Permissions

Permissions data

Note

This method is used in Channel.permissions_for

Source code in naff/models/discord/user.py
def channel_permissions(self, channel: "TYPE_GUILD_CHANNEL") -> Permissions:
    """
    Returns the permissions this member has in a channel.

    Args:
        channel: The channel in question

    Returns:
        Permissions data

    ??? note
        This method is used in `Channel.permissions_for`

    """
    permissions = self.guild_permissions

    if Permissions.ADMINISTRATOR in permissions:
        return Permissions.ALL

    overwrites = tuple(
        filter(
            lambda overwrite: overwrite.id in (self._guild_id, self.id, *self._role_ids),
            channel.permission_overwrites,
        )
    )

    for everyone_overwrite in filter(lambda overwrite: overwrite.id == self._guild_id, overwrites):
        permissions &= ~everyone_overwrite.deny
        permissions |= everyone_overwrite.allow

    for role_overwrite in filter(lambda overwrite: overwrite.id not in (self._guild_id, self.id), overwrites):
        permissions &= ~role_overwrite.deny
        permissions |= role_overwrite.allow

    for member_overwrite in filter(lambda overwrite: overwrite.id == self.id, overwrites):
        permissions &= ~member_overwrite.deny
        permissions |= member_overwrite.allow

    return permissions

async method edit_nickname(self, new_nickname, reason)

Change the user's nickname.

Parameters:

Name Type Description Default
new_nickname Union[str, naff.client.const.Missing]

The new nickname to apply

Missing
reason Union[str, naff.client.const.Missing]

The reason for this change

Missing

Note

Leave new_nickname empty to clean user's nickname

Source code in naff/models/discord/user.py
async def edit_nickname(self, new_nickname: Absent[str] = MISSING, reason: Absent[str] = MISSING) -> None:
    """
    Change the user's nickname.

    Args:
        new_nickname: The new nickname to apply
        reason: The reason for this change

    Note:
        Leave `new_nickname` empty to clean user's nickname

    """
    if self.id == self._client.user.id:
        await self._client.http.modify_current_member(self._guild_id, nickname=new_nickname, reason=reason)
    else:
        await self._client.http.modify_guild_member(self._guild_id, self.id, nickname=new_nickname, reason=reason)

async method add_role(self, role, reason)

Add a role to this member.

Parameters:

Name Type Description Default
role Union[int, str, SnowflakeObject, SupportsInt, naff.models.discord.role.Role]

The role to add

required
reason Union[str, naff.client.const.Missing]

The reason for adding this role

Missing
Source code in naff/models/discord/user.py
async def add_role(self, role: Union[Snowflake_Type, Role], reason: Absent[str] = MISSING) -> None:
    """
    Add a role to this member.

    Args:
        role: The role to add
        reason: The reason for adding this role

    """
    role = to_snowflake(role)
    await self._client.http.add_guild_member_role(self._guild_id, self.id, role, reason=reason)
    self._role_ids.append(role)

async method add_roles(self, roles, reason)

Atomically add multiple roles to this member.

Parameters:

Name Type Description Default
roles Iterable[Union[int, str, SnowflakeObject, SupportsInt, naff.models.discord.role.Role]]

The roles to add

required
reason Union[str, naff.client.const.Missing]

The reason for adding the roles

Missing
Source code in naff/models/discord/user.py
async def add_roles(self, roles: Iterable[Union[Snowflake_Type, Role]], reason: Absent[str] = MISSING) -> None:
    """
    Atomically add multiple roles to this member.

    Args:
        roles: The roles to add
        reason: The reason for adding the roles

    """
    new_roles = set(self._role_ids) | {to_snowflake(r) for r in roles}
    await self.edit(roles=new_roles, reason=reason)

async method remove_role(self, role, reason)

Remove a role from this user.

Parameters:

Name Type Description Default
role Union[int, str, SnowflakeObject, SupportsInt, naff.models.discord.role.Role]

The role to remove

required
reason Union[str, naff.client.const.Missing]

The reason for this removal

Missing
Source code in naff/models/discord/user.py
async def remove_role(self, role: Union[Snowflake_Type, Role], reason: Absent[str] = MISSING) -> None:
    """
    Remove a role from this user.

    Args:
        role: The role to remove
        reason: The reason for this removal

    """
    role = to_snowflake(role)
    await self._client.http.remove_guild_member_role(self._guild_id, self.id, role, reason=reason)
    try:
        self._role_ids.remove(role)
    except ValueError:
        pass

async method remove_roles(self, roles, reason)

Atomically remove multiple roles from this member.

Parameters:

Name Type Description Default
roles Iterable[Union[int, str, SnowflakeObject, SupportsInt, naff.models.discord.role.Role]]

The roles to remove

required
reason Union[str, naff.client.const.Missing]

The reason for removing the roles

Missing
Source code in naff/models/discord/user.py
async def remove_roles(self, roles: Iterable[Union[Snowflake_Type, Role]], reason: Absent[str] = MISSING) -> None:
    """
    Atomically remove multiple roles from this member.

    Args:
        roles: The roles to remove
        reason: The reason for removing the roles

    """
    new_roles = set(self._role_ids) - {to_snowflake(r) for r in roles}
    await self.edit(roles=new_roles, reason=reason)

method has_role(self, *roles)

Checks if the user has the given role(s).

Parameters:

Name Type Description Default
roles Union[int, str, SnowflakeObject, SupportsInt, naff.models.discord.role.Role]

The role(s) to check whether the user has it.

()
Source code in naff/models/discord/user.py
def has_role(self, *roles: Union[Snowflake_Type, Role]) -> bool:
    """
    Checks if the user has the given role(s).

    Args:
        roles: The role(s) to check whether the user has it.

    """
    return all(to_snowflake(role) in self._role_ids for role in roles)

async method timeout(self, communication_disabled_until, reason)

Disable a members communication for a given time.

Parameters:

Name Type Description Default
communication_disabled_until Union[Timestamp, datetime.datetime, int, float, str]

The time until the user can communicate again

required
reason Union[str, naff.client.const.Missing]

The reason for this timeout

Missing
Source code in naff/models/discord/user.py
async def timeout(
    self,
    communication_disabled_until: Union["Timestamp", datetime, int, float, str, None],
    reason: Absent[str] = MISSING,
) -> dict:
    """
    Disable a members communication for a given time.

    Args:
        communication_disabled_until: The time until the user can communicate again
        reason: The reason for this timeout

    """
    if isinstance(communication_disabled_until, (datetime, int, float, str)):
        communication_disabled_until = timestamp_converter(communication_disabled_until)

    self.communication_disabled_until = communication_disabled_until

    return await self._client.http.modify_guild_member(
        self._guild_id,
        self.id,
        communication_disabled_until=communication_disabled_until,
        reason=reason,
    )

async method move(self, channel_id)

Moves the member to a different voice channel.

Parameters:

Name Type Description Default
channel_id Snowflake_Type

The voice channel to move the member to

required
Source code in naff/models/discord/user.py
async def move(self, channel_id: "Snowflake_Type") -> None:
    """
    Moves the member to a different voice channel.

    Args:
        channel_id: The voice channel to move the member to

    """
    await self._client.http.modify_guild_member(self._guild_id, self.id, channel_id=channel_id)

async method edit(self, *, nickname, roles, mute, deaf, channel_id, communication_disabled_until, reason)

Modify attrbutes of this guild member.

Parameters:

Name Type Description Default
nickname Union[str, naff.client.const.Missing]

Value to set users nickname to

Missing
roles Union[Iterable[Snowflake_Type], naff.client.const.Missing]

Array of role ids the member is assigned

Missing
mute Union[bool, naff.client.const.Missing]

Whether the user is muted in voice channels. Will throw a 400 if the user is not in a voice channel

Missing
deaf Union[bool, naff.client.const.Missing]

Whether the user is deafened in voice channels

Missing
channel_id Union[Snowflake_Type, naff.client.const.Missing]

id of channel to move user to (if they are connected to voice)

Missing
communication_disabled_until Union[Timestamp, NoneType, naff.client.const.Missing]

when the user's timeout will expire and the user will be able to communicate in the guild again

Missing
reason Union[str, naff.client.const.Missing]

An optional reason for the audit log

Missing
Source code in naff/models/discord/user.py
async def edit(
    self,
    *,
    nickname: Absent[str] = MISSING,
    roles: Absent[Iterable["Snowflake_Type"]] = MISSING,
    mute: Absent[bool] = MISSING,
    deaf: Absent[bool] = MISSING,
    channel_id: Absent["Snowflake_Type"] = MISSING,
    communication_disabled_until: Absent[Union["Timestamp", None]] = MISSING,
    reason: Absent[str] = MISSING,
) -> None:
    """
    Modify attrbutes of this guild member.

    Args:
        nickname: Value to set users nickname to
        roles: Array of role ids the member is assigned
        mute: Whether the user is muted in voice channels. Will throw a 400 if the user is not in a voice channel
        deaf: Whether the user is deafened in voice channels
        channel_id: id of channel to move user to (if they are connected to voice)
        communication_disabled_until: 	when the user's timeout will expire and the user will be able to communicate in the guild again
        reason: An optional reason for the audit log
    """
    await self._client.http.modify_guild_member(
        self._guild_id,
        self.id,
        nickname=nickname,
        roles=roles,
        mute=mute,
        deaf=deaf,
        channel_id=channel_id,
        communication_disabled_until=communication_disabled_until,
        reason=reason,
    )

async method kick(self, reason)

Remove a member from the guild.

Parameters:

Name Type Description Default
reason Union[str, naff.client.const.Missing]

The reason for this removal

Missing
Source code in naff/models/discord/user.py
async def kick(self, reason: Absent[str] = MISSING) -> None:
    """
    Remove a member from the guild.

    Args:
        reason: The reason for this removal

    """
    await self._client.http.remove_guild_member(self._guild_id, self.id, reason=reason)

async method ban(self, delete_message_days, reason)

Ban a member from the guild.

Parameters:

Name Type Description Default
delete_message_days int

The number of days of messages to delete

0
reason Union[str, naff.client.const.Missing]

The reason for this ban

Missing
Source code in naff/models/discord/user.py
async def ban(self, delete_message_days: int = 0, reason: Absent[str] = MISSING) -> None:
    """
    Ban a member from the guild.

    Args:
        delete_message_days: The number of days of messages to delete
        reason: The reason for this ban

    """
    await self._client.http.create_guild_ban(self._guild_id, self.id, delete_message_days, reason=reason)