cleaned up queue file

This commit is contained in:
2022-01-31 13:18:58 -06:00
parent e719eaef95
commit ae7093ae1d
4 changed files with 202 additions and 199 deletions

View File

@@ -24,18 +24,31 @@ CLIENT.on('ready', client => {
CLIENT.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'queue')
await createQueue(interaction);
else if (interaction.commandName === 'join')
await joinQueue(interaction);
else if (interaction.commandName === 'leave')
await leaveQueue(interaction);
else if (interaction.commandName === 'ready')
await readyQueue(interaction);
else if (interaction.commandName === 'queueinfo')
await queueInfo(interaction);
else if (interaction.commandName === 'elo')
await getPlayerInteraction(interaction);
try {
if (interaction.commandName === 'queue')
await createQueue(interaction);
else if (interaction.commandName === 'join')
await joinQueue(interaction);
else if (interaction.commandName === 'leave')
await leaveQueue(interaction);
else if (interaction.commandName === 'ready')
await readyQueue(interaction);
else if (interaction.commandName === 'queueinfo')
await queueInfo(interaction);
else if (interaction.commandName === 'elo')
await getPlayerInteraction(interaction);
} catch (e) {
if (typeof e === 'string') {
if (interaction.deferred || interaction.replied)
interaction.editReply(e);
else
interaction.reply(e);
} else console.error(e);
}
});
CLIENT.login(TOKEN);

View File

@@ -1,3 +1,4 @@
import { info } from "console";
import { CommandInteraction, GuildMember } from "discord.js";
import { shuffle } from "./util";
@@ -10,25 +11,49 @@ type queueInfo = {
//maps ChannelID to QueueInfo
const QUEUE = new Map<string, queueInfo>();
export async function createQueue(interaction: CommandInteraction) {
console.log('COMMAND createQueue')
/**
* 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)) {
await interaction.reply('Unable to retrieve guild member information, please try again');
return;
}
if (!(member instanceof GuildMember))
throw 'Unable to retrieve guild member information, please try again';
let channelId = interaction.channelId;
return member;
}
if (QUEUE.has(channelId)) {
await interaction.reply('There is already an active queue in this channel, type `/join` to join'); //and you are already in it
return;
}
/**
* get the queueInfo of an interaction
* @param interaction
* @throws string message if it does not exist
* @returns queue info
*/
function getInfo(interaction: CommandInteraction): queueInfo {
let info = QUEUE.get(interaction.channelId);
let teamsize = interaction.options.getInteger('teamsize', true);
if (!info)
throw 'There is not an active queue in this channel, type `/queue` to create one';
return info;
}
/**
* creates a queue from an interaction
* @param interaction
* @throws string message if it cannot be created
*/
export async function createQueue(interaction: CommandInteraction) {
let {channelId} = interaction,
member = getMember(interaction),
teamsize = interaction.options.getInteger('teamsize', true);
if (QUEUE.has(channelId))
throw 'There is already an active queue in this channel, type `/join` to join'; //and you are already in it
QUEUE.set(channelId, {
players: [
@@ -42,117 +67,78 @@ export async function createQueue(interaction: CommandInteraction) {
}
/**
* joins a queue from an interaction
* @param interaction
* @throws string message if it cannot be joined
*/
export async function joinQueue(interaction: CommandInteraction) {
console.log('COMMAND joinQueue')
let member = getMember(interaction),
info = getInfo(interaction);
let member = interaction.member;
if (!(member instanceof GuildMember)) {
await interaction.reply('Unable to retrieve guild member information, please try again');
return;
}
let channelId = interaction.channelId;
let info = QUEUE.get(channelId);
if (!info) {
await interaction.reply('There is not an active queue in this channel, type `/queue` to create one');
return;
}
if (info.players.map(m=>m.id).includes(member.id)) {
await interaction.reply('You are already in the active queue');
return;
}
console.log(JSON.stringify(member.id))
console.log(JSON.stringify(info.players.map(m=>m.id)))
if (info.players.map(m=>m.id).includes(member.id))
throw 'You are already in the active queue';
info.players.push(member);
QUEUE.set(channelId, info);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Joined the queue');
}
/**
* leaves a queue from an interaction
* @param interaction
* @throws string message if it cannot be left
*/
export async function leaveQueue(interaction: CommandInteraction) {
console.log('COMMAND leaveQueue')
let member = getMember(interaction),
info = getInfo(interaction);
let member = interaction.member;
if (!(member instanceof GuildMember)) {
await interaction.reply('Unable to retrieve guild member information, please try again');
return;
}
let channelId = interaction.channelId;
let info = QUEUE.get(channelId);
if (!info) {
await interaction.reply('There is not an active queue in this channel, type `/queue` to create one');
return;
}
if (!info.players.map(m=>m.id).includes(member.id)) {
await interaction.reply('You aren\'t in the active queue');
return;
}
if (!info.players.map(m=>m.id).includes(member.id))
throw 'You aren\'t in the active queue';
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(channelId, info);
QUEUE.set(interaction.channelId, info);
await interaction.reply('Left the queue');
}
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be readied
*/
export async function readyQueue(interaction: CommandInteraction) {
console.log('COMMAND readyQueue');
let member = getMember(interaction),
info = getInfo(interaction),
{initiator} = info;
let member = interaction.member;
if (member.id !== initiator.id)
throw 'Only the queue initiator can ready the queue';
if (!(member instanceof GuildMember)) {
await interaction.reply('Unable to retrieve guild member information, please try again');
return;
}
//reset queue
QUEUE.delete(interaction.channelId);
let channelId = interaction.channelId;
let info = QUEUE.get(channelId);
if (!info) {
await interaction.reply('There is no active queue in this channel, type `/queue` to create one'); //and you are already in it
return;
}
let {initiator} = info;
if (member.id !== initiator.id) {
await interaction.reply('Only the queue initiator can ready the queue');
return;
}
QUEUE.delete(channelId);
if (info.players.filter(m => m.id !== initiator.id).length === 0) {
await interaction.reply('Nobody signed up for the queue, the queue has been reset');
return;
}
if (info.players.filter(m => m.id !== initiator.id).length === 0)
throw '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));
//convert teams to strings
let teamsStr: string[] = [];
teams.forEach((team, i) => {
let str = [`Team ${i+1}`];
@@ -165,17 +151,14 @@ export async function readyQueue(interaction: CommandInteraction) {
}
/**
* sends the queue information from an interaction
* @param interaction
* @throws string message if it cannot be read
*/
export async function queueInfo(interaction: CommandInteraction) {
console.log('COMMAND queueInfo')
let channelId = interaction.channelId,
info = QUEUE.get(channelId);
if (!info) {
await interaction.reply('There is not an active queue in this channel, type `/queue` to create one');
return;
}
let info = getInfo(interaction);
await interaction.reply('```'+`
players: ${info.players.map(p => p.user.tag).join('\n ')}