improved messages and general cleanup

This commit is contained in:
2022-01-31 19:33:39 -06:00
parent 00d04c787d
commit fe866050d5
10 changed files with 383 additions and 228 deletions

View File

@@ -1,70 +1,20 @@
import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
import { shuffle } from "./util";
type queueInfo = {
players: GuildMember[],
initiator: GuildMember,
teamsize: number,
timeout: NodeJS.Timeout
}
import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js";
import { emsg, getChannel, getMember, queueInfo, shuffle } from "./util";
//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
* @throws string message if it cannot be read
* @returns member
*/
function getMember(interaction: CommandInteraction): GuildMember {
let member = interaction.member;
if (!(member instanceof GuildMember))
throw 'Unable to retrieve guild member information, please try again';
return member;
}
/**
* get the TextChannel of an interaction
* @param interaction
* @throws string message if it cannot be read
* @returns member
*/
function getChannel(interaction: CommandInteraction): TextChannel {
let channel = interaction.channel;
if (!(channel instanceof TextChannel))
throw 'Unable to retrieve text channel information, please try again';
return channel;
}
/**
* get the queueInfo of an interaction
* @param interaction
* @throws string message if it does not exist
* @throws errorMessage class if it does not exist
* @returns queue info
*/
function getInfo(interaction: CommandInteraction): queueInfo {
export function getInfo(interaction: CommandInteraction): queueInfo {
let info = QUEUE.get(interaction.channelId);
if (!info)
throw 'There is not an active queue in this channel, type `/queue` to create one';
throw emsg('There is not an active queue in this channel, type `/queue` to create one');
return info;
}
@@ -72,10 +22,10 @@ function getInfo(interaction: CommandInteraction): queueInfo {
/**
* compiles all the get functions above
* @param interaction
* @throws string message if it does not exist
* @throws if another get function throws
* @returns object containing each
*/
const getAll = (interaction: CommandInteraction) => ({
export const getAll = (interaction: CommandInteraction) => ({
member: getMember(interaction),
channel: getChannel(interaction),
info: getInfo(interaction)
@@ -97,10 +47,74 @@ export function queueContains(interaction: CommandInteraction): boolean {
}
/**
* 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;
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 (origInteraction.deferred || origInteraction.replied)
await origInteraction.editReply({embeds: [embed]});
else
await origInteraction.reply({embeds: [embed]});
}
/**
* sends the list of teams
* @param interaction
*/
async function sendTeamsEmbed(interaction: CommandInteraction, teams: GuildMember[][]) {
let embed = new MessageEmbed()
.setTitle('Teams');
teams.forEach((team, i) => {
team.map(m => m.user.tag);
embed.addField(`Title ${i+1}`, team.join('\n'))
});
interaction.reply({embeds: [embed]});
}
/**
* 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 string message if it cannot be created
* @throws errorMessage class if it cannot be created
*/
export async function createQueue(interaction: CommandInteraction) {
@@ -109,32 +123,35 @@ export async function createQueue(interaction: CommandInteraction) {
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
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: member,
initiator: {
interaction,
member
},
teamsize: teamsize,
timeout: setQueueTimeout(interaction)
});
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
sendQueueEmbed(interaction);
}
/**
* joins a queue from an interaction
* @param interaction
* @throws string message if it cannot be joined
* @throws errorMessage class if it cannot be joined
*/
export async function joinQueue(interaction: CommandInteraction) {
let {member, info} = getAll(interaction);
if (queueContains(interaction))
throw 'You are already in the active queue';
throw emsg('You are already in the active queue');
info.players.push(member);
clearTimeout(info.timeout);
@@ -142,6 +159,7 @@ export async function joinQueue(interaction: CommandInteraction) {
QUEUE.set(interaction.channelId, info);
sendQueueEmbed(interaction);
await interaction.reply('Joined the queue');
}
@@ -149,14 +167,14 @@ export async function joinQueue(interaction: CommandInteraction) {
/**
* leaves a queue from an interaction
* @param interaction
* @throws string message if it cannot be left
* @throws errorMessage class if it cannot be left
*/
export async function leaveQueue(interaction: CommandInteraction) {
let {member, info} = getAll(interaction);
if (!queueContains(interaction))
throw 'You aren\'t in the active queue';
throw emsg('You aren\'t in the active queue');
info.players.splice(info.players.indexOf(member), 1);
clearTimeout(info.timeout);
@@ -164,6 +182,7 @@ export async function leaveQueue(interaction: CommandInteraction) {
QUEUE.set(interaction.channelId, info);
sendQueueEmbed(interaction);
await interaction.reply('Left the queue');
}
@@ -171,21 +190,20 @@ export async function leaveQueue(interaction: CommandInteraction) {
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be readied
* @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.id)
throw 'Only the queue initiator can ready the queue';
if (member.id !== initiator.member.id)
throw emsg('Only the queue initiator can ready the queue');
//reset queue
QUEUE.delete(interaction.channelId);
clearQueue(interaction);
if (info.players.filter(m => m.id !== initiator.id).length === 0)
throw 'Nobody signed up for the queue, the queue has been reset';
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),
@@ -195,52 +213,24 @@ export async function readyQueue(interaction: CommandInteraction) {
for (let i = 0; i < playerlist.length; i+= info.teamsize)
teams.push(playerlist.slice(i, i+info.teamsize));
//convert teams to strings
let teamsStr: string[] = [];
teams.forEach((team, i) => {
let str = [`Team ${i+1}`];
team.forEach(m => str.push(` ${m.user.tag}`));
teamsStr.push(str.join('\n'));
});
await interaction.reply('```\n'+teamsStr.join('\n\n')+'\n```');
sendTeamsEmbed(interaction, teams);
}
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be reset
* @throws errorMessage class if it cannot be reset
*/
export async function cancelQueue(interaction: CommandInteraction) {
let {member, channel} = getAll(interaction);
let {info, member, channel} = getAll(interaction);
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
throw 'You do not have permission to run this command';
throw emsg('You do not have permission to run this command');
//reset queue
QUEUE.delete(interaction.channelId);
clearQueue(interaction);
await interaction.reply('Queue has been reset');
}
/**
* sends the queue information from an interaction
* @param interaction
* @throws string message if it cannot be read
*/
export async function queueInfo(interaction: CommandInteraction) {
let info = getInfo(interaction);
await interaction.reply('```'+`
players: ${info.players.map(p => p.user.tag).join('\n ')}
initiator: ${info.initiator.user.tag}
teamsize: ${info.teamsize}
`+'```');
}