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

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 { 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);