added inactivity timeout
This commit is contained in:
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