added cancel command

This commit is contained in:
2022-01-31 17:39:06 -06:00
parent 50c297bfea
commit f0283145dc
6 changed files with 115 additions and 22 deletions

View File

@@ -1,5 +1,9 @@
import { info } from "console";
import { CommandInteraction, GuildMember } from "discord.js";
/* TODO
- queue timeout
- after 5 mins of inactivity, the queue will close
*/
import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
import { shuffle } from "./util";
type queueInfo = {
@@ -26,6 +30,21 @@ function getMember(interaction: CommandInteraction): GuildMember {
return member;
}
/**
* get the TextChannel of an interaction
* @param interaction
* @throws string message if it cannot be read
* @returns member
*/
function getChannel(interaction: CommandInteraction): TextChannel {
let channel = interaction.channel;
if (!(channel instanceof TextChannel))
throw 'Unable to retrieve text channel information, please try again';
return channel;
}
/**
* get the queueInfo of an interaction
* @param interaction
@@ -41,6 +60,18 @@ function getInfo(interaction: CommandInteraction): queueInfo {
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: CommandInteraction) => ({
member: getMember(interaction),
channel: getChannel(interaction),
info: getInfo(interaction)
});
/**
* checks if the interaction data is already in the queue
* @param interaction
@@ -48,8 +79,7 @@ function getInfo(interaction: CommandInteraction): queueInfo {
*/
export function queueContains(interaction: CommandInteraction): boolean {
let member = getMember(interaction),
info = getInfo(interaction);
let {member, info} = getAll(interaction);
if (info.players.map(m=>m.id).includes(member.id))
return true;
@@ -91,8 +121,7 @@ export async function createQueue(interaction: CommandInteraction) {
*/
export async function joinQueue(interaction: CommandInteraction) {
let member = getMember(interaction),
info = getInfo(interaction);
let {member, info} = getAll(interaction);
if (queueContains(interaction))
throw 'You are already in the active queue';
@@ -112,8 +141,7 @@ export async function joinQueue(interaction: CommandInteraction) {
*/
export async function leaveQueue(interaction: CommandInteraction) {
let member = getMember(interaction),
info = getInfo(interaction);
let {member, info} = getAll(interaction);
if (!queueContains(interaction))
throw 'You aren\'t in the active queue';
@@ -133,8 +161,7 @@ export async function leaveQueue(interaction: CommandInteraction) {
*/
export async function readyQueue(interaction: CommandInteraction) {
let member = getMember(interaction),
info = getInfo(interaction),
let {member, info} = getAll(interaction),
{initiator} = info;
if (member.id !== initiator.id)
@@ -168,6 +195,25 @@ export async function readyQueue(interaction: CommandInteraction) {
}
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be reset
*/
export async function cancelQueue(interaction: CommandInteraction) {
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');
}
/**
* sends the queue information from an interaction
* @param interaction