为什么我可以使用 bot.get_user 函数获得某些用户,而其他用户则不能? [Discord.py]
Why can I get some users with the bot.get_user function but not others? [Discord.py]
我正忙着做功课,我去我的 discord 服务器查看一整天的声誉和排行榜进度如何变化,当我使用命令时,我收到错误消息“Nonetype object没有属性 'display_name'”,所以我很自然地转到控制台 运行 脚本并重新启动它,以为这可能只是一个网络错误。但是重启后还是不行。在错误出现之前,我没有更改甚至重新启动脚本。我知道该命令昨天(2020 年 10 月 26 日)有效,但由于某种原因它今天停止工作。在摆弄代码并使用断点和打印语句之后,我发现它有点挑选用户。稍微移动了一下之后,我得出的结论是,这是因为我的一个朋友有一个独特的字符名称,无法被理解,但后来当我去另一台服务器和所有前三名用户时,这被证明是错误的排行榜上有标准的 UTF-8 用户名,但这次我仍然收到错误消息。它是“Nonetype object has no attribute 'avatar_url'。在那之后我决定放弃并来到堆栈溢出。这个命令是一个 python 3.8.5 异步信誉命令,旨在读取存储在.log 文件在文件扩展名“Logs/reputation.log”下。任何和所有 help/advice 将不胜感激。
命令:
@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)
编辑:
使用新的 discord intents 并在 Discord 开发者门户上启用 intents 对我有用。谢谢!
我猜你的问题是因为 discord.py(1.5.x) 的新版本。 Intents
有一些更新。 Intents类似于权限,您必须定义Intents才能获取频道、成员和一些事件等。您必须在定义bot = discord.Bot(prefix='')
之前定义它。
import discord
intents = discord.Intents().all()
bot = discord.Bot(prefix='', intents=intents)
如果你想获得更多关于Intents的信息,你可以查看API References。
我正忙着做功课,我去我的 discord 服务器查看一整天的声誉和排行榜进度如何变化,当我使用命令时,我收到错误消息“Nonetype object没有属性 'display_name'”,所以我很自然地转到控制台 运行 脚本并重新启动它,以为这可能只是一个网络错误。但是重启后还是不行。在错误出现之前,我没有更改甚至重新启动脚本。我知道该命令昨天(2020 年 10 月 26 日)有效,但由于某种原因它今天停止工作。在摆弄代码并使用断点和打印语句之后,我发现它有点挑选用户。稍微移动了一下之后,我得出的结论是,这是因为我的一个朋友有一个独特的字符名称,无法被理解,但后来当我去另一台服务器和所有前三名用户时,这被证明是错误的排行榜上有标准的 UTF-8 用户名,但这次我仍然收到错误消息。它是“Nonetype object has no attribute 'avatar_url'。在那之后我决定放弃并来到堆栈溢出。这个命令是一个 python 3.8.5 异步信誉命令,旨在读取存储在.log 文件在文件扩展名“Logs/reputation.log”下。任何和所有 help/advice 将不胜感激。
命令:
@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)
编辑: 使用新的 discord intents 并在 Discord 开发者门户上启用 intents 对我有用。谢谢!
我猜你的问题是因为 discord.py(1.5.x) 的新版本。 Intents
有一些更新。 Intents类似于权限,您必须定义Intents才能获取频道、成员和一些事件等。您必须在定义bot = discord.Bot(prefix='')
之前定义它。
import discord
intents = discord.Intents().all()
bot = discord.Bot(prefix='', intents=intents)
如果你想获得更多关于Intents的信息,你可以查看API References。