UNPKG

kbotify

Version:

kaiheila bot framework

65 lines 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GuildSession = void 0; const guild_1 = require("../guild"); const message_1 = require("../message"); const user_guild_1 = require("../user/user.guild"); const logger_1 = require("../logger"); const session_base_1 = require("./session.base"); class GuildSession extends session_base_1.BaseSession { constructor(command, args, msg, client) { super(command, args, msg, client); /** * 等待用户在当前频道发送的下一条消息。 * * @param {RegExp} condition * @param {(number | undefined)} [timeout=6e4] timeout time, in **ms** * @memberof GuildSession */ this.awaitMessage = async (condition, timeout = 6e4) => { if (timeout < 1e3) { logger_1.kBotifyLogger.warn(`timeout too short: ${timeout}, ${this}`); } const collector = this.client.collectors.user.create(this.userId, timeout); const result = new Promise((resolve, reject) => { collector.on('add', (message) => { if (condition.test(message.content)) { resolve(message); collector.stop(); } }); collector.on('stop', () => { resolve(undefined); }); collector.on('cancel', () => { reject(`new collector for ${this} is set, cancelling current collector`); }); }); return result; }; if (!msg.guildId) { throw new TypeError('getting msg without guildId'); } this.guild = new guild_1.Guild(msg.guildId, this.client); // TODO if (msg instanceof message_1.TextMessage) { this.userId = msg.authorId; this.user = new user_guild_1.GuildUser(msg.author, this.guild.id, this.client); } else { this.userId = msg.userId; this.user = new user_guild_1.GuildUser(msg.user, this.guild.id, this.client); } } } exports.GuildSession = GuildSession; GuildSession.fromSession = async (session, full = false) => { if (!session.guild?.id) { throw new Error('Trying to construct GuildSession without guild id'); } if (full && !(session.msg instanceof message_1.TextMessage)) { const user = new user_guild_1.GuildUser(session.user, session.guild.id, session.client); session.msg.user = await user.full(); } return new GuildSession(session.command, session.args, session.msg, session.client); }; //# sourceMappingURL=session.guild.js.map