Search code examples
pythondiscorddiscord.pynonetype

Why can I get some users with the bot.get_user function but not others? [Discord.py]


I was just going about my day and doing school work and I went to my discord server to check how the reputation and leaderboard progress had changed throughout the day and when I used the command, I got the error "Nonetype object has no attribute 'display_name'" so naturally I went to the console running the script and just restarted it thinking that maybe it was just a network error. However after restarting it, it would still not work. I hadn't changed or even restarted the script until after the error had come. I know the command worked yesterday (October 26, 2020) but for some reason it stopped working today. After fiddling around with the code and using break points as well as print statements, I discovered that it sort of picks and chooses users. After moving it around a bit I had come to the conclusion that it was due to one of my friends having a unique name in characters that were incapable of being understood, but that was later disproved when I went to another server and all top three users on the leaderboard had standard UTF-8 usernames and I still received an error this time. it was "Nonetype object has no attribute 'avatar_url'. After that I decided to give up and come to stack overflow. This command is a python 3.8.5 asynchronous reputation command designed to read a dictionary stored in a .log file under the file extension "Logs/reputation.log". Any and all help/advice would be greatly appreciated.

Command :

    @bot.command()
    async def leaderboard(ctx):
        with open('Logs/reputation.log', 'r') as file:
            dict = ast.literal_eval(file.read())
            dict = dict[str(ctx.guild.id)]
            sort = sorted(dict, key=lambda x: dict[x], reverse=True) #lambda x: (dict[str(ctx.guild.id)][x])

            st = bot.get_user(int(sort[0]))

            print(sort[0])

            nd = bot.get_user(int(sort[1]))

            print(sort[1])

            rd = bot.get_user(int(sort[2]))

            print(sort[2])

            try:
                first_mention = st.mention
            except AttributeError:
                first_mention = st

            try:
                second_mention = nd.mention
            except AttributeError:
                second_mention = nd

            try:
                third_mention = rd.mention
            except AttributeError:
                third_mention = rd

            print(st)
            print(nd)
            print(rd)

            if st != 'Error':
                embed1 = discord.Embed(
                    title="1st Place is",
                    description="{}\nWith a reputation of **{}**".format(first_mention, dict[sort[0]]),
                    color=0xDAA520
                )
            else:
                embed1 = discord.Embed(
                    title="1st Place is",
                    description="{}\nWith a reputation of **{}**".format(first_mention, dict[sort[0]]),
                    color=0xDAA520
                )
            if nd != 'Error':
                embed2 = discord.Embed(
                    title="2nd Place is",
                    description="{}\nWith a reputation of **{}**".format(second_mention, dict[sort[1]]),
                    color=0xC0C0C0
                )
            else:
                embed2 = discord.Embed(
                    title="2nd Place is",
                    description="{}\nWith a reputation of **{}**".format(second_mention, dict[sort[1]]),
                    color=0xC0C0C0
                )
            if rd != 'Error':
                embed3 = discord.Embed(
                    title="3rd Place is",
                    description="{}\nWith a reputation of **{}**".format(third_mention, dict[sort[2]]),
                    color=0xCD7F32
                )
            else:
                embed3 = discord.Embed(
                    title="3rd Place is",
                    description="{}\nWith a reputation of **{}**".format(third_mention, dict[sort[2]]),
                    color=0xCD7F32
                )
                # embed1.add_field(name="1st Place : ", value=st.mention, inline=False)
            embed1.set_thumbnail(url=st.avatar_url)
            # embed2.add_field(name="2nd Place : ", value=nd.mention, inline=False)
            embed2.set_thumbnail(url=nd.avatar_url)
            # embed3.add_field(name="3rd Place : ", value=rd.mention, inline=False)
            embed3.set_thumbnail(url=rd.avatar_url)
            
            # await ctx.send(embed=embed)
            await ctx.send(embed=embed1)
            await ctx.send(embed=embed2)
            await ctx.send(embed=embed3)

Edit : Using the new discord intents and enabling the intents on the Discord Developer Portal worked for me. Thanks!


Solution

  • I guess your problem is because of the new version of discord.py(1.5.x). There're some updates about Intents. Intents are similar to permissions, you have to define Intents to get channels, members and some events etc. You have to define it before defining the bot = discord.Bot(prefix='').

    import discord
    
    intents = discord.Intents().all()
    bot = discord.Bot(prefix='', intents=intents)
    

    If you want to get more information about Intents, you can look at the API References.