remade queue logic

This commit is contained in:
2022-02-01 13:47:52 -06:00
parent dc0cc6685d
commit cf453957e9
7 changed files with 278 additions and 249 deletions

View File

@@ -1,236 +1,262 @@
import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js";
import { emsg, getChannel, getMember, queueInfo, shuffle } from "./util";
/* TODO
//maps ChannelID to QueueInfo
const QUEUE = new Map<string, queueInfo>();
join message should contain your current position in the queue, editing it to keep it current
*/
/**
* get the queueInfo of an interaction
* @param interaction
* @throws errorMessage class if it does not exist
* @returns queue info
*/
export function getInfo(interaction: CommandInteraction): queueInfo {
let info = QUEUE.get(interaction.channelId);
import { Client, CommandInteraction, GuildMember, MessageEmbed, TextChannel } from "discord.js";
import * as fs from 'fs';
import { emsg, getChannel, getMember, memberIsModThrow, queueInfo, queueInfoBase } from "./util";
//load queues from file
if (!fs.existsSync('./queues'))
fs.writeFileSync('./queues', '{}');
const _QUEUE = fs.readFileSync('./queues').toString(),
QUEUE = new Map<string, queueInfo>();
try {
let queueJson = JSON.parse(_QUEUE);
for (let channelId of queueJson) {
let {teamsize} = queueJson[channelId] as queueInfoBase;
if (teamsize)
QUEUE.set(channelId, { teamsize, players: [] })
}
} catch(e) {}
function SaveQueue() {
let queueJson = Object.fromEntries(QUEUE),
queueFileJson: {[keys: string]: queueInfoBase} = {};
for (let channelId in QUEUE.keys())
queueFileJson[channelId] = { teamsize: queueJson[channelId].teamsize };
fs.writeFileSync('./queues.json', JSON.stringify(queueFileJson, null, 2));
}
async function checkQueue(channel: TextChannel) {
let info = QUEUE.get(channel.id);
if (!info)
throw emsg('There is not an active queue in this channel, type `/queue` to create one');
return info;
}
/**
* compiles all the get functions above
* @param interaction
* @throws if another get function throws
* @returns object containing each
*/
export const getAll = (interaction: CommandInteraction) => ({
member: getMember(interaction),
channel: getChannel(interaction),
info: getInfo(interaction)
});
/**
* checks if the interaction data is already in the queue
* @param interaction
* @returns boolean
*/
export function queueContains(interaction: CommandInteraction): boolean {
let {member, info} = getAll(interaction);
if (info.players.map(m=>m.id).includes(member.id))
return true;
return false;
}
/**
* creates the timeout for the queue
* @param interaction
* @returns time timeout identifier
*/
function setQueueTimeout(interaction: CommandInteraction) {
let channel = getChannel(interaction);
return setTimeout(() => {
clearQueue(interaction);
channel.send('Queue has been reset due to inactivity');
}, 5*60*1000) //5 minutes
}
/**
* updates the rich embed for the current queue
* @param interaction
*/
async function sendQueueEmbed(interaction: CommandInteraction, closed: boolean = false) {
let info = getInfo(interaction),
origInteraction = info.initiator.interaction;
return;
let embed = new MessageEmbed()
.setTitle('Queue')
.setAuthor({
name: info.initiator.member.displayName,
iconURL: info.initiator.member.displayAvatarURL({dynamic: true})
})
.addField('Team Size', info.teamsize.toString(), true)
.addField('Players Joined', info.players.length.toString(), true)
.setFooter({text: closed ? 'queue is finished' : 'type /join'});
if (info.players.length > info.teamsize) {
if (origInteraction.deferred || origInteraction.replied)
await origInteraction.editReply({embeds: [embed]});
else
await origInteraction.reply({embeds: [embed]});
let team = info.players.splice(0, info.teamsize).map(m => m.toString());
let embed = new MessageEmbed()
.setTitle('Team')
.setDescription(team.join('\n'));
await channel.send({embeds: [embed]});
}
}
namespace Queue {
/**
* sends the list of teams
* @param interaction
*/
async function sendTeamsEmbed(interaction: CommandInteraction, teams: GuildMember[][]) {
let embed = new MessageEmbed()
.setTitle('Teams');
export function create(channelId: string, teamsize: number) {
if (!QUEUE.has(channelId)) {
QUEUE.set(channelId, {teamsize, players: []});
SaveQueue();
}
}
teams.forEach((team, i) => {
team.map(m => m.user.tag);
embed.addField(`Team ${i+1}`, team.join('\n'))
});
export function remove(channelId: string) {
if (QUEUE.has(channelId)) {
QUEUE.delete(channelId);
SaveQueue();
}
}
interaction.reply({embeds: [embed]});
}
export function addPlayer(channelId: string, member: GuildMember) {
if (QUEUE.has(channelId)) {
QUEUE.delete(channelId);
}
}
/**
* sends the list of teams
* @param interaction
*/
async function clearQueue(interaction: CommandInteraction) {
let info = getInfo(interaction);
sendQueueEmbed(interaction, true);
clearTimeout(info.timeout);
QUEUE.delete(interaction.channelId);
}
/**
* creates a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be created
*/
export async function createQueue(interaction: CommandInteraction) {
let member = getMember(interaction),
{channelId} = interaction,
teamsize = interaction.options.getInteger('teamsize', true);
if (QUEUE.has(channelId))
throw emsg('There is already an active queue in this channel, ' + (queueContains(interaction) ? 'and you are already in it' : 'type `/join` to join'));
QUEUE.set(channelId, {
players: [
member
],
initiator: {
interaction,
member
},
teamsize: teamsize,
timeout: setQueueTimeout(interaction)
});
sendQueueEmbed(interaction);
export function removePlayer(channelId: string, member: GuildMember) {
if (QUEUE.has(channelId)) {
QUEUE.delete(channelId);
}
}
}
/**
* joins a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be joined
*/
export async function joinQueue(interaction: CommandInteraction) {
SaveQueue();
let {member, info} = getAll(interaction);
export async function discordInit(client: Client) {
if (queueContains(interaction))
throw emsg('You are already in the active queue');
for (let channelId in QUEUE.keys()) {
info.players.push(member);
clearTimeout(info.timeout);
info.timeout = setQueueTimeout(interaction)
let info = QUEUE.get(channelId),
channel = await client.channels.fetch(channelId);
QUEUE.set(interaction.channelId, info);
if (!info) { //no idea what could cause this but TS complains
Queue.remove(channelId);
continue;
}
sendQueueEmbed(interaction);
await interaction.reply('Joined the queue');
}
/**
* leaves a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be left
*/
export async function leaveQueue(interaction: CommandInteraction) {
let {member, info} = getAll(interaction);
if (!queueContains(interaction))
throw emsg('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);
sendQueueEmbed(interaction);
await interaction.reply('Left the queue');
}
/**
* readys a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be readied
*/
export async function readyQueue(interaction: CommandInteraction) {
let {member, info} = getAll(interaction),
{initiator} = info;
if (member.id !== initiator.member.id)
throw emsg('Only the queue initiator can ready the queue');
clearQueue(interaction);
if (info.players.filter(m => m.id !== initiator.member.id).length === 0)
throw emsg('Nobody signed up for the queue, the queue has been reset');
//team data
let playerlist: GuildMember[] = shuffle(info.players),
teams: GuildMember[][] = [];
//fill team data
for (let i = 0; i < playerlist.length; i+= info.teamsize)
teams.push(playerlist.slice(i, i+info.teamsize));
sendTeamsEmbed(interaction, teams);
}
/**
* readys a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be reset
*/
export async function cancelQueue(interaction: CommandInteraction) {
let {info, member, channel} = getAll(interaction);
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
throw emsg('You do not have permission to run this command');
clearQueue(interaction);
await interaction.reply('Queue has been reset');
if (!channel || !(channel instanceof TextChannel)) {
console.error(`Unable to find channel ${channelId} for teams of ${info?.teamsize}`);
Queue.remove(channelId);
continue;
}
channel.send('The bot has just restarted and anybody in the queues have been reset')
}
}
export namespace QueueCommands {
/**
* get the queueInfo of an interaction
* @param interaction
* @throws errorMessage class if it does not exist
* @returns queue info
*/
function getInfo(interaction: CommandInteraction): queueInfo {
let info = QUEUE.get(interaction.channelId);
if (!info)
throw emsg('There is not an active queue in this channel, type `/queue` to create one');
return info;
}
/**
* compiles all the get functions above
* @param interaction
* @throws if another get function throws
* @returns object containing each
*/
const getAll = (interaction: CommandInteraction) => ({
member: getMember(interaction),
channel: getChannel(interaction),
info: getInfo(interaction)
});
/**
* checks if the interaction data is already in the queue
* @param interaction
* @returns boolean
*/
export function queueContains(interaction: CommandInteraction): boolean {
let {member, info} = getAll(interaction);
if (info.players.map(m=>m.id).includes(member.id))
return true;
return false;
}
/**
* creates a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be left
*/
export function queueCreate(interaction: CommandInteraction) {
memberIsModThrow(interaction);
let {channelId} = interaction,
teamsize = interaction.options.getInteger('teamsize', true);
if (QUEUE.has(channelId))
throw emsg(`There is already an active queue in this channel for teams of ${QUEUE.get(channelId)?.teamsize}`);
Queue.create(channelId, teamsize);
interaction.reply(`A queue for teams of ${teamsize} has been started`)
}
/**
* creates a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be left
*/
export async function queue(interaction: CommandInteraction) {
let teamsize = interaction.options.getInteger('teamsize');
if (teamsize) {
queueCreate(interaction);
return;
}
let info = getInfo(interaction);
let embed = new MessageEmbed()
.setTitle('Active Queue')
.addField('Team Size', info.teamsize.toString(), true)
.addField('Players Joined', info.players.length.toString(), true)
.setFooter({text: 'type /join'});
await interaction.reply({embeds: [embed], ephemeral: true});
}
/**
* stops a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be joined
*/
export async function stop(interaction: CommandInteraction) {
memberIsModThrow(interaction);
QUEUE.delete(interaction.channelId);
await interaction.reply('Queue has been reset');
}
/**
* joins a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be readied
*/
export async function join(interaction: CommandInteraction) {
let {member, info, channel} = getAll(interaction);
if (queueContains(interaction))
throw emsg('You are already in the queue');
info.players.push(member);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Joined the queue');
checkQueue(channel);
}
/**
* leaves a queue from an interaction
* @param interaction
* @throws errorMessage class if it cannot be reset
*/
export async function leave(interaction: CommandInteraction) {
let {member, info} = getAll(interaction);
if (!queueContains(interaction))
throw emsg('You aren\'t in the queue');
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Left the queue');
}
}