UNPKG

db-discord

Version:

db-discord is a library that uses discord guilds and text channels as databases. db-discord relies on discord.js

310 lines 15.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var LongTable_1 = require("./LongTable"); var FileTable_1 = require("./FileTable"); var ShortTable_1 = require("./ShortTable"); var discordDB = /** @class */ (function () { function discordDB(guild, channels, metatable, tables) { this.guild = guild; this.channels = channels; this.metatable = metatable; this.tables = tables; } discordDB.init = function (guild) { return __awaiter(this, void 0, void 0, function () { var channels, metatable, _a, metadata, tables; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: channels = guild.channels.array().filter(function (x) { return x.type == "text"; }).filter(function (x) { return x.memberPermissions(guild.me).has("READ_MESSAGES"); }); _a = (function () { var metaChnnels = channels.filter(function (x) { return x.name === "metatable"; }); if (metaChnnels.length === 0) { return null; } return new ShortTable_1.ShortTable(metaChnnels[0]); })(); if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () { var channel; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, guild.createChannel("metatable", { type: "text" })]; case 1: channel = _a.sent(); channels[channels.length] = channel; return [2 /*return*/, new ShortTable_1.ShortTable(channel)]; } }); }); })()]; case 1: _a = (_b.sent()); _b.label = 2; case 2: metatable = _a; return [4 /*yield*/, metatable.search({})]; case 3: metadata = _b.sent(); tables = metadata.map(function (meta) { var channel = channels.filter(function (x) { return x.id === meta.Value.id; })[0]; var table = (function () { switch (meta.Value.type) { case "short": return new ShortTable_1.ShortTable(channel); break; case "long": return new LongTable_1.LongTable(channel); break; case "file": return new FileTable_1.FileTable(channel); break; default: throw new Error("table type not supported"); break; } })(); return { Key: meta.Value, Value: table }; }); return [2 /*return*/, new discordDB(guild, channels, metatable, tables)]; } }); }); }; discordDB.prototype.getShortTableFromName = function (name) { var channels = this.channels.filter(function (x) { return x.name === name; }); if (channels.length === 0) { return null; } return new ShortTable_1.ShortTable(channels[0]); }; discordDB.prototype.getLongTableFromName = function (name) { var channels = this.channels.filter(function (x) { return x.name === name; }); if (channels.length === 0) { return null; } return new LongTable_1.LongTable(channels[0]); }; discordDB.prototype.getFileTableFromName = function (id) { var channels = this.channels.filter(function (x) { return x.id === id; }); if (channels.length === 0) { return null; } return new FileTable_1.FileTable(channels[0]); }; discordDB.prototype.getShortTableFromID = function (id) { var channels = this.channels.filter(function (x) { return x.id === id; }); if (channels.length === 0) { return null; } return new ShortTable_1.ShortTable(channels[0]); }; discordDB.prototype.getLongTableFromID = function (id) { var channels = this.channels.filter(function (x) { return x.id === id; }); if (channels.length === 0) { return null; } return new LongTable_1.LongTable(channels[0]); }; discordDB.prototype.getFileTableFromID = function (id) { var channels = this.channels.filter(function (x) { return x.id === id; }); if (channels.length === 0) { return null; } return new FileTable_1.FileTable(channels[0]); }; discordDB.prototype.createNewShortTable = function (name) { return __awaiter(this, void 0, void 0, function () { var channel, table; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.guild.createChannel(name, { type: "text" })]; case 1: channel = _a.sent(); this.channels[this.channels.length] = channel; table = new ShortTable_1.ShortTable(channel); return [4 /*yield*/, this.addToMetatable(table)]; case 2: _a.sent(); return [2 /*return*/, table]; } }); }); }; discordDB.prototype.createNewLongTable = function (name) { return __awaiter(this, void 0, void 0, function () { var channel, table; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.guild.createChannel(name, { type: "text" })]; case 1: channel = _a.sent(); this.channels[this.channels.length] = channel; table = new LongTable_1.LongTable(channel); return [4 /*yield*/, this.addToMetatable(table)]; case 2: _a.sent(); return [2 /*return*/, table]; } }); }); }; discordDB.prototype.createNewFileTable = function (name) { return __awaiter(this, void 0, void 0, function () { var channel, table; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.guild.createChannel(name, { type: "text" })]; case 1: channel = _a.sent(); this.channels[this.channels.length] = channel; table = new FileTable_1.FileTable(channel); return [4 /*yield*/, this.addToMetatable(table)]; case 2: _a.sent(); return [2 /*return*/, table]; } }); }); }; discordDB.prototype.getOrCreateShortTable = function (name) { return __awaiter(this, void 0, void 0, function () { var table; return __generator(this, function (_a) { switch (_a.label) { case 0: table = this.getShortTableFromName(name); if (!(table === null)) return [3 /*break*/, 2]; return [4 /*yield*/, this.createNewShortTable(name)]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [2 /*return*/, table]; } }); }); }; discordDB.prototype.getOrCreateLongTable = function (name) { return __awaiter(this, void 0, void 0, function () { var table; return __generator(this, function (_a) { switch (_a.label) { case 0: table = this.getLongTableFromName(name); if (!(table === null)) return [3 /*break*/, 2]; return [4 /*yield*/, this.createNewLongTable(name)]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [2 /*return*/, table]; } }); }); }; discordDB.prototype.getOrCreateFileTable = function (name) { return __awaiter(this, void 0, void 0, function () { var table; return __generator(this, function (_a) { switch (_a.label) { case 0: table = this.getFileTableFromName(name); if (!(table === null)) return [3 /*break*/, 2]; return [4 /*yield*/, this.createNewFileTable(name)]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [2 /*return*/, table]; } }); }); }; discordDB.prototype.deleteTable = function (table) { return __awaiter(this, void 0, void 0, function () { var channel, i, _a, _b, _c, _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: channel = this.channels.filter(function (x) { return x.id === table.id; })[0]; i = this.channels.indexOf(channel); this.channels.slice(i, i + 1); _b = (_a = Promise).all; _c = [channel.delete()]; _e = (_d = this.metatable).delete; return [4 /*yield*/, this.metatable.search({ searchObject: { id: table.id } })]; case 1: return [4 /*yield*/, _e.apply(_d, [(_f.sent())[0].Key])]; case 2: return [4 /*yield*/, _b.apply(_a, [_c.concat([_f.sent()])]).then(function (x) { return ({}); })]; case 3: return [2 /*return*/, _f.sent()]; } }); }); }; discordDB.prototype.addToMetatable = function (table) { return __awaiter(this, void 0, void 0, function () { var metadata; return __generator(this, function (_a) { switch (_a.label) { case 0: metadata = { name: table.name, id: table.id, objectTypes: null, type: table.type, }; this.tables[this.tables.length] = { Key: metadata, Value: table }; return [4 /*yield*/, this.metatable.write(metadata)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; return discordDB; }()); exports.discordDB = discordDB; //# sourceMappingURL=db-discord.js.map