ColeTMK
ColeTMK

Reputation: 74

Cant find inviter id for discord server invite

im trying to find the user id of the person that invited someone to a discord server.. im getting the error

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'

My code is

@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!')

Basically im making the user id go into the csv file to keep track of the inviter and how many invites they have. The error is happening at inviter = member.guild.get_member(inv_user.id) How can I resolve this?

Upvotes: 0

Views: 300

Answers (1)

Axisnix
Axisnix

Reputation: 2907

Your code seems correct. However, you require the Members Intent this is because the get_member requires a Member object which you can only get with the Members Intent.

You will have to enable it here. Select the application you want it on -> Select Bot -> SERVER MEMBERS INTENT and then make sure it shows blue next to it. Then click Save Changes. You might also want to enable Presence intent as you may require it in the future and it saves you time going back and doing it again.

You will then have to edit your bot variable like so:

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

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

Upvotes: 1

Related Questions