Skip to main content

Using data transformers

Sometimes you may want to add additional data from your bot to the LoginData response from Using the built in OAUTH2 route (backend) and Refreshing OAUTH2 tokens and Discord data. This can be done by using a data transformer. On this page we will guide to writing and specifying your data transformer. Note that you can provide multiple data transformers if you want to separate concerns, each transformer is called in sequence and receives the data of the preceding transformer (or the base LoginData if it is the first). Each transformer should also return an object that satisfies interface of LoginData.

Lets say we want to add a property that tells us if the bot is in the specific guild, we can add the following transformer:

const { container } = require('@sapphire/framework');

function transformOauthGuilds(loginData) {
const { client } = container;

const transformedGuilds = loginData.guilds?.map((guild) => {
const cachedGuild = client.guilds.cache.get(guild.id);

return {
...guild,
botIsInGuild: typeof cachedGuild !== 'undefined'
};
});

return { ...loginData, transformedGuilds };
}
module.exports = {
transformOauthGuilds
};

We now specify to the API plugin that we want to use this transformer:

const { SapphireClient } = require('@sapphire/framework');

const client = new SapphireClient({
api: {
auth: {
// The other options
transformers: [transformOauthGuilds]
}
}
});