190 lines
6.5 KiB
JavaScript
190 lines
6.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.cancelQueue = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = exports.getAll = exports.getInfo = void 0;
|
|
const discord_js_1 = require("discord.js");
|
|
const util_1 = require("./util");
|
|
//maps ChannelID to QueueInfo
|
|
const QUEUE = new Map();
|
|
/**
|
|
* get the queueInfo of an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it does not exist
|
|
* @returns queue info
|
|
*/
|
|
function getInfo(interaction) {
|
|
let info = QUEUE.get(interaction.channelId);
|
|
if (!info)
|
|
throw (0, util_1.emsg)('There is not an active queue in this channel, type `/queue` to create one');
|
|
return info;
|
|
}
|
|
exports.getInfo = getInfo;
|
|
/**
|
|
* compiles all the get functions above
|
|
* @param interaction
|
|
* @throws if another get function throws
|
|
* @returns object containing each
|
|
*/
|
|
const getAll = (interaction) => ({
|
|
member: (0, util_1.getMember)(interaction),
|
|
channel: (0, util_1.getChannel)(interaction),
|
|
info: getInfo(interaction)
|
|
});
|
|
exports.getAll = getAll;
|
|
/**
|
|
* checks if the interaction data is already in the queue
|
|
* @param interaction
|
|
* @returns boolean
|
|
*/
|
|
function queueContains(interaction) {
|
|
let { member, info } = (0, exports.getAll)(interaction);
|
|
if (info.players.map(m => m.id).includes(member.id))
|
|
return true;
|
|
return false;
|
|
}
|
|
exports.queueContains = queueContains;
|
|
/**
|
|
* creates the timeout for the queue
|
|
* @param interaction
|
|
* @returns time timeout identifier
|
|
*/
|
|
function setQueueTimeout(interaction) {
|
|
let channel = (0, util_1.getChannel)(interaction);
|
|
return setTimeout(() => {
|
|
clearQueue(interaction);
|
|
channel.send('Queue has been reset due to inactivity');
|
|
}, 5 * 60 * 1000); //5 minutes
|
|
}
|
|
/**
|
|
* updates the rich embed for the current queue
|
|
* @param interaction
|
|
*/
|
|
async function sendQueueEmbed(interaction, closed = false) {
|
|
let info = getInfo(interaction), origInteraction = info.initiator.interaction;
|
|
let embed = new discord_js_1.MessageEmbed()
|
|
.setTitle('Queue')
|
|
.setAuthor({
|
|
name: info.initiator.member.displayName,
|
|
iconURL: info.initiator.member.displayAvatarURL({ dynamic: true })
|
|
})
|
|
.addField('Team Size', info.teamsize.toString(), true)
|
|
.addField('Players Joined', info.players.length.toString(), true)
|
|
.setFooter({ text: closed ? 'queue is finished' : 'type /join' });
|
|
if (origInteraction.deferred || origInteraction.replied)
|
|
await origInteraction.editReply({ embeds: [embed] });
|
|
else
|
|
await origInteraction.reply({ embeds: [embed] });
|
|
}
|
|
/**
|
|
* sends the list of teams
|
|
* @param interaction
|
|
*/
|
|
async function sendTeamsEmbed(interaction, teams) {
|
|
let embed = new discord_js_1.MessageEmbed()
|
|
.setTitle('Teams');
|
|
teams.forEach((team, i) => {
|
|
team.map(m => m.user.tag);
|
|
embed.addField(`Title ${i + 1}`, team.join('\n'));
|
|
});
|
|
interaction.reply({ embeds: [embed] });
|
|
}
|
|
/**
|
|
* sends the list of teams
|
|
* @param interaction
|
|
*/
|
|
async function clearQueue(interaction) {
|
|
let info = getInfo(interaction);
|
|
sendQueueEmbed(interaction, true);
|
|
clearTimeout(info.timeout);
|
|
QUEUE.delete(interaction.channelId);
|
|
}
|
|
/**
|
|
* creates a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be created
|
|
*/
|
|
async function createQueue(interaction) {
|
|
let member = (0, util_1.getMember)(interaction), { channelId } = interaction, teamsize = interaction.options.getInteger('teamsize', true);
|
|
if (QUEUE.has(channelId))
|
|
throw (0, util_1.emsg)('There is already an active queue in this channel, ' + (queueContains(interaction) ? 'and you are already in it' : 'type `/join` to join'));
|
|
QUEUE.set(channelId, {
|
|
players: [
|
|
member
|
|
],
|
|
initiator: {
|
|
interaction,
|
|
member
|
|
},
|
|
teamsize: teamsize,
|
|
timeout: setQueueTimeout(interaction)
|
|
});
|
|
sendQueueEmbed(interaction);
|
|
}
|
|
exports.createQueue = createQueue;
|
|
/**
|
|
* joins a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be joined
|
|
*/
|
|
async function joinQueue(interaction) {
|
|
let { member, info } = (0, exports.getAll)(interaction);
|
|
if (queueContains(interaction))
|
|
throw (0, util_1.emsg)('You are already in the active queue');
|
|
info.players.push(member);
|
|
clearTimeout(info.timeout);
|
|
info.timeout = setQueueTimeout(interaction);
|
|
QUEUE.set(interaction.channelId, info);
|
|
sendQueueEmbed(interaction);
|
|
await interaction.reply('Joined the queue');
|
|
}
|
|
exports.joinQueue = joinQueue;
|
|
/**
|
|
* leaves a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be left
|
|
*/
|
|
async function leaveQueue(interaction) {
|
|
let { member, info } = (0, exports.getAll)(interaction);
|
|
if (!queueContains(interaction))
|
|
throw (0, util_1.emsg)('You aren\'t in the active queue');
|
|
info.players.splice(info.players.indexOf(member), 1);
|
|
clearTimeout(info.timeout);
|
|
info.timeout = setQueueTimeout(interaction);
|
|
QUEUE.set(interaction.channelId, info);
|
|
sendQueueEmbed(interaction);
|
|
await interaction.reply('Left the queue');
|
|
}
|
|
exports.leaveQueue = leaveQueue;
|
|
/**
|
|
* readys a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be readied
|
|
*/
|
|
async function readyQueue(interaction) {
|
|
let { member, info } = (0, exports.getAll)(interaction), { initiator } = info;
|
|
if (member.id !== initiator.member.id)
|
|
throw (0, util_1.emsg)('Only the queue initiator can ready the queue');
|
|
clearQueue(interaction);
|
|
if (info.players.filter(m => m.id !== initiator.member.id).length === 0)
|
|
throw (0, util_1.emsg)('Nobody signed up for the queue, the queue has been reset');
|
|
//team data
|
|
let playerlist = (0, util_1.shuffle)(info.players), teams = [];
|
|
//fill team data
|
|
for (let i = 0; i < playerlist.length; i += info.teamsize)
|
|
teams.push(playerlist.slice(i, i + info.teamsize));
|
|
sendTeamsEmbed(interaction, teams);
|
|
}
|
|
exports.readyQueue = readyQueue;
|
|
/**
|
|
* readys a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be reset
|
|
*/
|
|
async function cancelQueue(interaction) {
|
|
let { info, member, channel } = (0, exports.getAll)(interaction);
|
|
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
|
|
throw (0, util_1.emsg)('You do not have permission to run this command');
|
|
clearQueue(interaction);
|
|
await interaction.reply('Queue has been reset');
|
|
}
|
|
exports.cancelQueue = cancelQueue;
|