added inactivity timeout

This commit is contained in:
2022-01-31 17:47:43 -06:00
parent f0283145dc
commit 00d04c787d
2 changed files with 42 additions and 15 deletions

25
dist/queue.js vendored
View File

@@ -1,14 +1,22 @@
"use strict"; "use strict";
/* TODO
- queue timeout
- after 5 mins of inactivity, the queue will close
*/
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.queueInfo = exports.cancelQueue = 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 discord_js_1 = require("discord.js");
const util_1 = require("./util"); const util_1 = require("./util");
//maps ChannelID to QueueInfo //maps ChannelID to QueueInfo
const QUEUE = new Map(); 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 * get the GuildMember of an interaction
* @param interaction * @param interaction
@@ -74,7 +82,7 @@ exports.queueContains = queueContains;
* @throws string message if it cannot be created * @throws string message if it cannot be created
*/ */
async function createQueue(interaction) { 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)) 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 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, { QUEUE.set(channelId, {
@@ -82,7 +90,8 @@ async function createQueue(interaction) {
member member
], ],
initiator: 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`); 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)) if (queueContains(interaction))
throw 'You are already in the active queue'; throw 'You are already in the active queue';
info.players.push(member); info.players.push(member);
clearTimeout(info.timeout);
info.timeout = setQueueTimeout(interaction);
QUEUE.set(interaction.channelId, info); QUEUE.set(interaction.channelId, info);
await interaction.reply('Joined the queue'); await interaction.reply('Joined the queue');
} }
@@ -111,6 +122,8 @@ async function leaveQueue(interaction) {
if (!queueContains(interaction)) if (!queueContains(interaction))
throw 'You aren\'t in the active queue'; throw 'You aren\'t in the active queue';
info.players.splice(info.players.indexOf(member), 1); info.players.splice(info.players.indexOf(member), 1);
clearTimeout(info.timeout);
info.timeout = setQueueTimeout(interaction);
QUEUE.set(interaction.channelId, info); QUEUE.set(interaction.channelId, info);
await interaction.reply('Left the queue'); await interaction.reply('Left the queue');
} }

View File

@@ -1,20 +1,29 @@
/* TODO
- queue timeout
- after 5 mins of inactivity, the queue will close
*/
import { CommandInteraction, GuildMember, TextChannel } from "discord.js"; import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
import { shuffle } from "./util"; import { shuffle } from "./util";
type queueInfo = { type queueInfo = {
players: GuildMember[], players: GuildMember[],
initiator: GuildMember, initiator: GuildMember,
teamsize: number teamsize: number,
timeout: NodeJS.Timeout
} }
//maps ChannelID to QueueInfo //maps ChannelID to QueueInfo
const QUEUE = new Map<string, 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 * get the GuildMember of an interaction
* @param interaction * @param interaction
@@ -95,8 +104,8 @@ export function queueContains(interaction: CommandInteraction): boolean {
*/ */
export async function createQueue(interaction: CommandInteraction) { export async function createQueue(interaction: CommandInteraction) {
let {channelId} = interaction, let member = getMember(interaction),
member = getMember(interaction), {channelId} = interaction,
teamsize = interaction.options.getInteger('teamsize', true); teamsize = interaction.options.getInteger('teamsize', true);
if (QUEUE.has(channelId)) if (QUEUE.has(channelId))
@@ -107,7 +116,8 @@ export async function createQueue(interaction: CommandInteraction) {
member member
], ],
initiator: 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`); 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'; throw 'You are already in the active queue';
info.players.push(member); info.players.push(member);
clearTimeout(info.timeout);
info.timeout = setQueueTimeout(interaction)
QUEUE.set(interaction.channelId, info); QUEUE.set(interaction.channelId, info);
@@ -147,6 +159,8 @@ export async function leaveQueue(interaction: CommandInteraction) {
throw 'You aren\'t in the active queue'; throw 'You aren\'t in the active queue';
info.players.splice(info.players.indexOf(member), 1); info.players.splice(info.players.indexOf(member), 1);
clearTimeout(info.timeout);
info.timeout = setQueueTimeout(interaction)
QUEUE.set(interaction.channelId, info); QUEUE.set(interaction.channelId, info);