forgot to build
This commit is contained in:
66
dist/api.js
vendored
66
dist/api.js
vendored
@@ -23,6 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.getPlayerInteraction = exports.getPlayer = void 0;
|
exports.getPlayerInteraction = exports.getPlayer = void 0;
|
||||||
|
/*eslint prefer-const: "error"*/
|
||||||
const cheerio = __importStar(require("cheerio"));
|
const cheerio = __importStar(require("cheerio"));
|
||||||
const discord_js_1 = require("discord.js");
|
const discord_js_1 = require("discord.js");
|
||||||
const https_1 = __importDefault(require("https"));
|
const https_1 = __importDefault(require("https"));
|
||||||
@@ -73,25 +74,25 @@ function getHTML(name) {
|
|||||||
* @throws errorMessage class if the request fails
|
* @throws errorMessage class if the request fails
|
||||||
*/
|
*/
|
||||||
function readHTML(html) {
|
function readHTML(html) {
|
||||||
let $ = cheerio.load(html);
|
const $ = cheerio.load(html), foundData = {
|
||||||
let metaElems = $('meta').toArray(), foundData = {
|
name: '',
|
||||||
name: "",
|
id: '',
|
||||||
id: "",
|
avatar: '',
|
||||||
avatar: "",
|
level: '',
|
||||||
level: "",
|
rank: '',
|
||||||
rank: "",
|
|
||||||
elo: null,
|
elo: null,
|
||||||
class: null,
|
class: null,
|
||||||
battles: "",
|
battles: '',
|
||||||
wins: "",
|
wins: '',
|
||||||
winrate: ""
|
winrate: ''
|
||||||
};
|
};
|
||||||
|
let metaElems = $('meta').toArray();
|
||||||
//filter down to just ones named "og:..."
|
//filter down to just ones named "og:..."
|
||||||
metaElems = metaElems.filter(el => el.attribs.property?.startsWith('og:'));
|
metaElems = metaElems.filter(el => el.attribs.property?.startsWith('og:'));
|
||||||
metaElems.forEach(el => {
|
metaElems.forEach(el => {
|
||||||
let attr = el.attribs;
|
const attr = el.attribs;
|
||||||
if (attr.property === 'og:title') {
|
if (attr.property === 'og:title') {
|
||||||
let data = uniteApiRegex.ogtitle.exec(attr.content);
|
const data = uniteApiRegex.ogtitle.exec(attr.content);
|
||||||
if (data !== null && data.length >= 3) {
|
if (data !== null && data.length >= 3) {
|
||||||
foundData.name = data[1];
|
foundData.name = data[1];
|
||||||
foundData.id = data[2];
|
foundData.id = data[2];
|
||||||
@@ -99,14 +100,15 @@ function readHTML(html) {
|
|||||||
}
|
}
|
||||||
else if (attr.property === 'og:description') {
|
else if (attr.property === 'og:description') {
|
||||||
//all lines
|
//all lines
|
||||||
let lines = attr.content.split('\n').map(l => l.trim()), extraLines = [];
|
let lines = attr.content.split('\n').map(l => l.trim());
|
||||||
|
const extraLines = [];
|
||||||
//ensure first line is correct
|
//ensure first line is correct
|
||||||
while (lines.length && !/pok.mon unite/i.test(lines[0])) {
|
while ((lines.length > 0) && !/pok.mon unite/i.test(lines[0])) {
|
||||||
let line = lines.shift();
|
const line = lines.shift();
|
||||||
if (line)
|
if (line !== undefined)
|
||||||
extraLines.push(line);
|
extraLines.push(line);
|
||||||
}
|
}
|
||||||
if (!lines.length)
|
if (lines.length === 0)
|
||||||
throw (0, util_1.emsg)('Unable to read data, please try again');
|
throw (0, util_1.emsg)('Unable to read data, please try again');
|
||||||
//bring the first lines removed back into the data
|
//bring the first lines removed back into the data
|
||||||
lines = [
|
lines = [
|
||||||
@@ -116,10 +118,9 @@ function readHTML(html) {
|
|||||||
//first line
|
//first line
|
||||||
{
|
{
|
||||||
//will be only text after "pokemon unite:"
|
//will be only text after "pokemon unite:"
|
||||||
let line = lines[0].split(':').slice(1).join(':').trim();
|
const line = lines[0].split(':').slice(1).join(':').trim(), regex = uniteApiRegex.ogdescription;
|
||||||
let regex = uniteApiRegex.ogdescription;
|
|
||||||
if (regex[0].test(line)) { //is master/has elo
|
if (regex[0].test(line)) { //is master/has elo
|
||||||
let regexData = line.match(regex[0]);
|
const regexData = line.match(regex[0]);
|
||||||
if (!regexData || regexData.length < 4)
|
if (!regexData || regexData.length < 4)
|
||||||
throw (0, util_1.emsg)('Unable to read data, please try again');
|
throw (0, util_1.emsg)('Unable to read data, please try again');
|
||||||
foundData.level = regexData[1];
|
foundData.level = regexData[1];
|
||||||
@@ -127,7 +128,7 @@ function readHTML(html) {
|
|||||||
foundData.elo = regexData[3];
|
foundData.elo = regexData[3];
|
||||||
}
|
}
|
||||||
else { //is not master/has a class
|
else { //is not master/has a class
|
||||||
let regexData = line.match(regex[1]);
|
const regexData = line.match(regex[1]);
|
||||||
if (!regexData || regexData.length < 4)
|
if (!regexData || regexData.length < 4)
|
||||||
throw (0, util_1.emsg)('Unable to read data, please try again');
|
throw (0, util_1.emsg)('Unable to read data, please try again');
|
||||||
foundData.level = regexData[1];
|
foundData.level = regexData[1];
|
||||||
@@ -138,7 +139,7 @@ function readHTML(html) {
|
|||||||
lines.shift();
|
lines.shift();
|
||||||
//rest of lines
|
//rest of lines
|
||||||
lines.forEach(line => {
|
lines.forEach(line => {
|
||||||
let split = line.split(':').map(l => l.trim()), key = split[0].toLowerCase().replace(/[^\w]/g, ''), value = split[1];
|
const split = line.split(':').map(l => l.trim()), key = split[0].toLowerCase().replace(/[^\w]/g, ''), value = split[1];
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'battles':
|
case 'battles':
|
||||||
foundData.battles = value;
|
foundData.battles = value;
|
||||||
@@ -153,7 +154,8 @@ function readHTML(html) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
foundData.avatar = $('.player-card-image img').attr('src') || "";
|
const imgSrc = $('.player-card-image img').attr('src');
|
||||||
|
foundData.avatar = imgSrc !== undefined ? imgSrc : '';
|
||||||
foundData.avatar = foundData.avatar.replace('../', 'https://uniteapi.dev/');
|
foundData.avatar = foundData.avatar.replace('../', 'https://uniteapi.dev/');
|
||||||
return foundData;
|
return foundData;
|
||||||
}
|
}
|
||||||
@@ -163,7 +165,7 @@ function readHTML(html) {
|
|||||||
* @returns boolean, valid or invalid
|
* @returns boolean, valid or invalid
|
||||||
*/
|
*/
|
||||||
function verifyData(data) {
|
function verifyData(data) {
|
||||||
if (data.id.length)
|
if (data.id.length > 0)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -173,21 +175,25 @@ function verifyData(data) {
|
|||||||
* @returns player data
|
* @returns player data
|
||||||
*/
|
*/
|
||||||
async function getPlayer(name) {
|
async function getPlayer(name) {
|
||||||
let html = await getHTML(name);
|
const html = await getHTML(name), data = readHTML(html);
|
||||||
let data = readHTML(html);
|
|
||||||
if (verifyData(data))
|
if (verifyData(data))
|
||||||
return data;
|
return data;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
exports.getPlayer = getPlayer;
|
exports.getPlayer = getPlayer;
|
||||||
async function sendPlayerEmbed(interaction, data) {
|
async function sendPlayerEmbed(interaction, data) {
|
||||||
let embed = new discord_js_1.MessageEmbed()
|
let eloStr;
|
||||||
|
if (data.elo !== null)
|
||||||
|
eloStr = `(${data.elo})`;
|
||||||
|
else
|
||||||
|
eloStr = `Class ${data.class}`;
|
||||||
|
const embed = new discord_js_1.MessageEmbed()
|
||||||
.setTitle(`${data.name} (${data.id})`)
|
.setTitle(`${data.name} (${data.id})`)
|
||||||
.setURL(`https://uniteapi.dev/p/${encodeURIComponent(data.name)}`)
|
.setURL(`https://uniteapi.dev/p/${encodeURIComponent(data.name)}`)
|
||||||
.setTimestamp()
|
.setTimestamp()
|
||||||
.setThumbnail(data.avatar)
|
.setThumbnail(data.avatar)
|
||||||
.setDescription(`Level ${data.level}
|
.setDescription(`Level ${data.level}
|
||||||
${data.rank} ${data.elo ? `(${data.elo})` : `Class ${data.class}`}
|
${data.rank} ${eloStr}
|
||||||
|
|
||||||
**Battles** ${data.battles}
|
**Battles** ${data.battles}
|
||||||
**Wins** ${data.wins}
|
**Wins** ${data.wins}
|
||||||
@@ -200,9 +206,9 @@ ${data.rank} ${data.elo ? `(${data.elo})` : `Class ${data.class}`}
|
|||||||
* @throws errorMessage class if the user cannot be found
|
* @throws errorMessage class if the user cannot be found
|
||||||
*/
|
*/
|
||||||
async function getPlayerInteraction(interaction) {
|
async function getPlayerInteraction(interaction) {
|
||||||
let username = interaction.options.getString('username', true);
|
const username = interaction.options.getString('username', true);
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
let data = await getPlayer(username);
|
const data = await getPlayer(username);
|
||||||
if (data === null)
|
if (data === null)
|
||||||
throw (0, util_1.emsg)('api.noUser');
|
throw (0, util_1.emsg)('api.noUser');
|
||||||
else
|
else
|
||||||
|
|||||||
4
dist/discord.js
vendored
4
dist/discord.js
vendored
@@ -1,6 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.registerCommands = void 0;
|
exports.registerCommands = void 0;
|
||||||
|
/* eslint-disable camelcase */
|
||||||
const rest_1 = require("@discordjs/rest");
|
const rest_1 = require("@discordjs/rest");
|
||||||
const v9_1 = require("discord-api-types/v9");
|
const v9_1 = require("discord-api-types/v9");
|
||||||
// list of commands to register with discord
|
// list of commands to register with discord
|
||||||
@@ -46,7 +47,8 @@ const commands = [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
], commandNames = commands.map(c => c.name);
|
];
|
||||||
|
/*commandNames = commands.map(c => c.name);*/
|
||||||
/**
|
/**
|
||||||
* register/reload commands on guild(s)
|
* register/reload commands on guild(s)
|
||||||
* @param token discord bot token
|
* @param token discord bot token
|
||||||
|
|||||||
6
dist/index.js
vendored
6
dist/index.js
vendored
@@ -23,7 +23,7 @@ const discord_js_1 = require("discord.js");
|
|||||||
const fs = __importStar(require("fs"));
|
const fs = __importStar(require("fs"));
|
||||||
const api_1 = require("./api");
|
const api_1 = require("./api");
|
||||||
const discord_1 = require("./discord");
|
const discord_1 = require("./discord");
|
||||||
const lang_1 = require("./lang");
|
const Lang = __importStar(require("./lang"));
|
||||||
const queue_1 = require("./queue");
|
const queue_1 = require("./queue");
|
||||||
const util_1 = require("./util");
|
const util_1 = require("./util");
|
||||||
const CLIENT = new discord_js_1.Client({ intents: [discord_js_1.Intents.FLAGS.GUILDS] });
|
const CLIENT = new discord_js_1.Client({ intents: [discord_js_1.Intents.FLAGS.GUILDS] });
|
||||||
@@ -32,13 +32,13 @@ console.log(new Date().toISOString() + '\n\n');
|
|||||||
//get token
|
//get token
|
||||||
if (!fs.existsSync('./token.txt')) {
|
if (!fs.existsSync('./token.txt')) {
|
||||||
fs.writeFileSync('./token.txt', '');
|
fs.writeFileSync('./token.txt', '');
|
||||||
console.error(lang_1.Lang.get('error.main.missingToken'));
|
console.error(Lang.get('error.main.missingToken'));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const TOKEN = fs.readFileSync('./token.txt').toString();
|
const TOKEN = fs.readFileSync('./token.txt').toString();
|
||||||
//discord connections
|
//discord connections
|
||||||
CLIENT.on('ready', client => {
|
CLIENT.on('ready', client => {
|
||||||
console.log(lang_1.Lang.get('main.login', {
|
console.log(Lang.get('main.login', {
|
||||||
user: client.user.tag
|
user: client.user.tag
|
||||||
}));
|
}));
|
||||||
client.guilds.fetch().then(guilds => (0, discord_1.registerCommands)(TOKEN, client.user.id, guilds.map(g => g.id)));
|
client.guilds.fetch().then(guilds => (0, discord_1.registerCommands)(TOKEN, client.user.id, guilds.map(g => g.id)));
|
||||||
|
|||||||
37
dist/lang.js
vendored
37
dist/lang.js
vendored
@@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.Lang = void 0;
|
exports.get = exports.setLang = void 0;
|
||||||
const LANG = {
|
const LANG = {
|
||||||
en: {
|
en: {
|
||||||
main: {
|
main: {
|
||||||
@@ -20,7 +20,7 @@ const LANG = {
|
|||||||
discord: {
|
discord: {
|
||||||
noQueue: 'There is not an active queue in this channel, type `/open` to create one',
|
noQueue: 'There is not an active queue in this channel, type `/open` to create one',
|
||||||
noChannel: 'Unable to find channel {channelId} for teams of {teamsize}',
|
noChannel: 'Unable to find channel {channelId} for teams of {teamsize}',
|
||||||
noCreate: 'There is already an active queue in this channel for teams of ${teamsize}',
|
noCreate: 'There is already an active queue in this channel for teams of {teamsize}',
|
||||||
inQueue: 'You are already in the queue',
|
inQueue: 'You are already in the queue',
|
||||||
notInQueue: 'You aren\'t in the queue',
|
notInQueue: 'You aren\'t in the queue',
|
||||||
notMod: 'Member is not a moderator'
|
notMod: 'Member is not a moderator'
|
||||||
@@ -35,37 +35,35 @@ const LANG = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var Lang;
|
let LANGID = 'en';
|
||||||
(function (Lang) {
|
if (!(LANGID in LANG))
|
||||||
var LANGID = 'en';
|
|
||||||
if (!(LANGID in LANG))
|
|
||||||
throw 'language id does not exist';
|
throw 'language id does not exist';
|
||||||
function setLang(langid) {
|
function setLang(langid) {
|
||||||
if (langid in LANG)
|
if (langid in LANG)
|
||||||
LANGID = langid;
|
LANGID = langid;
|
||||||
else
|
else
|
||||||
throw 'language id does not exist';
|
throw 'language id does not exist';
|
||||||
}
|
}
|
||||||
Lang.setLang = setLang;
|
exports.setLang = setLang;
|
||||||
function template(str, args) {
|
function template(str, args) {
|
||||||
return str.replace(/{\w+}/g, str => {
|
return str.replace(/{\w+}/g, str => {
|
||||||
let key = str.substring(1, str.length - 1);
|
const key = str.substring(1, str.length - 1);
|
||||||
if (key in args)
|
if (key in args)
|
||||||
return args[key];
|
return args[key];
|
||||||
return key;
|
return key;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* reads language json
|
* reads language json
|
||||||
* @param id ex: discord.error.noActiveQueue
|
* @param id ex: discord.error.noActiveQueue
|
||||||
* @returns language value, defaults to `id` parameter
|
* @returns language value, defaults to `id` parameter
|
||||||
*/
|
*/
|
||||||
function get(id, args = {}) {
|
function get(id, args = {}) {
|
||||||
let keySpl = id.split('.').map(k => k.trim()).filter(k => k);
|
const keySpl = id.split('.').map(k => k.trim()).filter(k => k);
|
||||||
let finding = LANG[LANGID];
|
let finding = LANG[LANGID];
|
||||||
for (let key of keySpl) {
|
for (const key of keySpl) {
|
||||||
if (key in finding) {
|
if (key in finding) {
|
||||||
let found = finding[key];
|
const found = finding[key];
|
||||||
if (typeof found === 'string')
|
if (typeof found === 'string')
|
||||||
return template(found, args);
|
return template(found, args);
|
||||||
finding = found;
|
finding = found;
|
||||||
@@ -74,6 +72,5 @@ var Lang;
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
Lang.get = get;
|
exports.get = get;
|
||||||
})(Lang = exports.Lang || (exports.Lang = {}));
|
|
||||||
|
|||||||
179
dist/queue.js
vendored
179
dist/queue.js
vendored
@@ -23,210 +23,187 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
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 discord_js_1 = require("discord.js");
|
||||||
const fs = __importStar(require("fs"));
|
const fs = __importStar(require("fs"));
|
||||||
const util_1 = require("./util");
|
const util_1 = require("./util");
|
||||||
const lang_1 = require("./lang");
|
const Lang = __importStar(require("./lang"));
|
||||||
//load queues from file
|
//load queues from file
|
||||||
if (!fs.existsSync('./queues.json'))
|
if (!fs.existsSync('./queues.json'))
|
||||||
fs.writeFileSync('./queues.json', '{}');
|
fs.writeFileSync('./queues.json', '{}');
|
||||||
const _QUEUE = fs.readFileSync('./queues.json').toString(), QUEUE = new Map();
|
const _QUEUE = fs.readFileSync('./queues.json').toString(), QUEUE = new Map();
|
||||||
try {
|
try {
|
||||||
let queueJson = JSON.parse(_QUEUE);
|
const queueJson = JSON.parse(_QUEUE);
|
||||||
for (let channelId in queueJson) {
|
for (const channelId in queueJson) {
|
||||||
let { teamsize } = queueJson[channelId];
|
const { teamsize } = queueJson[channelId];
|
||||||
if (teamsize)
|
if (teamsize !== 0)
|
||||||
QUEUE.set(channelId, { teamsize, players: [] });
|
QUEUE.set(channelId, { teamsize, players: [] });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) { }
|
catch (e) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
function SaveQueue() {
|
function SaveQueue() {
|
||||||
let queueJson = Object.fromEntries(QUEUE), queueFileJson = {};
|
const queueJson = Object.fromEntries(QUEUE), queueFileJson = {};
|
||||||
for (let channelId of QUEUE.keys())
|
for (const channelId of QUEUE.keys())
|
||||||
queueFileJson[channelId] = { teamsize: queueJson[channelId].teamsize };
|
queueFileJson[channelId] = { teamsize: queueJson[channelId].teamsize };
|
||||||
fs.writeFileSync('./queues.json', JSON.stringify(queueFileJson, null, 2));
|
fs.writeFileSync('./queues.json', JSON.stringify(queueFileJson, null, 2));
|
||||||
}
|
}
|
||||||
async function checkQueue(channel) {
|
async function checkQueue(channel) {
|
||||||
let info = QUEUE.get(channel.id);
|
const info = QUEUE.get(channel.id);
|
||||||
if (!info)
|
if (!info)
|
||||||
return;
|
return;
|
||||||
if (info.players.length >= info.teamsize) {
|
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
|
//TODO add embeds to lang.ts
|
||||||
let embed = new discord_js_1.MessageEmbed()
|
const embed = new discord_js_1.MessageEmbed()
|
||||||
.setTitle('Team')
|
.setTitle('Team')
|
||||||
.setDescription(team.join('\n'));
|
.setDescription(team.join('\n'));
|
||||||
await channel.send({ embeds: [embed] });
|
await channel.send({ embeds: [embed] });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var Queue;
|
function queueCreate(channelId, teamsize) {
|
||||||
(function (Queue) {
|
|
||||||
function create(channelId, teamsize) {
|
|
||||||
if (!QUEUE.has(channelId)) {
|
if (!QUEUE.has(channelId)) {
|
||||||
QUEUE.set(channelId, { teamsize, players: [] });
|
QUEUE.set(channelId, { teamsize, players: [] });
|
||||||
SaveQueue();
|
SaveQueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Queue.create = create;
|
exports.queueCreate = queueCreate;
|
||||||
function remove(channelId) {
|
function queueRemove(channelId) {
|
||||||
if (QUEUE.has(channelId)) {
|
if (QUEUE.has(channelId)) {
|
||||||
QUEUE.delete(channelId);
|
QUEUE.delete(channelId);
|
||||||
SaveQueue();
|
SaveQueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Queue.remove = remove;
|
exports.queueRemove = queueRemove;
|
||||||
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();
|
SaveQueue();
|
||||||
async function discordInit(client) {
|
async function discordInit(client) {
|
||||||
for (let channelId of QUEUE.keys()) {
|
for (const channelId of QUEUE.keys()) {
|
||||||
let info = QUEUE.get(channelId), channel = await client.channels.fetch(channelId);
|
const info = QUEUE.get(channelId), channel = await client.channels.fetch(channelId);
|
||||||
if (!info) { //no idea what could cause this but TS complains
|
if (!info) { //no idea what could cause this but TS complains
|
||||||
Queue.remove(channelId);
|
queueRemove(channelId);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!channel || !(channel instanceof discord_js_1.TextChannel)) {
|
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,
|
channelId,
|
||||||
teamsize: info.teamsize
|
teamsize: info.teamsize
|
||||||
});
|
});
|
||||||
Queue.remove(channelId);
|
queueRemove(channelId);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
channel.send(lang_1.Lang.get('discord.botRestart'));
|
channel.send(Lang.get('discord.botRestart'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.discordInit = discordInit;
|
exports.discordInit = discordInit;
|
||||||
var QueueCommands;
|
/**
|
||||||
(function (QueueCommands) {
|
|
||||||
/**
|
|
||||||
* get the queueInfo of an interaction
|
* get the queueInfo of an interaction
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it does not exist
|
* @throws errorMessage class if it does not exist
|
||||||
* @returns queue info
|
* @returns queue info
|
||||||
*/
|
*/
|
||||||
function getInfo(interaction) {
|
function getInfo(interaction) {
|
||||||
let info = QUEUE.get(interaction.channelId);
|
const info = QUEUE.get(interaction.channelId);
|
||||||
if (!info)
|
if (!info)
|
||||||
throw (0, util_1.emsg)('discord.noQueue');
|
throw (0, util_1.emsg)('discord.noQueue');
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* compiles all the get functions above
|
* compiles all the get functions above
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws if another get function throws
|
* @throws if another get function throws
|
||||||
* @returns object containing each
|
* @returns object containing each
|
||||||
*/
|
*/
|
||||||
const getAll = (interaction) => ({
|
const getAll = (interaction) => ({
|
||||||
member: (0, util_1.getMember)(interaction),
|
member: (0, util_1.getMember)(interaction),
|
||||||
channel: (0, util_1.getChannel)(interaction),
|
channel: (0, util_1.getChannel)(interaction),
|
||||||
info: getInfo(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, info } = getAll(interaction);
|
const { 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;
|
||||||
}
|
}
|
||||||
QueueCommands.queueContains = queueContains;
|
exports.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()
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
QueueCommands.queueCreate = queueCreate;
|
|
||||||
/**
|
|
||||||
* opens a queue
|
* opens a queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it cannot be left
|
* @throws errorMessage class if it cannot be left
|
||||||
*/
|
*/
|
||||||
async function open(interaction) {
|
function open(interaction) {
|
||||||
queueCreate(interaction);
|
(0, util_1.memberIsModThrow)(interaction);
|
||||||
}
|
const { channelId } = interaction, teamsize = interaction.options.getInteger('teamsize', true);
|
||||||
QueueCommands.open = open;
|
const existing = QUEUE.get(channelId);
|
||||||
/**
|
if (existing)
|
||||||
|
throw (0, util_1.emsg)(Lang.get('error.discord.noCreate', {
|
||||||
|
teamsize: existing.teamsize.toString()
|
||||||
|
}));
|
||||||
|
queueCreate(channelId, teamsize);
|
||||||
|
interaction.reply(Lang.get('discord.create', {
|
||||||
|
teamsize: teamsize.toString()
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
/**
|
||||||
* closes a queue
|
* closes a queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it cannot be joined
|
* @throws errorMessage class if it cannot be joined
|
||||||
*/
|
*/
|
||||||
async function close(interaction) {
|
async function close(interaction) {
|
||||||
(0, util_1.memberIsModThrow)(interaction);
|
(0, util_1.memberIsModThrow)(interaction);
|
||||||
QUEUE.delete(interaction.channelId);
|
QUEUE.delete(interaction.channelId);
|
||||||
await interaction.reply(lang_1.Lang.get('discord.close'));
|
await interaction.reply(Lang.get('discord.close'));
|
||||||
}
|
}
|
||||||
QueueCommands.close = close;
|
/**
|
||||||
/**
|
|
||||||
* gives info about the queue
|
* gives info about the queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it cannot be left
|
* @throws errorMessage class if it cannot be left
|
||||||
*/
|
*/
|
||||||
async function queue(interaction) {
|
async function queue(interaction) {
|
||||||
let info = getInfo(interaction);
|
const info = getInfo(interaction);
|
||||||
let embed = new discord_js_1.MessageEmbed()
|
const embed = new discord_js_1.MessageEmbed()
|
||||||
.setTitle('Active Queue')
|
.setTitle('Active Queue')
|
||||||
.addField('Team Size', info.teamsize.toString(), true)
|
.addField('Team Size', info.teamsize.toString(), true)
|
||||||
.addField('Players Joined', info.players.length.toString(), true)
|
.addField('Players Joined', info.players.length.toString(), true)
|
||||||
.setFooter({ text: 'type /join' }); //TODO
|
.setFooter({ text: 'type /join' }); //TODO
|
||||||
await interaction.reply({ embeds: [embed], ephemeral: true });
|
await interaction.reply({ embeds: [embed], ephemeral: true });
|
||||||
}
|
}
|
||||||
QueueCommands.queue = queue;
|
/**
|
||||||
/**
|
|
||||||
* joins a queue
|
* joins a queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it cannot be readied
|
* @throws errorMessage class if it cannot be readied
|
||||||
*/
|
*/
|
||||||
async function join(interaction) {
|
async function join(interaction) {
|
||||||
let { member, info, channel } = getAll(interaction);
|
const { member, info, channel } = getAll(interaction);
|
||||||
if (queueContains(interaction))
|
if (queueContains(interaction))
|
||||||
throw (0, util_1.emsg)('discord.inQueue');
|
throw (0, util_1.emsg)('discord.inQueue');
|
||||||
info.players.push(member);
|
info.players.push(member);
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
await interaction.reply(lang_1.Lang.get('discord.join'));
|
await interaction.reply(Lang.get('discord.join'));
|
||||||
checkQueue(channel);
|
checkQueue(channel);
|
||||||
}
|
}
|
||||||
QueueCommands.join = join;
|
/**
|
||||||
/**
|
|
||||||
* leaves a queue
|
* leaves a queue
|
||||||
* @param interaction
|
* @param interaction
|
||||||
* @throws errorMessage class if it cannot be reset
|
* @throws errorMessage class if it cannot be reset
|
||||||
*/
|
*/
|
||||||
async function leave(interaction) {
|
async function leave(interaction) {
|
||||||
let { member, info } = getAll(interaction);
|
const { member, info } = getAll(interaction);
|
||||||
if (!queueContains(interaction))
|
if (!queueContains(interaction))
|
||||||
throw (0, util_1.emsg)('discord.notInQueue');
|
throw (0, util_1.emsg)('discord.notInQueue');
|
||||||
info.players.splice(info.players.indexOf(member), 1);
|
info.players.splice(info.players.indexOf(member), 1);
|
||||||
QUEUE.set(interaction.channelId, info);
|
QUEUE.set(interaction.channelId, info);
|
||||||
await interaction.reply(lang_1.Lang.get('discord.leave'));
|
await interaction.reply(Lang.get('discord.leave'));
|
||||||
}
|
}
|
||||||
QueueCommands.leave = leave;
|
exports.QueueCommands = {
|
||||||
})(QueueCommands = exports.QueueCommands || (exports.QueueCommands = {}));
|
open,
|
||||||
|
close,
|
||||||
|
queue,
|
||||||
|
join,
|
||||||
|
leave
|
||||||
|
};
|
||||||
|
|||||||
1
dist/queueCommands.js
vendored
Normal file
1
dist/queueCommands.js
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"use strict";
|
||||||
30
dist/util.js
vendored
30
dist/util.js
vendored
@@ -1,14 +1,34 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
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 });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.memberIsModThrow = exports.memberIsMod = exports.getChannel = exports.getMember = exports.emsg = exports.errorMessage = exports.shuffle = void 0;
|
exports.memberIsModThrow = exports.memberIsMod = exports.getChannel = exports.getMember = exports.emsg = exports.errorMessage = exports.shuffle = void 0;
|
||||||
const discord_js_1 = require("discord.js");
|
const discord_js_1 = require("discord.js");
|
||||||
const lang_1 = require("./lang");
|
const Lang = __importStar(require("./lang"));
|
||||||
/**
|
/**
|
||||||
* shuffles an array
|
* shuffles an array
|
||||||
* https://stackoverflow.com/a/2450976/2856416
|
* https://stackoverflow.com/a/2450976/2856416
|
||||||
* @param array an array
|
* @param array an array
|
||||||
* @returns an array but shuffled
|
* @returns an array but shuffled
|
||||||
*/
|
*/
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
function shuffle(array) {
|
function shuffle(array) {
|
||||||
let currentIndex = array.length, randomIndex;
|
let currentIndex = array.length, randomIndex;
|
||||||
// While there remain elements to shuffle...
|
// While there remain elements to shuffle...
|
||||||
@@ -37,7 +57,7 @@ exports.errorMessage = errorMessage;
|
|||||||
* @param ephemeral (default=true)
|
* @param ephemeral (default=true)
|
||||||
* @returns new errorMessage
|
* @returns new errorMessage
|
||||||
*/
|
*/
|
||||||
const emsg = (msg, ephemeral = true) => new errorMessage(lang_1.Lang.get(`error.${msg}`), ephemeral);
|
const emsg = (msg, ephemeral = true) => new errorMessage(Lang.get(`error.${msg}`), ephemeral);
|
||||||
exports.emsg = emsg;
|
exports.emsg = emsg;
|
||||||
/**
|
/**
|
||||||
* get the GuildMember of an interaction
|
* get the GuildMember of an interaction
|
||||||
@@ -46,7 +66,7 @@ exports.emsg = emsg;
|
|||||||
* @returns member
|
* @returns member
|
||||||
*/
|
*/
|
||||||
function getMember(interaction) {
|
function getMember(interaction) {
|
||||||
let member = interaction.member;
|
const member = interaction.member;
|
||||||
if (!(member instanceof discord_js_1.GuildMember))
|
if (!(member instanceof discord_js_1.GuildMember))
|
||||||
throw (0, exports.emsg)('general.noMember');
|
throw (0, exports.emsg)('general.noMember');
|
||||||
return member;
|
return member;
|
||||||
@@ -59,14 +79,14 @@ exports.getMember = getMember;
|
|||||||
* @returns member
|
* @returns member
|
||||||
*/
|
*/
|
||||||
function getChannel(interaction) {
|
function getChannel(interaction) {
|
||||||
let channel = interaction.channel;
|
const channel = interaction.channel;
|
||||||
if (!(channel instanceof discord_js_1.TextChannel))
|
if (!(channel instanceof discord_js_1.TextChannel))
|
||||||
throw (0, exports.emsg)('general.noChannel');
|
throw (0, exports.emsg)('general.noChannel');
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
exports.getChannel = getChannel;
|
exports.getChannel = getChannel;
|
||||||
function memberIsMod(interaction) {
|
function memberIsMod(interaction) {
|
||||||
let member = getMember(interaction);
|
const member = getMember(interaction);
|
||||||
return member.permissionsIn(interaction.channelId).has('MANAGE_MESSAGES');
|
return member.permissionsIn(interaction.channelId).has('MANAGE_MESSAGES');
|
||||||
}
|
}
|
||||||
exports.memberIsMod = memberIsMod;
|
exports.memberIsMod = memberIsMod;
|
||||||
|
|||||||
Reference in New Issue
Block a user