var SERVER_ID = '';

var memberUserIds = db.server_members.distinct("_id.user", { "_id.server": SERVER_ID });
var bannedUserIds = db.server_bans.distinct("_id.user", { "_id.server": SERVER_ID });

var excludedIds = memberUserIds.concat(bannedUserIds);

var users = db.users.find({
    _id: { $nin: excludedIds }
}, { _id: 1 }).toArray();

var inserted = 0;
users.forEach(user => {
    db.server_members.insertOne({
        _id: {
            server: SERVER_ID,
            user: user._id
        },
        joined_at: Long(Date.now()),
        roles: []
    });
    inserted++;
});

print("Done. Inserted " + inserted + " users into server.");
