无法找到不和谐服务器邀请的邀请者 ID

时间:2021-07-27 00:57:27

标签: python discord.py

我正在尝试查找邀请某人加入不和谐服务器的人的用户 ID.. 我收到错误

Ignoring exception in on_member_join Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "main.py", line 164, in on_member_join inviter = member.guild.get_member(inv_user.id) AttributeError: 'NoneType' object has no attribute 'id'

我的代码是

@bot.event
async def on_member_join(member):
    global count_df
    global inviters_df
    global leavers_df

    inv_user = await tracker.fetch_inviter(member)
    inviter = member.guild.get_member(inv_user.id)
    
    #Update invite counts
    try: #Check to see if the user had an invite count saved already
        count_df.loc[inviter.id]
    except KeyError: #If not, make a new entry for the member
        new_entry = pd.DataFrame([[1,0,0]], index=[inviter.id], columns=count_df.columns)
        count_df = count_df.append(new_entry)
    else: #If yes, then add to it
        count_df.loc[inviter.id,"Real"] += 1
    

    #Update inviter/leaver record
    inviters_df = inviters_df.append(pd.DataFrame([[inviter.id]], index=[member.id], columns=inviters_df.columns))
    inviters_df.to_csv('csv/member_invite_record.csv', index_label='Member')

    try:
        entries =leavers_df.loc[member.id]
    except KeyError:
        return #Member has not left the guild before, no need to update anything
    else:
        for entry in pd.DataFrame([entries]).itertuples():
            if entry.Inviter == inviter.id:
                count_df.loc[inviter.id,"Left"] -= 1
        count_df.to_csv('csv/invite_count.csv', index_label='User_ID')
        leavers_df.drop(labels=member.id, inplace=True)
        leavers_df.to_csv('csv/member_leaver_record.csv', index_label='Leaver')
    
    await give_roles(inviter)
    channel = bot.get_channel(821556573386047518)
    await channel.send(f'{inviter.mention} invited {member.mention} to the server!')

基本上,我将用户 ID 放入 csv 文件以跟踪邀请者以及他们有多少邀请。错误发生在 inviter = member.guild.get_member(inv_user.id) 我该如何解决?

1 个答案:

答案 0 :(得分:1)

您的代码似乎正确。但是,您需要 Members Intent,这是因为 get_member 需要一个 Member 对象,您只能通过 Members Intent 获取该对象。

您必须启用它 here。选择您想要的应用程序 -> 选择 Bot -> SERVER MEMBERS INTENT,然后确保它旁边显示蓝色。然后单击保存更改。您可能还想启用 Presence intent,因为您将来可能需要它,这样可以节省您返回并再次执行的时间。

然后你必须像这样编辑你的机器人变量:

intents = discord.Intents()
intents.all()

bot = commands.Bot(command_prefix=".", intents=intents)