Fetch user profile script for custom social connection not updating app_metadata

I’m building a custom social connection for Discord. The reason I’m doing this is that the built-in Discord social connection does not seem to allow you to fetch a user’s roles in a given guild as part of the profile building (the only allowed scopes are identify and email, while I need identify and guilds.members.read).

I’m storing the user’s Discord role IDs in the app_metadata field, as the docs advised I should. However, despite the “Sync profile attributes on each login” switch being toggled on, the app_metadata field never updates after the first login. This is manifested by the fact that, even though the user’s roles are updated in the guild (and the API returns that information accurately), the discord_roles array remains static.

Fetch script attached below. Thanks in advance for any solutions you might have!

function (accessToken, context, callback) {
  const API_URL = 'https://discord.com/api/v10';
  const CDN_URL = 'https://cdn.discordapp.com';
      url: `${API_URL}/users/@me/guilds/<snip>/member`,
      headers: {
        Authorization: `Bearer ${accessToken}`
    (err, res, body) => {
      if (err) {
        return callback(err);
      if (res.statusCode !== 200) {
        return callback(new Error(body));
      let response;
      try {
        response = JSON.parse(body);
      } catch (jsonErr) {
        return callback(jsonErr);
      const { roles } = response;
      const { id, username, discriminator, avatar } = response.user;
      const tag = `${username}#${discriminator}`;
      const profile = {
        name: tag,
        nickname: username,
        picture: `${CDN_URL}/avatars/${id}/${avatar}.png`,
        app_metadata: {
          discord_roles: roles

      callback(null, profile);