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');
|
||||
}
|
||||
|
||||
32
src/queue.ts
32
src/queue.ts
@@ -1,20 +1,29 @@
|
||||
/* 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 = {
|
||||
players: GuildMember[],
|
||||
initiator: GuildMember,
|
||||
teamsize: number
|
||||
teamsize: number,
|
||||
timeout: NodeJS.Timeout
|
||||
}
|
||||
|
||||
//maps ChannelID to QueueInfo
|
||||
const QUEUE = new Map<string, queueInfo>();
|
||||
|
||||
/**
|
||||
* creates the timeout for the queue
|
||||
* @param interaction
|
||||
* @returns time timeout identifier
|
||||
*/
|
||||
function setQueueTimeout(interaction: CommandInteraction) {
|
||||
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
|
||||
@@ -95,8 +104,8 @@ export function queueContains(interaction: CommandInteraction): boolean {
|
||||
*/
|
||||
export async function createQueue(interaction: CommandInteraction) {
|
||||
|
||||
let {channelId} = interaction,
|
||||
member = getMember(interaction),
|
||||
let member = getMember(interaction),
|
||||
{channelId} = interaction,
|
||||
teamsize = interaction.options.getInteger('teamsize', true);
|
||||
|
||||
if (QUEUE.has(channelId))
|
||||
@@ -107,7 +116,8 @@ export async function createQueue(interaction: CommandInteraction) {
|
||||
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`);
|
||||
@@ -127,6 +137,8 @@ export async function joinQueue(interaction: CommandInteraction) {
|
||||
throw 'You are already in the active queue';
|
||||
|
||||
info.players.push(member);
|
||||
clearTimeout(info.timeout);
|
||||
info.timeout = setQueueTimeout(interaction)
|
||||
|
||||
QUEUE.set(interaction.channelId, info);
|
||||
|
||||
@@ -147,6 +159,8 @@ export async function leaveQueue(interaction: CommandInteraction) {
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user