219 lines
7.8 KiB
JavaScript
219 lines
7.8 KiB
JavaScript
"use strict";
|
|
/* TODO
|
|
|
|
join message should contain your current position in the queue, editing it to keep it current
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.QueueCommands = exports.discordInit = void 0;
|
|
const discord_js_1 = require("discord.js");
|
|
const fs = __importStar(require("fs"));
|
|
const util_1 = require("./util");
|
|
//load queues from file
|
|
if (!fs.existsSync('./queues.json'))
|
|
fs.writeFileSync('./queues.json', '{}');
|
|
const _QUEUE = fs.readFileSync('./queues.json').toString(), QUEUE = new Map();
|
|
try {
|
|
let queueJson = JSON.parse(_QUEUE);
|
|
for (let channelId of queueJson) {
|
|
let { teamsize } = queueJson[channelId];
|
|
if (teamsize)
|
|
QUEUE.set(channelId, { teamsize, players: [] });
|
|
}
|
|
}
|
|
catch (e) { }
|
|
function SaveQueue() {
|
|
let queueJson = Object.fromEntries(QUEUE), queueFileJson = {};
|
|
for (let channelId of QUEUE.keys())
|
|
queueFileJson[channelId] = { teamsize: queueJson[channelId].teamsize };
|
|
fs.writeFileSync('./queues.json', JSON.stringify(queueFileJson, null, 2));
|
|
}
|
|
async function checkQueue(channel) {
|
|
let info = QUEUE.get(channel.id);
|
|
if (!info)
|
|
return;
|
|
if (info.players.length > info.teamsize) {
|
|
let team = info.players.splice(0, info.teamsize).map(m => m.toString());
|
|
let embed = new discord_js_1.MessageEmbed()
|
|
.setTitle('Team')
|
|
.setDescription(team.join('\n'));
|
|
await channel.send({ embeds: [embed] });
|
|
}
|
|
}
|
|
var Queue;
|
|
(function (Queue) {
|
|
function create(channelId, teamsize) {
|
|
if (!QUEUE.has(channelId)) {
|
|
QUEUE.set(channelId, { teamsize, players: [] });
|
|
SaveQueue();
|
|
}
|
|
}
|
|
Queue.create = create;
|
|
function remove(channelId) {
|
|
if (QUEUE.has(channelId)) {
|
|
QUEUE.delete(channelId);
|
|
SaveQueue();
|
|
}
|
|
}
|
|
Queue.remove = remove;
|
|
function addPlayer(channelId, member) {
|
|
if (QUEUE.has(channelId)) {
|
|
QUEUE.delete(channelId);
|
|
}
|
|
}
|
|
Queue.addPlayer = addPlayer;
|
|
function removePlayer(channelId, member) {
|
|
if (QUEUE.has(channelId)) {
|
|
QUEUE.delete(channelId);
|
|
}
|
|
}
|
|
Queue.removePlayer = removePlayer;
|
|
})(Queue || (Queue = {}));
|
|
SaveQueue();
|
|
async function discordInit(client) {
|
|
for (let channelId in QUEUE.keys()) {
|
|
let info = QUEUE.get(channelId), channel = await client.channels.fetch(channelId);
|
|
if (!info) { //no idea what could cause this but TS complains
|
|
Queue.remove(channelId);
|
|
continue;
|
|
}
|
|
if (!channel || !(channel instanceof discord_js_1.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');
|
|
}
|
|
}
|
|
exports.discordInit = discordInit;
|
|
var QueueCommands;
|
|
(function (QueueCommands) {
|
|
/**
|
|
* get the queueInfo of an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it does not exist
|
|
* @returns queue info
|
|
*/
|
|
function getInfo(interaction) {
|
|
let info = QUEUE.get(interaction.channelId);
|
|
if (!info)
|
|
throw (0, util_1.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) => ({
|
|
member: (0, util_1.getMember)(interaction),
|
|
channel: (0, util_1.getChannel)(interaction),
|
|
info: getInfo(interaction)
|
|
});
|
|
/**
|
|
* checks if the interaction data is already in the queue
|
|
* @param interaction
|
|
* @returns boolean
|
|
*/
|
|
function queueContains(interaction) {
|
|
let { member, info } = getAll(interaction);
|
|
if (info.players.map(m => m.id).includes(member.id))
|
|
return true;
|
|
return false;
|
|
}
|
|
QueueCommands.queueContains = queueContains;
|
|
/**
|
|
* creates a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be left
|
|
*/
|
|
function queueCreate(interaction) {
|
|
(0, util_1.memberIsModThrow)(interaction);
|
|
let { channelId } = interaction, teamsize = interaction.options.getInteger('teamsize', true);
|
|
if (QUEUE.has(channelId))
|
|
throw (0, util_1.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`);
|
|
}
|
|
QueueCommands.queueCreate = queueCreate;
|
|
/**
|
|
* creates a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be left
|
|
*/
|
|
async function queue(interaction) {
|
|
let teamsize = interaction.options.getInteger('teamsize');
|
|
if (teamsize) {
|
|
queueCreate(interaction);
|
|
return;
|
|
}
|
|
let info = getInfo(interaction);
|
|
let embed = new discord_js_1.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 });
|
|
}
|
|
QueueCommands.queue = queue;
|
|
/**
|
|
* stops a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be joined
|
|
*/
|
|
async function stop(interaction) {
|
|
(0, util_1.memberIsModThrow)(interaction);
|
|
QUEUE.delete(interaction.channelId);
|
|
await interaction.reply('Queue has been reset');
|
|
}
|
|
QueueCommands.stop = stop;
|
|
/**
|
|
* joins a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be readied
|
|
*/
|
|
async function join(interaction) {
|
|
let { member, info, channel } = getAll(interaction);
|
|
if (queueContains(interaction))
|
|
throw (0, util_1.emsg)('You are already in the queue');
|
|
info.players.push(member);
|
|
QUEUE.set(interaction.channelId, info);
|
|
await interaction.reply('Joined the queue');
|
|
checkQueue(channel);
|
|
}
|
|
QueueCommands.join = join;
|
|
/**
|
|
* leaves a queue from an interaction
|
|
* @param interaction
|
|
* @throws errorMessage class if it cannot be reset
|
|
*/
|
|
async function leave(interaction) {
|
|
let { member, info } = getAll(interaction);
|
|
if (!queueContains(interaction))
|
|
throw (0, util_1.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');
|
|
}
|
|
QueueCommands.leave = leave;
|
|
})(QueueCommands = exports.QueueCommands || (exports.QueueCommands = {}));
|