dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
684 stars 162 forks source link

Check 'verified' before trying to overwrite attributes. #82

Closed caiocinel closed 3 years ago

caiocinel commented 3 years ago

What causes the problem is that the state coming from a guild overwrites the one already defined in the startup, with that private information is lost.

Fix #77

dolfies commented 3 years ago

verified isn't guaranteed to be in the initial user data. This could potentially produce more problems.

dolfies commented 3 years ago

After some testing, the correct (and fastest) solution would be to change store_user() to this:

    def store_user(self, data):
        # This way is 300% faster than `dict.setdefault`.
        user_id = int(data['id'])
        try:
            user = self._users[user_id]
            # We use the data available to us since we
            # might not have events for that user.
            # However, the data may only have an ID.
            if user_id != self.self_id:
                try:
                    user._update(data)
                except KeyError:
                    pass
            return user
        except KeyError:
            user = User(state=self, data=data)
            if user.discriminator != '0000':
                self._users[user_id] = user
            return user