'Discord.py - Issue iterating over member roles

I'm trying to iterate over all members on a discord server. While doing that, I want to look for members that have a specific role and write to an excel file using: writer.writerow([members.name, members.id, members.joined_at, 'ASSIGNED']), otherwise write using: writer.writerow([members.name, members.id, members.joined_at]).

Currently, I am able to do that, however, it is writing the same user multiple times (depending on how many roles they have assigned - if they have 3, the same user's information is written 3 times). Any advice on how to write a user's info once?

Running the most recent version of Python and Discord.py.

import discord
import csv

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    guild = client.get_guild(guild_id)

    with open('joindate.csv', 'w', encoding='utf-8') as f:
        writer = csv.writer(f)
        
        for members in guild.members:
            for role in members.roles:
                if role.id == 934989847247085568:
                    writer.writerow([members.name,members.id,members.joined_at, 'ASSIGNED'])
                else:
                    writer.writerow([members.name,members.id,members.joined_at])
       
            
client.run('token')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source