forgot to build

This commit is contained in:
2022-02-13 21:42:04 -06:00
parent f5ab2b4297
commit 8503f274bd
7 changed files with 248 additions and 245 deletions

307
dist/queue.js vendored
View File

@@ -23,210 +23,187 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueueCommands = exports.discordInit = void 0;
exports.QueueCommands = exports.queueContains = exports.discordInit = exports.queueRemove = exports.queueCreate = void 0;
const discord_js_1 = require("discord.js");
const fs = __importStar(require("fs"));
const util_1 = require("./util");
const lang_1 = require("./lang");
const Lang = __importStar(require("./lang"));
//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 in queueJson) {
let { teamsize } = queueJson[channelId];
if (teamsize)
const queueJson = JSON.parse(_QUEUE);
for (const channelId in queueJson) {
const { teamsize } = queueJson[channelId];
if (teamsize !== 0)
QUEUE.set(channelId, { teamsize, players: [] });
}
}
catch (e) { }
catch (e) {
//do nothing
}
function SaveQueue() {
let queueJson = Object.fromEntries(QUEUE), queueFileJson = {};
for (let channelId of QUEUE.keys())
const queueJson = Object.fromEntries(QUEUE), queueFileJson = {};
for (const 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);
const 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());
const team = info.players.splice(0, info.teamsize).map(m => m.toString());
//TODO add embeds to lang.ts
let embed = new discord_js_1.MessageEmbed()
const 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();
}
function queueCreate(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();
}
}
exports.queueCreate = queueCreate;
function queueRemove(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 = {}));
}
exports.queueRemove = queueRemove;
SaveQueue();
async function discordInit(client) {
for (let channelId of QUEUE.keys()) {
let info = QUEUE.get(channelId), channel = await client.channels.fetch(channelId);
for (const channelId of QUEUE.keys()) {
const info = QUEUE.get(channelId), channel = await client.channels.fetch(channelId);
if (!info) { //no idea what could cause this but TS complains
Queue.remove(channelId);
queueRemove(channelId);
continue;
}
if (!channel || !(channel instanceof discord_js_1.TextChannel)) {
console.error(lang_1.Lang.get('error.discord.noChannel'), {
console.error(Lang.get('error.discord.noChannel'), {
channelId,
teamsize: info.teamsize
});
Queue.remove(channelId);
queueRemove(channelId);
continue;
}
channel.send(lang_1.Lang.get('discord.botRestart'));
channel.send(Lang.get('discord.botRestart'));
}
}
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)('discord.noQueue');
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);
let existing = QUEUE.get(channelId);
if (existing)
throw (0, util_1.emsg)(lang_1.Lang.get('error.discord.noCreate', {
teamsize: existing.teamsize.toString()
}));
Queue.create(channelId, teamsize);
interaction.reply(lang_1.Lang.get('discord.create', {
teamsize: teamsize.toString()
/**
* get the queueInfo of an interaction
* @param interaction
* @throws errorMessage class if it does not exist
* @returns queue info
*/
function getInfo(interaction) {
const info = QUEUE.get(interaction.channelId);
if (!info)
throw (0, util_1.emsg)('discord.noQueue');
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) {
const { member, info } = getAll(interaction);
if (info.players.map(m => m.id).includes(member.id))
return true;
return false;
}
exports.queueContains = queueContains;
/**
* opens a queue
* @param interaction
* @throws errorMessage class if it cannot be left
*/
function open(interaction) {
(0, util_1.memberIsModThrow)(interaction);
const { channelId } = interaction, teamsize = interaction.options.getInteger('teamsize', true);
const existing = QUEUE.get(channelId);
if (existing)
throw (0, util_1.emsg)(Lang.get('error.discord.noCreate', {
teamsize: existing.teamsize.toString()
}));
}
QueueCommands.queueCreate = queueCreate;
/**
* opens a queue
* @param interaction
* @throws errorMessage class if it cannot be left
*/
async function open(interaction) {
queueCreate(interaction);
}
QueueCommands.open = open;
/**
* closes a queue
* @param interaction
* @throws errorMessage class if it cannot be joined
*/
async function close(interaction) {
(0, util_1.memberIsModThrow)(interaction);
QUEUE.delete(interaction.channelId);
await interaction.reply(lang_1.Lang.get('discord.close'));
}
QueueCommands.close = close;
/**
* gives info about the queue
* @param interaction
* @throws errorMessage class if it cannot be left
*/
async function queue(interaction) {
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' }); //TODO
await interaction.reply({ embeds: [embed], ephemeral: true });
}
QueueCommands.queue = queue;
/**
* joins a queue
* @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)('discord.inQueue');
info.players.push(member);
QUEUE.set(interaction.channelId, info);
await interaction.reply(lang_1.Lang.get('discord.join'));
checkQueue(channel);
}
QueueCommands.join = join;
/**
* leaves a queue
* @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)('discord.notInQueue');
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(interaction.channelId, info);
await interaction.reply(lang_1.Lang.get('discord.leave'));
}
QueueCommands.leave = leave;
})(QueueCommands = exports.QueueCommands || (exports.QueueCommands = {}));
queueCreate(channelId, teamsize);
interaction.reply(Lang.get('discord.create', {
teamsize: teamsize.toString()
}));
}
/**
* closes a queue
* @param interaction
* @throws errorMessage class if it cannot be joined
*/
async function close(interaction) {
(0, util_1.memberIsModThrow)(interaction);
QUEUE.delete(interaction.channelId);
await interaction.reply(Lang.get('discord.close'));
}
/**
* gives info about the queue
* @param interaction
* @throws errorMessage class if it cannot be left
*/
async function queue(interaction) {
const info = getInfo(interaction);
const 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' }); //TODO
await interaction.reply({ embeds: [embed], ephemeral: true });
}
/**
* joins a queue
* @param interaction
* @throws errorMessage class if it cannot be readied
*/
async function join(interaction) {
const { member, info, channel } = getAll(interaction);
if (queueContains(interaction))
throw (0, util_1.emsg)('discord.inQueue');
info.players.push(member);
QUEUE.set(interaction.channelId, info);
await interaction.reply(Lang.get('discord.join'));
checkQueue(channel);
}
/**
* leaves a queue
* @param interaction
* @throws errorMessage class if it cannot be reset
*/
async function leave(interaction) {
const { member, info } = getAll(interaction);
if (!queueContains(interaction))
throw (0, util_1.emsg)('discord.notInQueue');
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(interaction.channelId, info);
await interaction.reply(Lang.get('discord.leave'));
}
exports.QueueCommands = {
open,
close,
queue,
join,
leave
};