为什么我可以使用 bot.get_user 函数获得一些用户,而其他用户却不能?[不和谐.py]

2022-01-15 00:00:00 python discord discord.py nonetype

问题描述

我只是在做我的一天并做功课,我去我的不和谐服务器检查声誉和排行榜进展如何在一天中发生变化,当我使用命令时,我收到错误Nonetype object has没有属性display_name""所以我很自然地去运行脚本的控制台,然后重新启动它,以为这可能只是一个网络错误.但是重启后还是不行.在错误出现之前,我没有更改甚至重新启动脚本.我知道该命令昨天(2020 年 10 月 26 日)有效,但由于某种原因它今天停止工作.在摆弄代码并使用断点和打印语句之后,我发现它有点挑选用户.稍微移动一下后,我得出的结论是,这是由于我的一个朋友在字符中具有无法理解的独特名称,但后来当我转到另一台服务器和所有前三名用户时,这被推翻了排行榜上有标准的 UTF-8 用户名,但这次我仍然收到错误消息.它是Nonetype 对象没有属性‘avatar_url’.在那之后,我决定放弃,来到堆栈溢出.此命令是 python 3.8.5 异步信誉命令,旨在读取存储在文件扩展名为Logs/reputation.log"下的 .log 文件中的字典.任何和所有的帮助/建议将不胜感激.

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.

命令:

    @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="{}
With a reputation of **{}**".format(first_mention, dict[sort[0]]),
                    color=0xDAA520
                )
            else:
                embed1 = discord.Embed(
                    title="1st Place is",
                    description="{}
With a reputation of **{}**".format(first_mention, dict[sort[0]]),
                    color=0xDAA520
                )
            if nd != 'Error':
                embed2 = discord.Embed(
                    title="2nd Place is",
                    description="{}
With a reputation of **{}**".format(second_mention, dict[sort[1]]),
                    color=0xC0C0C0
                )
            else:
                embed2 = discord.Embed(
                    title="2nd Place is",
                    description="{}
With a reputation of **{}**".format(second_mention, dict[sort[1]]),
                    color=0xC0C0C0
                )
            if rd != 'Error':
                embed3 = discord.Embed(
                    title="3rd Place is",
                    description="{}
With a reputation of **{}**".format(third_mention, dict[sort[2]]),
                    color=0xCD7F32
                )
            else:
                embed3 = discord.Embed(
                    title="3rd Place is",
                    description="{}
With 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!


解决方案

我猜你的问题是因为新版本的 discord.py(1.5.x).有一些关于 Intents 的更新.Intents类似于权限,你必须定义Intents来获取频道、成员和一些事件等.你必须在定义bot = discord.Bot(prefix='')之前定义它..p>

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)

如果你想获取更多关于 Intents 的信息,可以查看 API 参考.

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

相关文章