UNPKG

@questlabs/core

Version:
184 lines (183 loc) 7.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const variables = __importStar(require("../../utils/config")); class Discord { constructor(config) { this.apiKey = config.apiKey; this.apiSecret = config.apiSecret; this.baseUrl = (config === null || config === void 0 ? void 0 : config.testMode) == true ? variables.serverTestUrl : variables.serverUrl; this.config = { headers: { apiKey: this.apiKey, apiSecret: this.apiSecret, "User-Agent": config.userAgent, }, }; } connect({ entityId, userId, token, discordRedirectCode, redirectUri, }) { return __awaiter(this, void 0, void 0, function* () { try { if (!discordRedirectCode || discordRedirectCode == "") { throw new Error("Invalid discordRedirectCode"); } if (!redirectUri || redirectUri == "") { throw new Error("Invalid redirectUri"); } if (!entityId || entityId == "") { throw new Error("Invalid entityId"); } if (!userId || userId == "") { throw new Error("Invalid userId"); } if (!token || token == "") { throw new Error("Invalid token"); } const headers = Object.assign(Object.assign({}, this.config.headers), { userId, token }); const body = { code: discordRedirectCode, redirectUri, entityId, userId, }; const response = yield axios_1.default.post(`${this.baseUrl}/discord/login`, body, { headers, }); return response.data; } catch (error) { throw new Error(error.message); } }); } disconnect({ entityId, userId, token, }) { return __awaiter(this, void 0, void 0, function* () { try { if (!entityId || entityId == "") { throw new Error("Invalid entityId"); } if (!userId || userId == "") { throw new Error("Invalid userId"); } if (!token || token == "") { throw new Error("Invalid token"); } const headers = Object.assign(Object.assign({}, this.config.headers), { userId, token }); const body = { userId, entityId }; const response = yield axios_1.default.post(`${this.baseUrl}/discord/logout`, body, { headers }); return response.data; } catch (error) { throw new Error(error.message); } }); } fetchMyGuilds({ entityId, userId, token }) { return __awaiter(this, void 0, void 0, function* () { try { if (!entityId || entityId == "") { throw new Error("Invalid entityId"); } if (!userId || userId == "") { throw new Error("Invalid userId"); } if (!token || token == "") { throw new Error("Invalid token"); } const headers = Object.assign(Object.assign({}, this.config.headers), { userId, token }); const params = { entityId, userId }; const response = yield axios_1.default.get(`${this.baseUrl}/discord/my/guilds`, { headers, params, }); return response.data; } catch (error) { throw new Error(error.message); } }); } fetchMyGuildRoles({ entityId, userId, token, guildId, }) { return __awaiter(this, void 0, void 0, function* () { try { if (!entityId || entityId == "") { throw new Error("Invalid entityId"); } if (!userId || userId == "") { throw new Error("Invalid userId"); } if (!token || token == "") { throw new Error("Invalid token"); } if (!guildId || guildId == "") { throw new Error("Invalid guildId"); } const headers = Object.assign(Object.assign({}, this.config.headers), { userId, token }); const params = { entityId, userId }; const response = yield axios_1.default.get(`${this.baseUrl}/discord/guilds/${guildId}/roles`, { headers, params }); return response.data; } catch (error) { throw new Error(error.message); } }); } fetchGuildSceduledEvents({ entityId, userId, token, guildId, }) { return __awaiter(this, void 0, void 0, function* () { try { if (!entityId || entityId == "") { throw new Error("Invalid entityId"); } if (!userId || userId == "") { throw new Error("Invalid userId"); } if (!token || token == "") { throw new Error("Invalid token"); } if (!guildId || guildId == "") { throw new Error("Invalid guildId"); } const headers = Object.assign(Object.assign({}, this.config.headers), { userId, token }); const params = { entityId, userId }; const response = yield axios_1.default.get(`${this.baseUrl}/discord/guilds/${guildId}/events`, { headers, params }); return response.data; } catch (error) { throw new Error(error.message); } }); } } exports.default = Discord;