Compare commits
2 Commits
50c297bfea
...
00d04c787d
| Author | SHA1 | Date | |
|---|---|---|---|
| 00d04c787d | |||
| f0283145dc |
7
dist/discord.js
vendored
7
dist/discord.js
vendored
@@ -13,7 +13,8 @@ const commands = [
|
|||||||
type: 4,
|
type: 4,
|
||||||
name: 'teamsize',
|
name: 'teamsize',
|
||||||
description: 'size of each team',
|
description: 'size of each team',
|
||||||
required: true
|
required: true,
|
||||||
|
min_value: 1
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -30,8 +31,8 @@ const commands = [
|
|||||||
description: 'ready the queue and display team info'
|
description: 'ready the queue and display team info'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'queueinfo',
|
name: 'cancel',
|
||||||
description: 'get info of the current queue'
|
description: 'cancels the current queue (must have the Manage Messages permission)'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'elo',
|
name: 'elo',
|
||||||
|
|||||||
2
dist/index.js
vendored
2
dist/index.js
vendored
@@ -51,6 +51,8 @@ CLIENT.on('interactionCreate', async (interaction) => {
|
|||||||
await (0, queue_1.leaveQueue)(interaction);
|
await (0, queue_1.leaveQueue)(interaction);
|
||||||
else if (interaction.commandName === 'ready')
|
else if (interaction.commandName === 'ready')
|
||||||
await (0, queue_1.readyQueue)(interaction);
|
await (0, queue_1.readyQueue)(interaction);
|
||||||
|
else if (interaction.commandName === 'cancel')
|
||||||
|
await (0, queue_1.cancelQueue)(interaction);
|
||||||
else if (interaction.commandName === 'queueinfo')
|
else if (interaction.commandName === 'queueinfo')
|
||||||
await (0, queue_1.queueInfo)(interaction);
|
await (0, queue_1.queueInfo)(interaction);
|
||||||
else if (interaction.commandName === 'elo')
|
else if (interaction.commandName === 'elo')
|
||||||
|
|||||||
68
dist/queue.js
vendored
68
dist/queue.js
vendored
@@ -1,10 +1,22 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.queueInfo = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = void 0;
|
exports.queueInfo = exports.cancelQueue = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = void 0;
|
||||||
const discord_js_1 = require("discord.js");
|
const discord_js_1 = require("discord.js");
|
||||||
const util_1 = require("./util");
|
const util_1 = require("./util");
|
||||||
//maps ChannelID to QueueInfo
|
//maps ChannelID to QueueInfo
|
||||||
const QUEUE = new Map();
|
const QUEUE = new Map();
|
||||||
|
/**
|
||||||
|
* creates the timeout for the queue
|
||||||
|
* @param interaction
|
||||||
|
* @returns time timeout identifier
|
||||||
|
*/
|
||||||
|
function setQueueTimeout(interaction) {
|
||||||
|
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
|
* get the GuildMember of an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
@@ -17,6 +29,18 @@ function getMember(interaction) {
|
|||||||
throw 'Unable to retrieve guild member information, please try again';
|
throw 'Unable to retrieve guild member information, please try again';
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* get the TextChannel of an interaction
|
||||||
|
* @param interaction
|
||||||
|
* @throws string message if it cannot be read
|
||||||
|
* @returns member
|
||||||
|
*/
|
||||||
|
function getChannel(interaction) {
|
||||||
|
let channel = interaction.channel;
|
||||||
|
if (!(channel instanceof discord_js_1.TextChannel))
|
||||||
|
throw 'Unable to retrieve text channel information, please try again';
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* get the queueInfo of an interaction
|
* get the queueInfo of an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
@@ -29,13 +53,24 @@ function getInfo(interaction) {
|
|||||||
throw 'There is not an active queue in this channel, type `/queue` to create one';
|
throw 'There is not an active queue in this channel, type `/queue` to create one';
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* compiles all the get functions above
|
||||||
|
* @param interaction
|
||||||
|
* @throws string message if it does not exist
|
||||||
|
* @returns object containing each
|
||||||
|
*/
|
||||||
|
const getAll = (interaction) => ({
|
||||||
|
member: getMember(interaction),
|
||||||
|
channel: getChannel(interaction),
|
||||||
|
info: getInfo(interaction)
|
||||||
|
});
|
||||||
/**
|
/**
|
||||||
* checks if the interaction data is already in the queue
|
* checks if the interaction data is already in the queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @returns boolean
|
* @returns boolean
|
||||||
*/
|
*/
|
||||||
function queueContains(interaction) {
|
function queueContains(interaction) {
|
||||||
let member = getMember(interaction), info = getInfo(interaction);
|
let { member, info } = getAll(interaction);
|
||||||
if (info.players.map(m => m.id).includes(member.id))
|
if (info.players.map(m => m.id).includes(member.id))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
@@ -47,7 +82,7 @@ exports.queueContains = queueContains;
|
|||||||
* @throws string message if it cannot be created
|
* @throws string message if it cannot be created
|
||||||
*/
|
*/
|
||||||
async function createQueue(interaction) {
|
async function createQueue(interaction) {
|
||||||
let { channelId } = interaction, member = getMember(interaction), teamsize = interaction.options.getInteger('teamsize', true);
|
let member = getMember(interaction), { channelId } = interaction, teamsize = interaction.options.getInteger('teamsize', true);
|
||||||
if (QUEUE.has(channelId))
|
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 '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
|
||||||
QUEUE.set(channelId, {
|
QUEUE.set(channelId, {
|
||||||
@@ -55,7 +90,8 @@ async function createQueue(interaction) {
|
|||||||
member
|
member
|
||||||
],
|
],
|
||||||
initiator: 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`);
|
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
|
||||||
}
|
}
|
||||||
@@ -66,10 +102,12 @@ exports.createQueue = createQueue;
|
|||||||
* @throws string message if it cannot be joined
|
* @throws string message if it cannot be joined
|
||||||
*/
|
*/
|
||||||
async function joinQueue(interaction) {
|
async function joinQueue(interaction) {
|
||||||
let member = getMember(interaction), info = getInfo(interaction);
|
let { member, info } = getAll(interaction);
|
||||||
if (queueContains(interaction))
|
if (queueContains(interaction))
|
||||||
throw 'You are already in the active queue';
|
throw 'You are already in the active queue';
|
||||||
info.players.push(member);
|
info.players.push(member);
|
||||||
|
clearTimeout(info.timeout);
|
||||||
|
info.timeout = setQueueTimeout(interaction);
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
await interaction.reply('Joined the queue');
|
await interaction.reply('Joined the queue');
|
||||||
}
|
}
|
||||||
@@ -80,10 +118,12 @@ exports.joinQueue = joinQueue;
|
|||||||
* @throws string message if it cannot be left
|
* @throws string message if it cannot be left
|
||||||
*/
|
*/
|
||||||
async function leaveQueue(interaction) {
|
async function leaveQueue(interaction) {
|
||||||
let member = getMember(interaction), info = getInfo(interaction);
|
let { member, info } = getAll(interaction);
|
||||||
if (!queueContains(interaction))
|
if (!queueContains(interaction))
|
||||||
throw 'You aren\'t in the active queue';
|
throw 'You aren\'t in the active queue';
|
||||||
info.players.splice(info.players.indexOf(member), 1);
|
info.players.splice(info.players.indexOf(member), 1);
|
||||||
|
clearTimeout(info.timeout);
|
||||||
|
info.timeout = setQueueTimeout(interaction);
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
await interaction.reply('Left the queue');
|
await interaction.reply('Left the queue');
|
||||||
}
|
}
|
||||||
@@ -94,7 +134,7 @@ exports.leaveQueue = leaveQueue;
|
|||||||
* @throws string message if it cannot be readied
|
* @throws string message if it cannot be readied
|
||||||
*/
|
*/
|
||||||
async function readyQueue(interaction) {
|
async function readyQueue(interaction) {
|
||||||
let member = getMember(interaction), info = getInfo(interaction), { initiator } = info;
|
let { member, info } = getAll(interaction), { initiator } = info;
|
||||||
if (member.id !== initiator.id)
|
if (member.id !== initiator.id)
|
||||||
throw 'Only the queue initiator can ready the queue';
|
throw 'Only the queue initiator can ready the queue';
|
||||||
//reset queue
|
//reset queue
|
||||||
@@ -116,6 +156,20 @@ async function readyQueue(interaction) {
|
|||||||
await interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```');
|
await interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```');
|
||||||
}
|
}
|
||||||
exports.readyQueue = readyQueue;
|
exports.readyQueue = readyQueue;
|
||||||
|
/**
|
||||||
|
* readys a queue from an interaction
|
||||||
|
* @param interaction
|
||||||
|
* @throws string message if it cannot be reset
|
||||||
|
*/
|
||||||
|
async function cancelQueue(interaction) {
|
||||||
|
let { member, channel } = getAll(interaction);
|
||||||
|
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
|
||||||
|
throw 'You do not have permission to run this command';
|
||||||
|
//reset queue
|
||||||
|
QUEUE.delete(interaction.channelId);
|
||||||
|
await interaction.reply('Queue has been reset');
|
||||||
|
}
|
||||||
|
exports.cancelQueue = cancelQueue;
|
||||||
/**
|
/**
|
||||||
* sends the queue information from an interaction
|
* sends the queue information from an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ const commands = [
|
|||||||
type: 4, //INTEGER
|
type: 4, //INTEGER
|
||||||
name: 'teamsize',
|
name: 'teamsize',
|
||||||
description: 'size of each team',
|
description: 'size of each team',
|
||||||
required: true
|
required: true,
|
||||||
|
min_value: 1
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -28,8 +29,8 @@ const commands = [
|
|||||||
description: 'ready the queue and display team info'
|
description: 'ready the queue and display team info'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'queueinfo',
|
name: 'cancel',
|
||||||
description: 'get info of the current queue'
|
description: 'cancels the current queue (must have the Manage Messages permission)'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'elo',
|
name: 'elo',
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Client, Intents } from 'discord.js';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { getPlayerInteraction } from './api';
|
import { getPlayerInteraction } from './api';
|
||||||
import { registerCommands } from './discord';
|
import { registerCommands } from './discord';
|
||||||
import { createQueue, joinQueue, leaveQueue, queueInfo, readyQueue } from './queue';
|
import { cancelQueue, createQueue, joinQueue, leaveQueue, queueInfo, readyQueue } from './queue';
|
||||||
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
|
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
|
||||||
|
|
||||||
//init logs with a timestamp
|
//init logs with a timestamp
|
||||||
@@ -37,6 +37,8 @@ CLIENT.on('interactionCreate', async interaction => {
|
|||||||
await leaveQueue(interaction);
|
await leaveQueue(interaction);
|
||||||
else if (interaction.commandName === 'ready')
|
else if (interaction.commandName === 'ready')
|
||||||
await readyQueue(interaction);
|
await readyQueue(interaction);
|
||||||
|
else if (interaction.commandName === 'cancel')
|
||||||
|
await cancelQueue(interaction);
|
||||||
else if (interaction.commandName === 'queueinfo')
|
else if (interaction.commandName === 'queueinfo')
|
||||||
await queueInfo(interaction);
|
await queueInfo(interaction);
|
||||||
else if (interaction.commandName === 'elo')
|
else if (interaction.commandName === 'elo')
|
||||||
|
|||||||
88
src/queue.ts
88
src/queue.ts
@@ -1,16 +1,29 @@
|
|||||||
import { info } from "console";
|
import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
|
||||||
import { CommandInteraction, GuildMember } from "discord.js";
|
|
||||||
import { shuffle } from "./util";
|
import { shuffle } from "./util";
|
||||||
|
|
||||||
type queueInfo = {
|
type queueInfo = {
|
||||||
players: GuildMember[],
|
players: GuildMember[],
|
||||||
initiator: GuildMember,
|
initiator: GuildMember,
|
||||||
teamsize: number
|
teamsize: number,
|
||||||
|
timeout: NodeJS.Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
//maps ChannelID to QueueInfo
|
//maps ChannelID to QueueInfo
|
||||||
const QUEUE = new Map<string, 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
|
* get the GuildMember of an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
@@ -26,6 +39,21 @@ function getMember(interaction: CommandInteraction): GuildMember {
|
|||||||
return member;
|
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
|
* get the queueInfo of an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
@@ -41,6 +69,18 @@ function getInfo(interaction: CommandInteraction): queueInfo {
|
|||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compiles all the get functions above
|
||||||
|
* @param interaction
|
||||||
|
* @throws string message if it does not exist
|
||||||
|
* @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
|
* checks if the interaction data is already in the queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
@@ -48,8 +88,7 @@ function getInfo(interaction: CommandInteraction): queueInfo {
|
|||||||
*/
|
*/
|
||||||
export function queueContains(interaction: CommandInteraction): boolean {
|
export function queueContains(interaction: CommandInteraction): boolean {
|
||||||
|
|
||||||
let member = getMember(interaction),
|
let {member, info} = getAll(interaction);
|
||||||
info = getInfo(interaction);
|
|
||||||
|
|
||||||
if (info.players.map(m=>m.id).includes(member.id))
|
if (info.players.map(m=>m.id).includes(member.id))
|
||||||
return true;
|
return true;
|
||||||
@@ -65,8 +104,8 @@ export function queueContains(interaction: CommandInteraction): boolean {
|
|||||||
*/
|
*/
|
||||||
export async function createQueue(interaction: CommandInteraction) {
|
export async function createQueue(interaction: CommandInteraction) {
|
||||||
|
|
||||||
let {channelId} = interaction,
|
let member = getMember(interaction),
|
||||||
member = getMember(interaction),
|
{channelId} = interaction,
|
||||||
teamsize = interaction.options.getInteger('teamsize', true);
|
teamsize = interaction.options.getInteger('teamsize', true);
|
||||||
|
|
||||||
if (QUEUE.has(channelId))
|
if (QUEUE.has(channelId))
|
||||||
@@ -77,7 +116,8 @@ export async function createQueue(interaction: CommandInteraction) {
|
|||||||
member
|
member
|
||||||
],
|
],
|
||||||
initiator: 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`);
|
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
|
||||||
@@ -91,13 +131,14 @@ export async function createQueue(interaction: CommandInteraction) {
|
|||||||
*/
|
*/
|
||||||
export async function joinQueue(interaction: CommandInteraction) {
|
export async function joinQueue(interaction: CommandInteraction) {
|
||||||
|
|
||||||
let member = getMember(interaction),
|
let {member, info} = getAll(interaction);
|
||||||
info = getInfo(interaction);
|
|
||||||
|
|
||||||
if (queueContains(interaction))
|
if (queueContains(interaction))
|
||||||
throw 'You are already in the active queue';
|
throw 'You are already in the active queue';
|
||||||
|
|
||||||
info.players.push(member);
|
info.players.push(member);
|
||||||
|
clearTimeout(info.timeout);
|
||||||
|
info.timeout = setQueueTimeout(interaction)
|
||||||
|
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
|
|
||||||
@@ -112,13 +153,14 @@ export async function joinQueue(interaction: CommandInteraction) {
|
|||||||
*/
|
*/
|
||||||
export async function leaveQueue(interaction: CommandInteraction) {
|
export async function leaveQueue(interaction: CommandInteraction) {
|
||||||
|
|
||||||
let member = getMember(interaction),
|
let {member, info} = getAll(interaction);
|
||||||
info = getInfo(interaction);
|
|
||||||
|
|
||||||
if (!queueContains(interaction))
|
if (!queueContains(interaction))
|
||||||
throw 'You aren\'t in the active queue';
|
throw 'You aren\'t in the active queue';
|
||||||
|
|
||||||
info.players.splice(info.players.indexOf(member), 1);
|
info.players.splice(info.players.indexOf(member), 1);
|
||||||
|
clearTimeout(info.timeout);
|
||||||
|
info.timeout = setQueueTimeout(interaction)
|
||||||
|
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
|
|
||||||
@@ -133,8 +175,7 @@ export async function leaveQueue(interaction: CommandInteraction) {
|
|||||||
*/
|
*/
|
||||||
export async function readyQueue(interaction: CommandInteraction) {
|
export async function readyQueue(interaction: CommandInteraction) {
|
||||||
|
|
||||||
let member = getMember(interaction),
|
let {member, info} = getAll(interaction),
|
||||||
info = getInfo(interaction),
|
|
||||||
{initiator} = info;
|
{initiator} = info;
|
||||||
|
|
||||||
if (member.id !== initiator.id)
|
if (member.id !== initiator.id)
|
||||||
@@ -168,6 +209,25 @@ export async function readyQueue(interaction: CommandInteraction) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readys a queue from an interaction
|
||||||
|
* @param interaction
|
||||||
|
* @throws string message if it cannot be reset
|
||||||
|
*/
|
||||||
|
export async function cancelQueue(interaction: CommandInteraction) {
|
||||||
|
|
||||||
|
let {member, channel} = getAll(interaction);
|
||||||
|
|
||||||
|
if (!member.permissionsIn(channel).has('MANAGE_MESSAGES'))
|
||||||
|
throw 'You do not have permission to run this command';
|
||||||
|
|
||||||
|
//reset queue
|
||||||
|
QUEUE.delete(interaction.channelId);
|
||||||
|
|
||||||
|
await interaction.reply('Queue has been reset');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sends the queue information from an interaction
|
* sends the queue information from an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
|
|||||||
Reference in New Issue
Block a user