diff --git a/dist/index.js b/dist/index.js index 9cab9ec..12598e7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -48,17 +48,29 @@ CLIENT.on('ready', client => { CLIENT.on('interactionCreate', (interaction) => __awaiter(void 0, void 0, void 0, function* () { if (!interaction.isCommand()) return; - if (interaction.commandName === 'queue') - yield (0, queue_1.createQueue)(interaction); - else if (interaction.commandName === 'join') - yield (0, queue_1.joinQueue)(interaction); - else if (interaction.commandName === 'leave') - yield (0, queue_1.leaveQueue)(interaction); - else if (interaction.commandName === 'ready') - yield (0, queue_1.readyQueue)(interaction); - else if (interaction.commandName === 'queueinfo') - yield (0, queue_1.queueInfo)(interaction); - else if (interaction.commandName === 'elo') - yield (0, api_1.getPlayerInteraction)(interaction); + try { + if (interaction.commandName === 'queue') + yield (0, queue_1.createQueue)(interaction); + else if (interaction.commandName === 'join') + yield (0, queue_1.joinQueue)(interaction); + else if (interaction.commandName === 'leave') + yield (0, queue_1.leaveQueue)(interaction); + else if (interaction.commandName === 'ready') + yield (0, queue_1.readyQueue)(interaction); + else if (interaction.commandName === 'queueinfo') + yield (0, queue_1.queueInfo)(interaction); + else if (interaction.commandName === 'elo') + yield (0, api_1.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); diff --git a/dist/queue.js b/dist/queue.js index 111d706..408631b 100644 --- a/dist/queue.js +++ b/dist/queue.js @@ -14,20 +14,40 @@ const discord_js_1 = require("discord.js"); const util_1 = require("./util"); //maps ChannelID to QueueInfo const QUEUE = new Map(); +/** + * get the GuildMember of an interaction + * @param interaction + * @throws string message if it cannot be read + * @returns member + */ +function getMember(interaction) { + let member = interaction.member; + if (!(member instanceof discord_js_1.GuildMember)) + throw 'Unable to retrieve guild member information, please try again'; + return member; +} +/** + * get the queueInfo of an interaction + * @param interaction + * @throws string message if it does not exist + * @returns queue info + */ +function getInfo(interaction) { + let info = QUEUE.get(interaction.channelId); + 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 + */ function createQueue(interaction) { return __awaiter(this, void 0, void 0, function* () { - console.log('COMMAND createQueue'); - let member = interaction.member; - if (!(member instanceof discord_js_1.GuildMember)) { - yield interaction.reply('Unable to retrieve guild member information, please try again'); - return; - } - let channelId = interaction.channelId; - if (QUEUE.has(channelId)) { - yield interaction.reply('There is already an active queue in this channel, type `/join` to join'); //and you are already in it - return; - } - let teamsize = interaction.options.getInteger('teamsize', true); + 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: [ member @@ -39,83 +59,58 @@ function createQueue(interaction) { }); } exports.createQueue = createQueue; +/** + * joins a queue from an interaction + * @param interaction + * @throws string message if it cannot be joined + */ function joinQueue(interaction) { return __awaiter(this, void 0, void 0, function* () { - console.log('COMMAND joinQueue'); - let member = interaction.member; - if (!(member instanceof discord_js_1.GuildMember)) { - yield interaction.reply('Unable to retrieve guild member information, please try again'); - return; - } - let channelId = interaction.channelId; - let info = QUEUE.get(channelId); - if (!info) { - yield 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)) { - yield 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))); + let member = getMember(interaction), info = getInfo(interaction); + 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); yield interaction.reply('Joined the queue'); }); } exports.joinQueue = joinQueue; +/** + * leaves a queue from an interaction + * @param interaction + * @throws string message if it cannot be left + */ function leaveQueue(interaction) { return __awaiter(this, void 0, void 0, function* () { - console.log('COMMAND leaveQueue'); - let member = interaction.member; - if (!(member instanceof discord_js_1.GuildMember)) { - yield interaction.reply('Unable to retrieve guild member information, please try again'); - return; - } - let channelId = interaction.channelId; - let info = QUEUE.get(channelId); - if (!info) { - yield 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)) { - yield interaction.reply('You aren\'t in the active queue'); - return; - } + let member = getMember(interaction), info = getInfo(interaction); + 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); yield interaction.reply('Left the queue'); }); } exports.leaveQueue = leaveQueue; +/** + * readys a queue from an interaction + * @param interaction + * @throws string message if it cannot be readied + */ function readyQueue(interaction) { return __awaiter(this, void 0, void 0, function* () { - console.log('COMMAND readyQueue'); - let member = interaction.member; - if (!(member instanceof discord_js_1.GuildMember)) { - yield interaction.reply('Unable to retrieve guild member information, please try again'); - return; - } - let channelId = interaction.channelId; - let info = QUEUE.get(channelId); - if (!info) { - yield 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) { - yield 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) { - yield interaction.reply('Nobody signed up for the queue, the queue has been reset'); - return; - } + let member = getMember(interaction), info = getInfo(interaction), { initiator } = info; + if (member.id !== initiator.id) + throw 'Only the queue initiator can ready the queue'; + //reset queue + QUEUE.delete(interaction.channelId); + 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 = (0, util_1.shuffle)(info.players), teams = []; + //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 = []; teams.forEach((team, i) => { let str = [`Team ${i + 1}`]; @@ -126,14 +121,14 @@ function readyQueue(interaction) { }); } exports.readyQueue = readyQueue; +/** + * sends the queue information from an interaction + * @param interaction + * @throws string message if it cannot be read + */ function queueInfo(interaction) { return __awaiter(this, void 0, void 0, function* () { - console.log('COMMAND queueInfo'); - let channelId = interaction.channelId, info = QUEUE.get(channelId); - if (!info) { - yield interaction.reply('There is not an active queue in this channel, type `/queue` to create one'); - return; - } + let info = getInfo(interaction); yield interaction.reply('```' + ` players: ${info.players.map(p => p.user.tag).join('\n ')} initiator: ${info.initiator.user.tag} diff --git a/src/index.ts b/src/index.ts index 8d4ceaf..f434ff6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/src/queue.ts b/src/queue.ts index a279e6f..05d8c63 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -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(); -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 ')}