Search code examples
pythondiscord

getting the Premium tier of a User without redirect uri


I want to check whether a user is subscribed to Discord Nitro. I found out that you can do that with the following line of code:

response = requests.get(f"https://discord.com/api/v8/users/{message.author.id}", headers={
    "Authorization": f"Bot TOKEN"
})
if response.status_code == 200:
    data = response.json()
    premium_type = data.get("premium_type")
    print(f"User {message.author} has premium tier {premium_type}")
else:
    print(f"Failed to get user {message.author}'s premium tier. Status code: {response.status_code}")

However, this code does not give me the expected output. I am subscribed to Discord Nitro, so the output should be 'User EntchenEric#1002 has premium tier 3', but instead it is 'User EntchenEric#1002 has premium tier None'.

After some Googling, I found out that you need the guilds scope to access the premium tier information. However, when I try to generate an invite link with this scope, I have to enter a redirect URI. When I invite the bot, I get redirected but the bot does not join the server I specified in the invite process.

Is there any way to get the premium tier without any scopes (other than bot and applications.commands)? Or what do I have to change with the invite process to get the bot to work with the guilds scope?"


Solution

  • It is not possible to get a user's nitro status using the API, as Discord doesn't tell us whether or not someone has nitro. If you want to check if a member is boosting the server, you can do that by parsing Member.premium_since, but if you want a more general method, which can be applied to any members and users, what you can do is develop a function to analyze if the user has any nitro user characteristics, such as animated avatar, banner or custom emojis. Below I leave the code of a function that I found in the discord.py support server:

    def guess_user_nitro_status(user: Union[discord.User, discord.Member]):
        """Guess if an user or member has Discord Nitro"""
    
        if isinstance(user, discord.Member):
            # Check if they have a custom emote in their status
            has_emote_status = any([a.emoji.is_custom_emoji() for a in user.activities if getattr(a, 'emoji', None)])
    
            return any([user.display_avatar.is_animated(), has_emote_status, user.premium_since, user.guild_avatar])
    
        return any([user.display_avatar.is_animated(), user.banner])
    

    Note: For an user's banner to be available, they must be fetched with bot.fetch_user()