From 5ce68086b880c13f85973054cd7ab4c9df3117f9 Mon Sep 17 00:00:00 2001 From: ashley zomo Date: Mon, 31 Jan 2022 15:53:23 -0600 Subject: [PATCH] updated to es2020 --- dist/api.js | 43 +++++++----------- dist/discord.js | 36 ++++++--------- dist/index.js | 28 +++++------- dist/queue.js | 115 ++++++++++++++++++++---------------------------- tsconfig.json | 2 +- 5 files changed, 88 insertions(+), 136 deletions(-) diff --git a/dist/api.js b/dist/api.js index 26f7d3b..47c0851 100644 --- a/dist/api.js +++ b/dist/api.js @@ -18,15 +18,6 @@ var __importStar = (this && this.__importStar) || function (mod) { __setModuleDefault(result, mod); return result; }; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; @@ -107,7 +98,7 @@ function readHTML(html) { winrate: "" }; //filter down to just ones named "og:..." - metaElems = metaElems.filter(el => { var _a; return (_a = el.attribs.property) === null || _a === void 0 ? void 0 : _a.startsWith('og:'); }); + metaElems = metaElems.filter(el => el.attribs.property?.startsWith('og:')); metaElems.forEach(el => { let attr = el.attribs; if (attr.property === 'og:title') { @@ -201,30 +192,26 @@ function verifyData(data) { * @param name name of player * @returns player data */ -function getPlayer(name) { - return __awaiter(this, void 0, void 0, function* () { - let html = yield getHTML(name); - let data = readHTML(html); - if (verifyData(data)) - return data; - return null; - }); +async function getPlayer(name) { + let html = await getHTML(name); + let data = readHTML(html); + if (verifyData(data)) + return data; + return null; } exports.getPlayer = getPlayer; /** * calls getPlayer() with the name from the interaction * @param interaction discord interaction */ -function getPlayerInteraction(interaction) { - return __awaiter(this, void 0, void 0, function* () { - let username = interaction.options.getString('username', true); - yield interaction.deferReply(); - let data = yield getPlayer(username); - if (data === null) - yield interaction.editReply('Unable to find user'); - else - yield interaction.editReply('```\n' + JSON.stringify(data, null, 2) + '\n```'); - }); +async function getPlayerInteraction(interaction) { + let username = interaction.options.getString('username', true); + await interaction.deferReply(); + let data = await getPlayer(username); + if (data === null) + await interaction.editReply('Unable to find user'); + else + await interaction.editReply('```\n' + JSON.stringify(data, null, 2) + '\n```'); } exports.getPlayerInteraction = getPlayerInteraction; //await getPlayer('IanWhysp') diff --git a/dist/discord.js b/dist/discord.js index f86ad58..0ee607b 100644 --- a/dist/discord.js +++ b/dist/discord.js @@ -1,17 +1,9 @@ "use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerCommands = void 0; const rest_1 = require("@discordjs/rest"); const v9_1 = require("discord-api-types/v9"); +// list of commands to register with discord const commands = [ { name: 'queue', @@ -60,20 +52,18 @@ const commands = [ * @param clientId discord bot client id * @param guildIds discord guild id(s) */ -function registerCommands(token, clientId, guildIds) { - return __awaiter(this, void 0, void 0, function* () { - const rest = new rest_1.REST({ version: '9' }).setToken(token); - if (typeof guildIds === 'string') - guildIds = [guildIds]; - for (let i = 0; i < guildIds.length; i++) { - try { - yield rest.put(v9_1.Routes.applicationGuildCommands(clientId, guildIds[i]), { body: commands }); - console.log(`[${guildIds[i]}] registered command`); - } - catch (error) { - console.error(error); - } +async function registerCommands(token, clientId, guildIds) { + const rest = new rest_1.REST({ version: '9' }).setToken(token); + if (typeof guildIds === 'string') + guildIds = [guildIds]; + for (let i = 0; i < guildIds.length; i++) { + try { + await rest.put(v9_1.Routes.applicationGuildCommands(clientId, guildIds[i]), { body: commands }); + console.log(`[${guildIds[i]}] registered command`); } - }); + catch (error) { + console.error(error); + } + } } exports.registerCommands = registerCommands; diff --git a/dist/index.js b/dist/index.js index 12598e7..5c47fc3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -18,15 +18,6 @@ var __importStar = (this && this.__importStar) || function (mod) { __setModuleDefault(result, mod); return result; }; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; Object.defineProperty(exports, "__esModule", { value: true }); const discord_js_1 = require("discord.js"); const fs = __importStar(require("fs")); @@ -34,33 +25,36 @@ const api_1 = require("./api"); const discord_1 = require("./discord"); const queue_1 = require("./queue"); const CLIENT = new discord_js_1.Client({ intents: [discord_js_1.Intents.FLAGS.GUILDS] }); +//init logs with a timestamp console.log(new Date().toISOString() + '\n\n'); +//get token if (!fs.existsSync('./token')) { fs.writeFileSync('./token', ''); console.error('Missing Discord Token, please enter the bot token into the token file'); process.exit(1); } const TOKEN = fs.readFileSync('./token').toString(); +//discord connections CLIENT.on('ready', client => { console.log(`Logged in as ${client.user.tag}`); client.guilds.fetch().then(guilds => (0, discord_1.registerCommands)(TOKEN, client.user.id, guilds.map(g => g.id))); }); -CLIENT.on('interactionCreate', (interaction) => __awaiter(void 0, void 0, void 0, function* () { +CLIENT.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; try { if (interaction.commandName === 'queue') - yield (0, queue_1.createQueue)(interaction); + await (0, queue_1.createQueue)(interaction); else if (interaction.commandName === 'join') - yield (0, queue_1.joinQueue)(interaction); + await (0, queue_1.joinQueue)(interaction); else if (interaction.commandName === 'leave') - yield (0, queue_1.leaveQueue)(interaction); + await (0, queue_1.leaveQueue)(interaction); else if (interaction.commandName === 'ready') - yield (0, queue_1.readyQueue)(interaction); + await (0, queue_1.readyQueue)(interaction); else if (interaction.commandName === 'queueinfo') - yield (0, queue_1.queueInfo)(interaction); + await (0, queue_1.queueInfo)(interaction); else if (interaction.commandName === 'elo') - yield (0, api_1.getPlayerInteraction)(interaction); + await (0, api_1.getPlayerInteraction)(interaction); } catch (e) { if (typeof e === 'string') { @@ -72,5 +66,5 @@ CLIENT.on('interactionCreate', (interaction) => __awaiter(void 0, void 0, void 0 else console.error(e); } -})); +}); CLIENT.login(TOKEN); diff --git a/dist/queue.js b/dist/queue.js index 408631b..a183b01 100644 --- a/dist/queue.js +++ b/dist/queue.js @@ -1,13 +1,4 @@ "use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; Object.defineProperty(exports, "__esModule", { value: true }); exports.queueInfo = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = void 0; const discord_js_1 = require("discord.js"); @@ -43,20 +34,18 @@ function getInfo(interaction) { * @param interaction * @throws string message if it cannot be created */ -function createQueue(interaction) { - return __awaiter(this, void 0, void 0, function* () { - 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 - ], - initiator: member, - teamsize: teamsize - }); - yield interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`); +async function createQueue(interaction) { + 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 + ], + initiator: member, + teamsize: teamsize }); + await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`); } exports.createQueue = createQueue; /** @@ -64,15 +53,13 @@ exports.createQueue = createQueue; * @param interaction * @throws string message if it cannot be joined */ -function joinQueue(interaction) { - return __awaiter(this, void 0, void 0, function* () { - 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(interaction.channelId, info); - yield interaction.reply('Joined the queue'); - }); +async function joinQueue(interaction) { + 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(interaction.channelId, info); + await interaction.reply('Joined the queue'); } exports.joinQueue = joinQueue; /** @@ -80,15 +67,13 @@ exports.joinQueue = joinQueue; * @param interaction * @throws string message if it cannot be left */ -function leaveQueue(interaction) { - return __awaiter(this, void 0, void 0, function* () { - 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(interaction.channelId, info); - yield interaction.reply('Left the queue'); - }); +async function leaveQueue(interaction) { + 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(interaction.channelId, info); + await interaction.reply('Left the queue'); } exports.leaveQueue = leaveQueue; /** @@ -96,29 +81,27 @@ exports.leaveQueue = leaveQueue; * @param interaction * @throws string message if it cannot be readied */ -function readyQueue(interaction) { - return __awaiter(this, void 0, void 0, function* () { - 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}`]; - team.forEach(m => str.push(` ${m.user.tag}`)); - teamsStr.push(str.join('\n')); - }); - yield interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```'); +async function readyQueue(interaction) { + 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}`]; + team.forEach(m => str.push(` ${m.user.tag}`)); + teamsStr.push(str.join('\n')); }); + await interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```'); } exports.readyQueue = readyQueue; /** @@ -126,14 +109,12 @@ exports.readyQueue = readyQueue; * @param interaction * @throws string message if it cannot be read */ -function queueInfo(interaction) { - return __awaiter(this, void 0, void 0, function* () { - let info = getInfo(interaction); - yield interaction.reply('```' + ` +async function queueInfo(interaction) { + let info = getInfo(interaction); + await interaction.reply('```' + ` players: ${info.players.map(p => p.user.tag).join('\n ')} initiator: ${info.initiator.user.tag} teamsize: ${info.teamsize} ` + '```'); - }); } exports.queueInfo = queueInfo; diff --git a/tsconfig.json b/tsconfig.json index bc3961d..1e9efb6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es2016", + "target": "es2020", "module": "commonjs", "rootDir": "./src", "outDir": "./dist",