discord-inv
Version:
Get more information about a discord invitation link!
45 lines (44 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInvite = exports.getInviteFromURL = exports.getInviteDataFromURL = void 0;
const InviteFetchError_1 = require("./InviteFetchError");
const Invite_1 = require("./Invite");
const centra = require('centra');
/**
* Extracts data from an discord invite url
* @param {string} inviteURL URL to extract data from
* @returns {InviteData} Extracted data from URL
*/
function getInviteDataFromURL(inviteURL) {
const url = new URL(inviteURL);
if (url.hostname !== 'discord.gg' && url.hostname !== 'discord.com')
throw new Error('Invalid URL: Please use a discord.com or discord.gg invite url');
const eventID = url.searchParams.get('event');
return { url, eventID, code: url.pathname.replaceAll('/invite', '').replaceAll('/', '') };
}
exports.getInviteDataFromURL = getInviteDataFromURL;
/**
* Extracts data from an invite url and queries the invite from the Discord API
* @param {string} inviteURL Discord-Invite-URL
* @param {boolean} withCounts `approximate_member_count` and `approximate_presence_count` will only be set, if this is true
* @returns {Promise<Invite>} Queried invite
*/
async function getInviteFromURL(inviteURL, withCounts = false) {
const inviteData = getInviteDataFromURL(inviteURL);
return getInvite(inviteData.code, withCounts, inviteData.eventID);
}
exports.getInviteFromURL = getInviteFromURL;
/**
* Queries an invite from the Discord API by its code
* @param {string} code Code of the invite to query
* @param {boolean} withCounts `approximate_member_count` and `approximate_presence_count` will only be set, if this is true
* @param {Snowflake} eventID Snowflake of an event associated with the invite. `guild_scheduled_event` will only be set, if this is valid
* @return {Promise<Invite>} Fetched invite
*/
async function getInvite(code, withCounts = false, eventID) {
const res = await centra(`https://discord.com/api/v8/invites/${code}?with_counts=${withCounts}${eventID ? `&guild_scheduled_event_id=${eventID}` : ''}`, 'GET').send();
if (res.statusCode !== 200)
throw new InviteFetchError_1.InviteFetchError(JSON.parse(res.body.toString()));
return new Invite_1.Invite(JSON.parse(res.body.toString()));
}
exports.getInvite = getInvite;