@ayanaware/bentocord
Version:
Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.
88 lines • 3.56 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptManager = void 0;
const bento_1 = require("@ayanaware/bento");
const logger_api_1 = require("@ayanaware/logger-api");
const eris_1 = require("eris");
const Discord_1 = require("../discord/Discord");
const DiscordEvent_1 = require("../discord/constants/DiscordEvent");
const log = logger_api_1.Logger.get();
class PromptManager {
constructor() {
this.name = '@ayanaware/bentocord:PromptManager';
this.prompts = new Map();
}
async onUnload() {
// close all prompts onUnload
for (const [key, [, close]] of this.prompts) {
if (!close)
continue;
try {
await close({ key: 'BENTOCORD_PROMPTMANAGER_UNLOAD', backup: 'The manager is unloading.' });
}
catch { /* NO-OP */ }
this.prompts.delete(key);
}
}
hasPrompt(channelId, userId) {
const key = `${channelId}.${userId}`;
return this.prompts.has(key);
}
async addPrompt(channelId, userId, handler, close) {
const key = `${channelId}.${userId}`;
if (!close)
close = async () => { };
// close any other open prompts
await this.closePrompt(channelId, userId, {
key: 'BENTOCORD_PROMPT_CANCELED_NEW', backup: 'New prompt was opened.'
});
this.prompts.set(key, [handler, close]);
}
async removePrompt(channelId, userId) {
const key = `${channelId}.${userId}`;
this.prompts.delete(key);
}
async closePrompt(channelId, userId, reason) {
const key = `${channelId}.${userId}`;
const [, close] = this.prompts.get(key) ?? [];
if (!close)
return;
try {
await close(reason);
}
catch { /* NO-OP */ }
this.prompts.delete(key);
}
async handleResponse(channelId, userId, response, message) {
const key = `${channelId}.${userId}`;
const [promptHandler] = this.prompts.get(key) ?? [];
if (!promptHandler)
return;
try {
await promptHandler(response, message);
}
catch (e) {
log.error(`Prompt Handler Error: ${e}`);
}
}
async handleMessage(message) {
return this.handleResponse(message.channel.id, message.author.id, message.content, message);
}
}
__decorate([
(0, bento_1.Subscribe)(Discord_1.Discord, DiscordEvent_1.DiscordEvent.MESSAGE_CREATE),
__metadata("design:type", Function),
__metadata("design:paramtypes", [eris_1.Message]),
__metadata("design:returntype", Promise)
], PromptManager.prototype, "handleMessage", null);
exports.PromptManager = PromptManager;
//# sourceMappingURL=PromptManager.js.map