added cancel command
This commit is contained in:
7
dist/discord.js
vendored
7
dist/discord.js
vendored
@@ -13,7 +13,8 @@ const commands = [
|
||||
type: 4,
|
||||
name: 'teamsize',
|
||||
description: 'size of each team',
|
||||
required: true
|
||||
required: true,
|
||||
min_value: 1
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -30,8 +31,8 @@ const commands = [
|
||||
description: 'ready the queue and display team info'
|
||||
},
|
||||
{
|
||||
name: 'queueinfo',
|
||||
description: 'get info of the current queue'
|
||||
name: 'cancel',
|
||||
description: 'cancels the current queue (must have the Manage Messages permission)'
|
||||
},
|
||||
{
|
||||
name: 'elo',
|
||||
|
||||
2
dist/index.js
vendored
2
dist/index.js
vendored
@@ -51,6 +51,8 @@ CLIENT.on('interactionCreate', async (interaction) => {
|
||||
await (0, queue_1.leaveQueue)(interaction);
|
||||
else if (interaction.commandName === 'ready')
|
||||
await (0, queue_1.readyQueue)(interaction);
|
||||
else if (interaction.commandName === 'cancel')
|
||||
await (0, queue_1.cancelQueue)(interaction);
|
||||
else if (interaction.commandName === 'queueinfo')
|
||||
await (0, queue_1.queueInfo)(interaction);
|
||||
else if (interaction.commandName === 'elo')
|
||||
|
||||
51
dist/queue.js
vendored
51
dist/queue.js
vendored
@@ -1,6 +1,10 @@
|
||||
"use strict";
|
||||
/* TODO
|
||||
- queue timeout
|
||||
- after 5 mins of inactivity, the queue will close
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.queueInfo = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = void 0;
|
||||
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
|
||||
@@ -17,6 +21,18 @@ function getMember(interaction) {
|
||||
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
|
||||
@@ -29,13 +45,24 @@ function getInfo(interaction) {
|
||||
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 = getMember(interaction), info = getInfo(interaction);
|
||||
let { member, info } = getAll(interaction);
|
||||
if (info.players.map(m => m.id).includes(member.id))
|
||||
return true;
|
||||
return false;
|
||||
@@ -66,7 +93,7 @@ exports.createQueue = createQueue;
|
||||
* @throws string message if it cannot be joined
|
||||
*/
|
||||
async function joinQueue(interaction) {
|
||||
let member = getMember(interaction), info = getInfo(interaction);
|
||||
let { member, info } = getAll(interaction);
|
||||
if (queueContains(interaction))
|
||||
throw 'You are already in the active queue';
|
||||
info.players.push(member);
|
||||
@@ -80,7 +107,7 @@ exports.joinQueue = joinQueue;
|
||||
* @throws string message if it cannot be left
|
||||
*/
|
||||
async function leaveQueue(interaction) {
|
||||
let member = getMember(interaction), info = getInfo(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);
|
||||
@@ -94,7 +121,7 @@ exports.leaveQueue = leaveQueue;
|
||||
* @throws string message if it cannot be readied
|
||||
*/
|
||||
async function readyQueue(interaction) {
|
||||
let member = getMember(interaction), info = getInfo(interaction), { initiator } = info;
|
||||
let { member, info } = getAll(interaction), { initiator } = info;
|
||||
if (member.id !== initiator.id)
|
||||
throw 'Only the queue initiator can ready the queue';
|
||||
//reset queue
|
||||
@@ -116,6 +143,20 @@ async function readyQueue(interaction) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user