Files
1800queue/dist/queue.js
2022-01-31 17:39:06 -06:00

174 lines
5.7 KiB
JavaScript

"use strict";
/* TODO
- queue timeout
- after 5 mins of inactivity, the queue will close
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.queueInfo = exports.cancelQueue = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = void 0;
const discord_js_1 = require("discord.js");
const util_1 = require("./util");
//maps ChannelID to QueueInfo
const QUEUE = new Map();
/**
* get the GuildMember of an interaction
* @param interaction
* @throws string message if it cannot be read
* @returns member
*/
function getMember(interaction) {
let member = interaction.member;
if (!(member instanceof discord_js_1.GuildMember))
throw 'Unable to retrieve guild member information, please try again';
return member;
}
/**
* get the TextChannel of an interaction
* @param interaction
* @throws string message if it cannot be read
* @returns member
*/
function getChannel(interaction) {
let channel = interaction.channel;
if (!(channel instanceof discord_js_1.TextChannel))
throw 'Unable to retrieve text channel information, please try again';
return channel;
}
/**
* get the queueInfo of an interaction
* @param interaction
* @throws string message if it does not exist
* @returns queue info
*/
function getInfo(interaction) {
let info = QUEUE.get(interaction.channelId);
if (!info)
throw 'There is not an active queue in this channel, type `/queue` to create one';
return info;
}
/**
* compiles all the get functions above
* @param interaction
* @throws string message if it does not exist
* @returns object containing each
*/
const getAll = (interaction) => ({
member: getMember(interaction),
channel: getChannel(interaction),
info: getInfo(interaction)
});
/**
* checks if the interaction data is already in the queue
* @param interaction
* @returns boolean
*/
function queueContains(interaction) {
let { member, info } = getAll(interaction);
if (info.players.map(m => m.id).includes(member.id))
return true;
return false;
}
exports.queueContains = queueContains;
/**
* creates a queue from an interaction
* @param interaction
* @throws string message if it cannot be created
*/
async function createQueue(interaction) {
let { channelId } = interaction, member = getMember(interaction), teamsize = interaction.options.getInteger('teamsize', true);
if (QUEUE.has(channelId))
throw 'There is already an active queue in this channel, ' + (queueContains(interaction) ? 'and you are already in it' : 'type `/join` to join'); //and you are already in it
QUEUE.set(channelId, {
players: [
member
],
initiator: member,
teamsize: teamsize
});
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
}
exports.createQueue = createQueue;
/**
* joins a queue from an interaction
* @param interaction
* @throws string message if it cannot be joined
*/
async function joinQueue(interaction) {
let { member, info } = getAll(interaction);
if (queueContains(interaction))
throw 'You are already in the active queue';
info.players.push(member);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Joined the queue');
}
exports.joinQueue = joinQueue;
/**
* leaves a queue from an interaction
* @param interaction
* @throws string message if it cannot be left
*/
async function leaveQueue(interaction) {
let { member, info } = getAll(interaction);
if (!queueContains(interaction))
throw 'You aren\'t in the active queue';
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Left the queue');
}
exports.leaveQueue = leaveQueue;
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be readied
*/
async function readyQueue(interaction) {
let { member, info } = getAll(interaction), { initiator } = info;
if (member.id !== initiator.id)
throw 'Only the queue initiator can ready the queue';
//reset queue
QUEUE.delete(interaction.channelId);
if (info.players.filter(m => m.id !== initiator.id).length === 0)
throw '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));
//convert teams to strings
let teamsStr = [];
teams.forEach((team, i) => {
let str = [`Team ${i + 1}`];
team.forEach(m => str.push(` ${m.user.tag}`));
teamsStr.push(str.join('\n'));
});
await interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```');
}
exports.readyQueue = readyQueue;
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be reset
*/
async function cancelQueue(interaction) {
let { member, channel } = getAll(interaction);
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
throw 'You do not have permission to run this command';
//reset queue
QUEUE.delete(interaction.channelId);
await interaction.reply('Queue has been reset');
}
exports.cancelQueue = cancelQueue;
/**
* sends the queue information from an interaction
* @param interaction
* @throws string message if it cannot be read
*/
async function queueInfo(interaction) {
let info = getInfo(interaction);
await interaction.reply('```' + `
players: ${info.players.map(p => p.user.tag).join('\n ')}
initiator: ${info.initiator.user.tag}
teamsize: ${info.teamsize}
` + '```');
}
exports.queueInfo = queueInfo;