discord.js-collector-utils
Version:
Collector utilities for discord.js.
449 lines • 31.2 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 });
var discord_js_1 = require("discord.js");
var _1 = require(".");
var Config = require('../config/config.json');
function start() {
return __awaiter(this, void 0, void 0, function () {
var client;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
client = new discord_js_1.Client({
intents: [
discord_js_1.IntentsBitField.Flags.Guilds,
discord_js_1.IntentsBitField.Flags.GuildMessages,
discord_js_1.IntentsBitField.Flags.GuildMessageReactions,
discord_js_1.IntentsBitField.Flags.MessageContent,
],
});
client.on(discord_js_1.Events.ClientReady, function () {
console.log("Logged in as '".concat(client.user.tag, "'!"));
});
client.on(discord_js_1.Events.MessageCreate, function (event) { return __awaiter(_this, void 0, void 0, function () {
var client_1, channel_1, user, args, command, subCommand, _a, prompt_1, result, prompt_2, result, prompt_3, result, prompt_4, favoriteFruit, favoriteColor, error_1;
var _this = this;
var _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_d.trys.push([0, 26, , 27]);
client_1 = event.client;
channel_1 = event.channel;
user = event.author;
args = event.content.split(' ');
command = (_b = args[0]) === null || _b === void 0 ? void 0 : _b.toLowerCase();
if (command !== 'test') {
return [2 /*return*/];
}
subCommand = (_c = args[1]) === null || _c === void 0 ? void 0 : _c.toLowerCase();
if (!!subCommand) return [3 /*break*/, 2];
return [4 /*yield*/, channel_1.send('Please supply a test to run.')];
case 1:
_d.sent();
return [2 /*return*/];
case 2:
_a = subCommand;
switch (_a) {
case 'button': return [3 /*break*/, 3];
case 'select-menu': return [3 /*break*/, 7];
case 'modal': return [3 /*break*/, 11];
case 'reaction': return [3 /*break*/, 15];
case 'message': return [3 /*break*/, 19];
}
return [3 /*break*/, 23];
case 3: return [4 /*yield*/, channel_1.send({
content: 'Please select your favorite fruit!',
components: [
{
type: discord_js_1.ComponentType.ActionRow,
components: [
{
type: discord_js_1.ComponentType.Button,
customId: 'watermelon',
emoji: '🍉',
style: discord_js_1.ButtonStyle.Primary,
},
{
type: discord_js_1.ComponentType.Button,
customId: 'apple',
emoji: '🍎',
style: discord_js_1.ButtonStyle.Primary,
},
{
type: discord_js_1.ComponentType.Button,
customId: 'banana',
emoji: '🍌',
style: discord_js_1.ButtonStyle.Primary,
},
],
},
],
})];
case 4:
prompt_1 = _d.sent();
return [4 /*yield*/, _1.CollectorUtils.collectByButton(prompt_1,
// Retrieve Result
function (buttonInteraction) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (buttonInteraction.customId) {
case 'watermelon':
return [2 /*return*/, { intr: buttonInteraction, value: 'Watermelon' }];
case 'apple':
return [2 /*return*/, { intr: buttonInteraction, value: 'Apple' }];
case 'banana':
return [2 /*return*/, { intr: buttonInteraction, value: 'Banana' }];
default:
return [2 /*return*/];
}
return [2 /*return*/];
});
}); },
// Options
{
time: 10000,
reset: true,
target: user,
stopFilter: function (message) { return message.content.toLowerCase() === 'stop'; },
onExpire: function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, channel_1.send('Too slow! Try being more decisive next time.')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
})];
case 5:
result = _d.sent();
if (result === undefined) {
return [2 /*return*/];
}
return [4 /*yield*/, result.intr.reply("You selected **".concat(result.value, "**. Nice choice!"))];
case 6:
_d.sent();
return [2 /*return*/];
case 7: return [4 /*yield*/, channel_1.send({
content: 'Please select your favorite fruit!',
components: [
{
type: discord_js_1.ComponentType.ActionRow,
components: [
{
type: discord_js_1.ComponentType.SelectMenu,
customId: 'select_menu',
options: [
{
emoji: '🍉',
label: 'Watermelon',
value: 'Watermelon',
},
{
emoji: '🍎',
label: 'Apple',
value: 'Apple',
},
{
emoji: '🍌',
label: 'Banana',
value: 'Banana',
},
],
},
],
},
],
})];
case 8:
prompt_2 = _d.sent();
return [4 /*yield*/, _1.CollectorUtils.collectBySelectMenu(prompt_2,
// Retrieve Result
function (selectMenuInteraction) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, {
intr: selectMenuInteraction,
value: selectMenuInteraction.values[0],
}];
});
}); },
// Options
{
time: 10000,
reset: true,
target: user,
stopFilter: function (message) { return message.content.toLowerCase() === 'stop'; },
onExpire: function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, channel_1.send('Too slow! Try being more decisive next time.')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
})];
case 9:
result = _d.sent();
if (result === undefined) {
return [2 /*return*/];
}
return [4 /*yield*/, result.intr.reply("You selected **".concat(result.value, "**. Nice choice!"))];
case 10:
_d.sent();
return [2 /*return*/];
case 11: return [4 /*yield*/, channel_1.send({
content: 'What is your favorite movie?',
components: [
{
type: discord_js_1.ComponentType.ActionRow,
components: [
{
type: discord_js_1.ComponentType.Button,
customId: 'enter_response',
emoji: '⌨️',
label: 'Enter Response',
style: discord_js_1.ButtonStyle.Primary,
},
],
},
],
})];
case 12:
prompt_3 = _d.sent();
return [4 /*yield*/, _1.CollectorUtils.collectByModal(prompt_3, new discord_js_1.ModalBuilder({
customId: 'modal',
title: client_1.user.username,
components: [
{
type: discord_js_1.ComponentType.ActionRow,
components: [
{
type: discord_js_1.ComponentType.TextInput,
customId: 'favorite_movie',
label: 'Favorite Movie',
required: true,
style: discord_js_1.TextInputStyle.Short,
},
],
},
],
}),
// Retrieve Result
function (modalSubmitInteraction) { return __awaiter(_this, void 0, void 0, function () {
var textInput;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
textInput = modalSubmitInteraction.components[0].components[0];
if (textInput.type !== discord_js_1.ComponentType.TextInput) {
return [2 /*return*/];
}
if (!textInput.value.toLowerCase().includes('fight club')) return [3 /*break*/, 2];
return [4 /*yield*/, modalSubmitInteraction.reply("We don't talk about fight club. Try again.")];
case 1:
_a.sent();
return [2 /*return*/];
case 2: return [2 /*return*/, { intr: modalSubmitInteraction, value: textInput.value }];
}
});
}); },
// Options
{
time: 10000,
reset: true,
target: user,
stopFilter: function (message) { return message.content.toLowerCase() === 'stop'; },
onExpire: function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, channel_1.send('Too slow! Try being more decisive next time.')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
})];
case 13:
result = _d.sent();
if (result === undefined) {
return [2 /*return*/];
}
return [4 /*yield*/, result.intr.reply("Oh, **".concat(result.value, "**? That one's hilarious!"))];
case 14:
_d.sent();
return [2 /*return*/];
case 15: return [4 /*yield*/, channel_1.send('Please select your favorite fruit!')];
case 16:
prompt_4 = _d.sent();
prompt_4.react('🍉');
prompt_4.react('🍎');
prompt_4.react('🍌');
return [4 /*yield*/, _1.CollectorUtils.collectByReaction(prompt_4,
// Retrieve Result
function (messageReaction, user) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (messageReaction.emoji.name) {
case '🍉':
return [2 /*return*/, 'Watermelon'];
case '🍎':
return [2 /*return*/, 'Apple'];
case '🍌':
return [2 /*return*/, 'Banana'];
default:
return [2 /*return*/];
}
return [2 /*return*/];
});
}); },
// Options
{
time: 10000,
reset: true,
target: user,
stopFilter: function (message) { return message.content.toLowerCase() === 'stop'; },
onExpire: function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, channel_1.send('Too slow! Try being more decisive next time.')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
})];
case 17:
favoriteFruit = _d.sent();
if (favoriteFruit === undefined) {
return [2 /*return*/];
}
return [4 /*yield*/, channel_1.send("You selected **".concat(favoriteFruit, "**. Nice choice!"))];
case 18:
_d.sent();
return [2 /*return*/];
case 19: return [4 /*yield*/, channel_1.send('What is your favorite color?')];
case 20:
_d.sent();
return [4 /*yield*/, _1.CollectorUtils.collectByMessage(channel_1,
// Retrieve Result
function (message) { return __awaiter(_this, void 0, void 0, function () {
var colorOptions, favoriteColor;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
colorOptions = [
'red',
'orange',
'yellow',
'green',
'blue',
'purple',
];
favoriteColor = colorOptions.find(function (colorOption) { return colorOption === message.content.toLowerCase(); });
if (!!favoriteColor) return [3 /*break*/, 2];
return [4 /*yield*/, channel_1.send("Sorry, that color is not an option.")];
case 1:
_a.sent();
return [2 /*return*/];
case 2:
if (!(favoriteColor === 'yellow')) return [3 /*break*/, 4];
return [4 /*yield*/, channel_1.send("Ew, **yellow**?! Please choose a better color.")];
case 3:
_a.sent();
return [2 /*return*/];
case 4: return [2 /*return*/, favoriteColor];
}
});
}); },
// Options
{
time: 10000,
reset: true,
target: user,
stopFilter: function (message) { return message.content.toLowerCase() === 'stop'; },
onExpire: function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, channel_1.send("Too slow! Try being more decisive next time.")];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
})];
case 21:
favoriteColor = _d.sent();
if (favoriteColor === undefined) {
return [2 /*return*/];
}
return [4 /*yield*/, channel_1.send("You selected **".concat(favoriteColor, "**. Nice choice!"))];
case 22:
_d.sent();
return [2 /*return*/];
case 23: return [4 /*yield*/, channel_1.send('Unknown test.')];
case 24:
_d.sent();
_d.label = 25;
case 25: return [3 /*break*/, 27];
case 26:
error_1 = _d.sent();
console.log(error_1);
return [3 /*break*/, 27];
case 27: return [2 /*return*/];
}
});
}); });
return [4 /*yield*/, client.login(Config.client.token)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
start().catch(function (error) {
console.error(error);
});
//# sourceMappingURL=test.js.map