This code imports dependencies and exports three functions: userGuilds
, getGuildRoles
, and userConnections
, which interact with the Discord API to retrieve user information, guild roles, and user connections, respectively. The functions are asynchronous and use the request
function to make HTTP requests to the Discord API, with optional parameters for specifying user or guild IDs.
npm run import -- "discord guilds"
var {DEFAULT_GUILD} = importer.import("discord configuration")
var {request} = importer.import("discord authorization")
async function userGuilds(userId = '@me') {
return await request({
method: 'GET',
url: `users/${userId}/guilds`
})
}
async function getGuildRoles(guildId = DEFAULT_GUILD) {
return await request({
method: 'GET',
url: `guilds/${guildId}/roles`
})
}
async function userConnections(userId = '@me') {
return await request({
method: 'GET',
url: `users/${userId}/connections`
})
}
module.exports = {
userGuilds,
getGuildRoles,
userConnections
}
// Importing required modules and configurations
const { DEFAULT_GUILD } = require('./discord-configuration');
const { request } = require('./discord-authorization');
// Defining API endpoint URLs
const ENDPOINTS = {
USERS_GUILDS: (userId) => `users/${userId}/guilds`,
GUILD_ROLES: (guildId) => `guilds/${guildId}/roles`,
USER_CONNECTIONS: (userId) => `users/${userId}/connections`,
};
// Fetch user guilds
/**
* Retrieves the guilds a user is a part of.
* @param {string} [userId='@me'] - The ID of the user.
* @returns {Promise} A promise that resolves with the user's guilds.
*/
async function userGuilds(userId = '@me') {
try {
const response = await request({ method: 'GET', url: ENDPOINTS.USERS_GUILDS(userId) });
return response.data;
} catch (error) {
// TODO: Implement error handling and logging
console.error('Error fetching user guilds:', error);
return null;
}
}
// Fetch guild roles
/**
* Retrieves the roles in a guild.
* @param {string} [guildId=DEFAULT_GUILD] - The ID of the guild.
* @returns {Promise} A promise that resolves with the guild's roles.
*/
async function getGuildRoles(guildId = DEFAULT_GUILD) {
try {
const response = await request({ method: 'GET', url: ENDPOINTS.GUILD_ROLES(guildId) });
return response.data;
} catch (error) {
// TODO: Implement error handling and logging
console.error('Error fetching guild roles:', error);
return null;
}
}
// Fetch user connections
/**
* Retrieves the connections a user has.
* @param {string} [userId='@me'] - The ID of the user.
* @returns {Promise} A promise that resolves with the user's connections.
*/
async function userConnections(userId = '@me') {
try {
const response = await request({ method: 'GET', url: ENDPOINTS.USER_CONNECTIONS(userId) });
return response.data;
} catch (error) {
// TODO: Implement error handling and logging
console.error('Error fetching user connections:', error);
return null;
}
}
// Exporting the API endpoints
module.exports = {
userGuilds,
getGuildRoles,
userConnections,
};
var {DEFAULT_GUILD} = importer.import('discord configuration')
var {request} = importer.import('discord authorization')
DEFAULT_GUILD
and request
, from external modules using the importer.import
function.DEFAULT_GUILD
is likely a predefined constant representing the default guild ID.request
is an asynchronous function for making HTTP requests, likely used for interacting with the Discord API.async function userGuilds(userId = '@me') {
return await request({
method: 'GET',
url: `users/${userId}/guilds`
})
}
request
function.userId
parameter is optional and defaults to @me
, which is likely a placeholder for the current user's ID.async function getGuildRoles(guildId = DEFAULT_GUILD) {
return await request({
method: 'GET',
url: `guilds/${guildId}/roles`
})
}
request
function.guildId
parameter is optional and defaults to DEFAULT_GUILD
, which is the predefined default guild ID.async function userConnections(userId = '@me') {
return await request({
method: 'GET',
url: `users/${userId}/connections`
})
}
request
function.userId
parameter is optional and defaults to @me
, which is likely a placeholder for the current user's ID.module.exports = {
userGuilds,
getGuildRoles,
userConnections
}