Exemple #1
0
    def __init__(self, client, **kwargs):
        super().__init__(kwargs["user"]["id"], client)

        # copy user data for when the user is decached
        self._user_data = kwargs["user"]
        self._bot.state.make_user(self._user_data)

        #: An iterable of role IDs this member has.
        self.role_ids = [int(rid) for rid in kwargs.get("roles", [])]

        #: A :class:`._MemberRoleContainer` that represents the roles of this member.
        self.roles = MemberRoleContainer(self)

        #: The date the user joined the guild.
        self.joined_at = to_datetime(kwargs.get(
            "joined_at", None))  # type: datetime.datetime

        nick = kwargs.get("nick")
        #: The member's current :class:`.Nickname`.
        self._nickname = Nickname(self, nick)  # type: Nickname

        #: The ID of the guild that this member is in.
        self.guild_id = None  # type: int

        #: The current :class:`.Presence` of this member.
        self.presence = Presence(status=kwargs.get("status", Status.OFFLINE),
                                 game=kwargs.get("game", None))
Exemple #2
0
    def __init__(self, client, **kwargs):
        super().__init__(client, **kwargs)

        #: The presence for this friend.
        self.presence = Presence(status=kwargs.get("status"),
                                 game=kwargs.get("game"))

        #: The :class:`.FriendType` of friend this user is.
        self.type_ = None  # type: FriendType
Exemple #3
0
    def from_guild_create(self, **data: dict) -> 'Guild':
        """
        Populates the fields from a GUILD_CREATE event.

        :param data: The GUILD_CREATE data to use.
        """
        self.unavailable = data.get("unavailable", False)

        if self.unavailable:
            # We can't use any of the extra data here, so don't bother.
            return self

        self.name = data.get("name")  # type: str
        self.icon_hash = data.get("icon")  # type: str
        self.splash_hash = data.get("splash")  # type: str
        self.owner_id = int(data.get("owner_id", 0)) or None  # type: int
        self._large = data.get("large", None)
        self.features = data.get("features", [])
        self.region = data.get("region")

        afk_channel_id = data.get("afk_channel_id", 0)
        if afk_channel_id:
            afk_channel_id = int(afk_channel_id)

        self.afk_channel_id = afk_channel_id
        self.afk_timeout = data.get("afk_timeout")

        self.verification_level = VerificationLevel(data.get("verification_level", 0))
        self.mfa_level = MFALevel(data.get("mfa_level", 0))
        self.notification_level = NotificationLevel(data.get("default_message_notifications", 0))
        self.content_filter_level = ContentFilterLevel(data.get("explicit_content_filter", 0))

        self.member_count = data.get("member_count", 0)

        # Create all the Role objects for the server.
        for role_data in data.get("roles", []):
            role_obj = dt_role.Role(self._bot, **role_data)
            role_obj.guild_id = self.id
            self._roles[role_obj.id] = role_obj

        # Create all the Member objects for the server.
        self._handle_member_chunk(data.get("members", []))

        for presence in data.get("presences", []):
            member_id = int(presence["user"]["id"])
            member_obj = self._members.get(member_id)

            if not member_obj:
                continue

            member_obj.presence = Presence(**presence)

        # Create all of the channel objects.
        for channel_data in data.get("channels", []):
            channel_obj = dt_channel.Channel(self._bot, **channel_data)
            self._channels[channel_obj.id] = channel_obj
            channel_obj.guild_id = self.id
            channel_obj._update_overwrites(channel_data.get("permission_overwrites", []), )

        # Create all of the voice states.
        for vs_data in data.get("voice_states", []):
            user_id = int(vs_data.get("user_id", 0))
            member = self.members.get(user_id)
            if not member:
                # o well
                continue

            voice_state = dt_vs.VoiceState(**vs_data, client=self._bot)
            self._voice_states[voice_state.user_id] = voice_state

            vs_channel = self._channels.get(int(vs_data.get("channel_id", 0)))
            if vs_channel is not None:
                voice_state.channel_id = vs_channel.id
                voice_state.guild_id = self.id

        # delegate to other function
        self._handle_emojis(data.get("emojis", []))
Exemple #4
0
    async def handle_presence_update(self, gw: 'gateway.GatewayHandler', event_data: dict):
        """
        Called when a member changes game.
        """
        guild_id = int(event_data.get("guild_id", 0))
        guild = self._guilds.get(guild_id)

        # awful payloads
        user = event_data.get("user")
        if not user:
            return

        # also awful payloads
        try:
            user_id = int(user.get("id"))
        except (ValueError, TypeError):
            return

        # deprecated - previously friend updates, but userbot support was removed
        if not guild:
            return

        # try and create a new member from the presence update
        member = guild.members.get(user_id)
        if member is None:
            # create the member from the presence
            # we only pass the User here as we're about to update everything
            member = Member(client=self.client, user=event_data["user"])
            member.guild_id = guild.id
            old_member = None
        else:
            old_member = member._copy()

        # Update the member's presence
        member.presence = Presence(status=event_data.get("status"), game=event_data.get("game", {}))

        # copy the roles if it exists
        if old_member is not None:
            fallback = old_member.role_ids
        else:
            fallback = None

        roles = event_data.get("roles", fallback)
        if roles:
            # clear roles
            member.role_ids = [int(rid) for rid in roles]

        # update the nickname
        if old_member is not None:
            fallback = old_member.nickname.value
        else:
            fallback = None

        member.nickname = event_data.get("nick", fallback)
        # recreate the user object, so the user is properly cached
        if "username" in event_data["user"]:
            self.make_user(event_data["user"], override_cache=True)

        # Note: Usually, when a member has a change that we need to cache, we get sent a
        # GUILD_MEMBER_UPDATE packet.
        # However, sometimes Discord might not send it to us. So we always update.
        # We might get PRESENCE_UPDATE events for members that recently left the guild though,
        # so we must ensure we only update, not add a member
        if user_id in guild._members:
            guild._members[user_id] = member
        yield "member_update", old_member, member,