added inactivity timeout
This commit is contained in:
25
dist/queue.js
vendored
25
dist/queue.js
vendored
@@ -1,14 +1,22 @@
|
||||
"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();
|
||||
/**
|
||||
* creates the timeout for the queue
|
||||
* @param interaction
|
||||
* @returns time timeout identifier
|
||||
*/
|
||||
function setQueueTimeout(interaction) {
|
||||
let channel = getChannel(interaction);
|
||||
return setTimeout(() => {
|
||||
QUEUE.delete(channel.id);
|
||||
channel.send('Queue has been reset due to inactivity');
|
||||
}, 5 * 60 * 1000); //5 minutes
|
||||
}
|
||||
/**
|
||||
* get the GuildMember of an interaction
|
||||
* @param interaction
|
||||
@@ -74,7 +82,7 @@ exports.queueContains = queueContains;
|
||||
* @throws string message if it cannot be created
|
||||
*/
|
||||
async function createQueue(interaction) {
|
||||
let { channelId } = interaction, member = getMember(interaction), teamsize = interaction.options.getInteger('teamsize', true);
|
||||
let member = getMember(interaction), { channelId } = 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, {
|
||||
@@ -82,7 +90,8 @@ async function createQueue(interaction) {
|
||||
member
|
||||
],
|
||||
initiator: member,
|
||||
teamsize: teamsize
|
||||
teamsize: teamsize,
|
||||
timeout: setQueueTimeout(interaction)
|
||||
});
|
||||
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
|
||||
}
|
||||
@@ -97,6 +106,8 @@ async function joinQueue(interaction) {
|
||||
if (queueContains(interaction))
|
||||
throw 'You are already in the active queue';
|
||||
info.players.push(member);
|
||||
clearTimeout(info.timeout);
|
||||
info.timeout = setQueueTimeout(interaction);
|
||||
QUEUE.set(interaction.channelId, info);
|
||||
await interaction.reply('Joined the queue');
|
||||
}
|
||||
@@ -111,6 +122,8 @@ async function leaveQueue(interaction) {
|
||||
if (!queueContains(interaction))
|
||||
throw '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);
|
||||
await interaction.reply('Left the queue');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user