UNPKG

kmidjourney

Version:

Node.js client for the unofficial MidJourney API.

136 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MidjourneyMessage = void 0; const tslib_1 = require("tslib"); const queue_1 = require("./queue"); const utls_1 = require("./utls"); class MidjourneyMessage { constructor(ChannelId, SalaiToken, debug = false, Limit = 50, uid = '', prompt = '', maxWait = 500) { this.ChannelId = ChannelId; this.SalaiToken = SalaiToken; this.debug = debug; this.Limit = Limit; this.uid = uid; this.prompt = prompt; this.maxWait = maxWait; this.magApiQueue = (0, queue_1.CreateQueue)(1); this.log("MidjourneyMessage constructor"); } log(...args) { this.debug && console.log(...args, new Date().toISOString()); } FilterMessages(prompt, loading, options, index) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const data = yield this.safeRetrieveMessages(this.Limit); for (let i = 0; i < data.length; i++) { const item = data[i]; if (item.author.id === "936929561302675456" && (item.content.includes(`**${prompt}`) || item.id == this.uid || (item.content.includes(this.prompt) && this.prompt != ''))) { this.uid = item.id; if (prompt != item.content) { this.prompt = item.content.split("**")[1]; } // Upscaled or Variation if (options && !(item.content.includes(options) || (options === "Upscaled" && item.content.includes(`Image #${index}`)))) { this.log("no options"); continue; } if (item.attachments.length === 0) { this.log("no attachment"); break; } const imageUrl = item.attachments[0].url; //waiting if (item.attachments[0].filename.startsWith("grid") || item.components.length === 0) { this.log(`content`, item.content); const regex = /\(([^)]+)\)/; // matches the value inside the first parenthesis const match = item.content.match(regex); let progress = "wait"; if (match) { progress = match[1]; } else { this.log("No match found"); } loading === null || loading === void 0 ? void 0 : loading(imageUrl, progress); break; } //finished this.log(`finished`, item.content); const content = item.content.split("**")[1]; const msg = { id: item.id, uri: imageUrl, hash: this.UriToHash(imageUrl), content: content, progress: "done", }; this.uid = ''; this.prompt = ''; return msg; } } return null; }); } UriToHash(uri) { var _a, _b; return (_b = (_a = uri.split("_").pop()) === null || _a === void 0 ? void 0 : _a.split(".")[0]) !== null && _b !== void 0 ? _b : ""; } WaitMessage(prompt, loading) { return tslib_1.__awaiter(this, void 0, void 0, function* () { for (let i = 0; i < this.maxWait; i++) { const msg = yield this.FilterMessages(prompt, loading); if (msg !== null) { return msg; } this.log(i, "wait no message found"); yield (0, utls_1.sleep)(1000 * 2); } }); } WaitOptionMessage(content, options, loading) { return tslib_1.__awaiter(this, void 0, void 0, function* () { for (let i = 0; i < this.maxWait; i++) { const msg = yield this.FilterMessages(content, loading, options); if (msg !== null) { return msg; } this.log(i, content, "wait no message found"); yield (0, utls_1.sleep)(1000 * 2); } }); } WaitUpscaledMessage(content, index, loading) { return tslib_1.__awaiter(this, void 0, void 0, function* () { for (let i = 0; i < this.maxWait; i++) { const msg = yield this.FilterMessages(content, loading, "Upscaled", index); if (msg !== null) { return msg; } this.log(i, content, "wait no message found"); yield (0, utls_1.sleep)(1000 * 2); } }); } // limit the number of concurrent interactions safeRetrieveMessages(limit = 50) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.magApiQueue.addTask(() => this.RetrieveMessages(limit)); }); } RetrieveMessages(limit = 50) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const headers = { authorization: this.SalaiToken }; const response = yield fetch(`https://discord.com/api/v10/channels/${this.ChannelId}/messages?limit=${limit}`, { headers: headers, }); const data = yield response.json(); return data; }); } } exports.MidjourneyMessage = MidjourneyMessage; //# sourceMappingURL=midjourney.message.js.map