discord.js-collector-utils
Version:
Collector utilities for discord.js.
573 lines • 33.8 kB
JavaScript
"use strict";
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 __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 (g && (g = 0, op[0] && (_ = 0)), _) 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 });
exports.CollectorUtils = void 0;
var discord_js_1 = require("discord.js");
var CollectorUtils = /** @class */ (function () {
function CollectorUtils() {
}
/**
* Collect a response by buttons.
* @param message Message to collect button interactions on.
* @param retriever Method which takes a collected button interaction and returns a desired result, or `undefined` if invalid.
* @param options Options for collection.
* @returns A desired result, or `undefined` if the collector expired.
*/
CollectorUtils.collectByButton = function (message, retriever, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
options = Object.assign({
time: 120000,
reset: true,
}, options);
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var btnCollector, stopCollector, expired;
var _this = this;
return __generator(this, function (_a) {
btnCollector = message.createMessageComponentCollector({
componentType: discord_js_1.ComponentType.Button,
filter: function (intr) {
if (options.target) {
return intr.user.id === options.target.id;
}
return true;
},
time: options.time,
});
stopCollector = message.channel.createMessageCollector({
filter: function (message) {
if (!options.stopFilter) {
return false;
}
var stop = options.stopFilter(message);
if (!stop) {
return false;
}
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
// Make sure message collector is ahead of reaction collector
time: options.time + 1000,
});
expired = true;
btnCollector.on('collect', function (intr) { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, retriever(intr)];
case 1:
result = _a.sent();
if (result === undefined) {
if (options.reset) {
btnCollector.resetTimer();
stopCollector.resetTimer();
}
return [2 /*return*/];
}
else {
expired = false;
btnCollector.stop();
resolve(result);
return [2 /*return*/];
}
return [2 /*return*/];
}
});
}); });
btnCollector.on('end', function (collected) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stopCollector.stop();
if (!(expired && options.onExpire)) return [3 /*break*/, 2];
return [4 /*yield*/, options.onExpire()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
stopCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
expired = false;
btnCollector.stop();
resolve(undefined);
return [2 /*return*/];
});
}); });
return [2 /*return*/];
});
}); })];
});
});
};
/**
* Collect a response by select menu.
* @param message Message to collect select menu interactions on.
* @param retriever Method which takes a collected select menu interaction and returns a desired result, or `undefined` if invalid.
* @param options Options for collection.
* @returns A desired result, or `undefined` if the collector expired.
*/
CollectorUtils.collectBySelectMenu = function (message, retriever, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
options = Object.assign({
time: 120000,
reset: true,
}, options);
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var smCollector, stopCollector, expired;
var _this = this;
return __generator(this, function (_a) {
smCollector = message.createMessageComponentCollector({
componentType: discord_js_1.ComponentType.SelectMenu,
filter: function (intr) {
if (options.target) {
return intr.user.id === options.target.id;
}
return true;
},
time: options.time,
});
stopCollector = message.channel.createMessageCollector({
filter: function (message) {
if (!options.stopFilter) {
return false;
}
var stop = options.stopFilter(message);
if (!stop) {
return false;
}
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
// Make sure message collector is ahead of reaction collector
time: options.time + 1000,
});
expired = true;
smCollector.on('collect', function (intr) { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, retriever(intr)];
case 1:
result = _a.sent();
if (result === undefined) {
if (options.reset) {
smCollector.resetTimer();
stopCollector.resetTimer();
}
return [2 /*return*/];
}
else {
expired = false;
smCollector.stop();
resolve(result);
return [2 /*return*/];
}
return [2 /*return*/];
}
});
}); });
smCollector.on('end', function (collected) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stopCollector.stop();
if (!(expired && options.onExpire)) return [3 /*break*/, 2];
return [4 /*yield*/, options.onExpire()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
stopCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
expired = false;
smCollector.stop();
resolve(undefined);
return [2 /*return*/];
});
}); });
return [2 /*return*/];
});
}); })];
});
});
};
/**
* Collect a response through a modal.
* @param message Message to collect button interactions on.
* @param modal Modal to show when the button is clicked.
* @param retriever Method which takes a collected modal interaction and returns a desired result, or `undefined` if invalid.
* @param options Options for collection.
* @returns A desired result, or `undefined` if the collector expired.
*/
CollectorUtils.collectByModal = function (message, modal, retriever, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
options = Object.assign({
time: 120000,
reset: true,
}, options);
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var btnCollector, stopCollector, expired;
var _this = this;
return __generator(this, function (_a) {
btnCollector = message.createMessageComponentCollector({
componentType: discord_js_1.ComponentType.Button,
filter: function (intr) {
if (options.target) {
return intr.user.id === options.target.id;
}
return true;
},
time: options.time,
});
stopCollector = message.channel.createMessageCollector({
filter: function (message) {
if (!options.stopFilter) {
return false;
}
var stop = options.stopFilter(message);
if (!stop) {
return false;
}
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
// Make sure message collector is ahead of reaction collector
time: options.time + 1000,
});
expired = true;
btnCollector.on('collect', function (intr) { return __awaiter(_this, void 0, void 0, function () {
var modalIntr, error_1, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
modal.setCustomId("modal-".concat(intr.id));
return [4 /*yield*/, intr.showModal(modal)];
case 1:
_a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, intr.awaitModalSubmit({
filter: function (modalIntr) {
return modalIntr.customId === "modal-".concat(intr.id);
},
time: options.time,
})];
case 3:
modalIntr = _a.sent();
return [3 /*break*/, 5];
case 4:
error_1 = _a.sent();
// Timed out
return [2 /*return*/];
case 5: return [4 /*yield*/, retriever(modalIntr)];
case 6:
result = _a.sent();
if (result === undefined) {
if (options.reset) {
btnCollector.resetTimer();
stopCollector.resetTimer();
}
return [2 /*return*/];
}
else {
expired = false;
btnCollector.stop();
resolve(result);
return [2 /*return*/];
}
return [2 /*return*/];
}
});
}); });
btnCollector.on('end', function (collected) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stopCollector.stop();
if (!(expired && options.onExpire)) return [3 /*break*/, 2];
return [4 /*yield*/, options.onExpire()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
stopCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
expired = false;
btnCollector.stop();
resolve(undefined);
return [2 /*return*/];
});
}); });
return [2 /*return*/];
});
}); })];
});
});
};
/**
* Collect a response by reactions.
* @param message Message to collect reactions on.
* @param retriever Method which takes a collected reaction and returns a desired result, or `undefined` if invalid.
* @param options Options for collection.
* @returns A desired result, or `undefined` if the collector expired.
*/
CollectorUtils.collectByReaction = function (message, retriever, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
options = Object.assign({
time: 120000,
reset: true,
}, options);
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var reactCollector, stopCollector, expired;
var _this = this;
return __generator(this, function (_a) {
reactCollector = message.createReactionCollector({
filter: function (msgReaction, reactor) {
if (options.target) {
return reactor.id === options.target.id;
}
return true;
},
time: options.time,
});
stopCollector = message.channel.createMessageCollector({
filter: function (message) {
if (!options.stopFilter) {
return false;
}
var stop = options.stopFilter(message);
if (!stop) {
return false;
}
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
// Make sure message collector is ahead of reaction collector
time: options.time + 1000,
});
expired = true;
reactCollector.on('collect', function (msgReaction, reactor) { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, retriever(msgReaction, reactor)];
case 1:
result = _a.sent();
if (result === undefined) {
if (options.reset) {
reactCollector.resetTimer();
stopCollector.resetTimer();
}
return [2 /*return*/];
}
else {
expired = false;
reactCollector.stop();
resolve(result);
return [2 /*return*/];
}
return [2 /*return*/];
}
});
}); });
reactCollector.on('end', function (collected) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stopCollector.stop();
if (!(expired && options.onExpire)) return [3 /*break*/, 2];
return [4 /*yield*/, options.onExpire()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
stopCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
expired = false;
reactCollector.stop();
resolve(undefined);
return [2 /*return*/];
});
}); });
return [2 /*return*/];
});
}); })];
});
});
};
/**
* Collect a response by messages.
* @param channel Channel to collect messages on.
* @param retriever Method which takes a collected message and returns a desired result, or `undefined` if invalid.
* @param options Options for collection.
* @returns A desired result, or `undefined` if the collector expired.
*/
CollectorUtils.collectByMessage = function (channel, retriever, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
options = Object.assign({
time: 120000,
reset: true,
}, options);
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var msgCollector, stopCollector, expired;
var _this = this;
return __generator(this, function (_a) {
msgCollector = channel.createMessageCollector({
filter: function (message) {
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
time: options.time,
});
stopCollector = channel.createMessageCollector({
filter: function (message) {
if (!options.stopFilter) {
return false;
}
var stop = options.stopFilter(message);
if (!stop) {
return false;
}
if (options.target) {
return message.author.id === options.target.id;
}
return true;
},
// Make sure message collector is ahead of reaction collector
time: options.time + 1000,
});
expired = true;
msgCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
var stop, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stop = options.stopFilter(nextMsg);
if (stop) {
// Let the stopCollector handle
return [2 /*return*/];
}
return [4 /*yield*/, retriever(nextMsg)];
case 1:
result = _a.sent();
if (result === undefined) {
if (options.reset) {
msgCollector.resetTimer();
stopCollector.resetTimer();
}
return [2 /*return*/];
}
else {
expired = false;
msgCollector.stop();
resolve(result);
return [2 /*return*/];
}
return [2 /*return*/];
}
});
}); });
msgCollector.on('end', function (collected) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stopCollector.stop();
if (!(expired && options.onExpire)) return [3 /*break*/, 2];
return [4 /*yield*/, options.onExpire()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
stopCollector.on('collect', function (nextMsg) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
expired = false;
msgCollector.stop();
resolve(undefined);
return [2 /*return*/];
});
}); });
return [2 /*return*/];
});
}); })];
});
});
};
return CollectorUtils;
}());
exports.CollectorUtils = CollectorUtils;
//# sourceMappingURL=index.js.map