tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
61 lines (54 loc) • 1.85 kB
JavaScript
;
var json = require('../../../http/fetch/json.cjs');
var config = require('../config.cjs');
var errorValidator = require('../get/errorValidator.cjs');
// @ts-nocheck
/**
* Retrieves the authenticated user's linked connections from the Discord API.
*
* This includes external accounts the user has connected to their Discord profile,
* such as Steam, Twitch, YouTube, GitHub, etc.
*
* Requires the `connections` scope to be present in the OAuth2 token.
*
* @param {string} access_token - The OAuth2 access token used to authenticate the user.
*
* @returns {Promise<Array<Record<string, any>>>} Resolves with an array of connection objects.
*
* @throws {Record<string, any>} If the request fails, rejects with an error object containing `code` and `message`.
*
* @see {@link https://discord.com/developers/docs/resources/user#get-user-connections} Discord API Reference
*
* @example
* getUserConnections('your_access_token')
* .then(connections => {
* connections.forEach(conn => {
* console.log(`${conn.type}: ${conn.name}`);
* });
* })
* .catch(err => console.error('Failed to get connections:', err));
*/
function getUserConnections(access_token) {
return new Promise(function (resolve, reject) {
// API URL
const apiURL = config.url;
// Response
json(`${apiURL}users/@me/connections`, {
method: 'GET',
headers: {
Authorization: `Bearer ${access_token}`,
'Content-Type': 'application/json',
},
})
.then((data) => {
// Error Validator
const result = errorValidator(data);
if (!result.error) resolve(result.data);
else reject(result.error);
})
.catch((err) => {
reject({ code: err.response.status, message: err.message });
});
});
}
module.exports = getUserConnections;