Mostly cleaning and added the JSON to Map to Print function

This commit is contained in:
ION606
2022-07-16 19:26:06 +03:00
parent b9c4caa608
commit 34bbd823ab
13 changed files with 737 additions and 410 deletions
+1
View File
@@ -22,6 +22,7 @@ async function welcome(member, welcomechannel, welcomemessage = null, welcomeban
// const background = new CanvasImport.Image();
// background.src = arrayBufferToBuffer(data);
// This uses the canvas dimensions to stretch the image onto the entire canvas
// context.drawImage(background, 0, 0, canvas.width, canvas.height);
+38 -30
View File
@@ -11,39 +11,47 @@ module.exports = {
name += args[i] + " ";
i++;
}
}
} else { name = args[0]; }
if (args[args.length - 1] != args[args.length - 1] != '~fancy' && args[args.length - 1] != '~summary' && args[args.length - 1] != '~stats') { args.push('~stats'); }
scraper.getInfoFromName(name).then((data) => {
if (args[args.length - 1] == '~stats') {
const newEmbed = new Discord.MessageEmbed()
.setColor('#002eff')
.setTitle(data.title)
//.setURL('https://discordjs.guide/popular-topics/embeds.html#embed-preview')
//.setDescription('My professional resume')
.setImage(data.picture)
.addFields(
{name: 'Genres:', value: data.genres.join(", ")},
{name: 'Score:', value: data.score},
{name: 'Episode:', value: data.episodes}
).setURL(data.trailer);
message.channel.send({ embeds: [newEmbed] });
} else if (args[args.length - 1] == '~fancy') {
let temp = `The ${data.genres.join(", ")} anime _${data.title}_ first aired on ${data.premiered}`;
if (data.aired) { temp += `. This anime ran for ${data.aired} for a total of ${data.episodes} episodes.`}
else { temp += ` and is still airing with ${data.episodes} so far!`}
try {
if (args[args.length - 1] == '~stats') {
const newEmbed = new Discord.MessageEmbed()
.setColor('#002eff')
.setTitle(data.title)
//.setURL('https://discordjs.guide/popular-topics/embeds.html#embed-preview')
//.setDescription('My professional resume')
.setImage(data.picture)
.addFields(
{name: 'Genres:', value: data.genres.join(", ")},
{name: 'Score:', value: data.score},
{name: 'Episode:', value: data.episodes}
).setURL(data.trailer);
message.channel.send({ embeds: [newEmbed] });
} else if (args[args.length - 1] == '~fancy') {
let temp = `The ${data.genres.join(", ")} anime _${data.title}_ first aired on ${data.premiered}`;
if (data.aired) { temp += `. This anime ran for ${data.aired} for a total of ${data.episodes} episodes.`}
else { temp += ` and is still airing with ${data.episodes} so far!`}
temp += ` This anime has a score of ${data.score} and is ${data.popularity} on MyAnimeList!\n`;
temp += `You can see a trailer for ${data.title} here: ${data.trailer}`;
temp += `\n\n(to see a summary of the anime, use '${bot.prefix}asearch <anime name> ~summary')`;
message.channel.send({ embeds: [new Discord.MessageEmbed().setImage(data.picture)]});
message.channel.send(temp);
} else if (args[args.length - 1] == '~summary') {
let temp = data.synopsis;
message.channel.send(temp);
} else {
message.reply(`Unknown command, try using the format '${bot.prefix}asearch <anime name> [~stats or ~fancy or ~summary]`);
temp += ` This anime has a score of ${data.score} and is ${data.popularity} on MyAnimeList!\n`;
temp += `You can see a trailer for ${data.title} here: ${data.trailer}`;
temp += `\n\n(to see a summary of the anime, use '${bot.prefix}asearch <anime name> ~summary')`;
message.channel.send({ embeds: [new Discord.MessageEmbed().setImage(data.picture)]});
message.channel.send(temp);
} else if (args[args.length - 1] == '~summary') {
let temp = data.synopsis;
message.channel.send(temp);
} else {
message.reply(`Unknown command, try using the format '${bot.prefix}asearch <anime name> [~stats or ~fancy or ~summary]`);
}
} catch (err) {
if (err.message.indexOf('MessageEmbed field values must be non-empty strings') != -1) {
message.reply(`Insufficient information on website!\nThe page can be found here: ${data.url}`);
}
}
});
+5 -4
View File
@@ -1,6 +1,7 @@
const { MongoClient, ServerApiVersion } = require('mongodb');
const { createSubscriptionManual } = require('./premium/stripe.js');
const { pause_start_stop, playNext, showQueue } = require('./misc/playAudio.js');
// const { RSSInteractionHandler } = require('./premium/rssFeed.js');
async function handle_interaction(interaction, mongouri, turnManager, bot, STATE, items, xp_collection) {
@@ -69,7 +70,7 @@ async function handle_interaction(interaction, mongouri, turnManager, bot, STATE
pause_start_stop(interaction, bot);
}
} //else ifs here
} //Button else ifs here
});
client.close();
@@ -129,9 +130,9 @@ async function handle_interaction(interaction, mongouri, turnManager, bot, STATE
//Handle the interaction here
}
}
//menu else ifs here
} /*else if (interaction.customId.indexOf('RSS') != -1) {
RSSInteractionHandler(bot, interaction);
}*/ //menu else ifs here
} //other selection types here
}
+2 -1
View File
@@ -211,7 +211,8 @@ function fromMessage(bot, command, msg) {
const message = msg.channel.messages.cache.get(data[2]);
// console.log(message);
var em = message.embeds[0];
var em;
if (message.embeds) { em = message.embeds[0]; }
var rows;
if (command == 'stop') {
+4 -1
View File
@@ -12,13 +12,16 @@ module.exports ={
const html = response.data;
const $ = cheerio.load(html);
//lyrics = $('.para_row').text();
const haste = await hastebin(html, { extension: "txt" });
message.channel.send(haste);
message.channel.send(haste);
// console.log(lyrics);
})
.catch(function(err) {
if (err.message.indexOf('The "url" argument must be of type string') != -1) {
message.reply("The URL should be a string!");
} else if (err.code == 'ERR_BAD_REQUEST') {
message.reply("404 link not valid!")
} else {
message.reply("Oops! There's been an error");
}
+74
View File
@@ -0,0 +1,74 @@
/**
* @param {JSON} inp
* @returns {Map<String, Map>}
*/
function jsonToMapRecursive(inp) {
if (typeof(inp) != 'object') {
return inp;
}
let m2 = new Map();
Object.entries(inp).forEach((key) => { m2.set(key[0], jsonToMapRecursive(inp[key[0]])); });
return m2;
}
/**
*
* @param {Map} inp
* @param {int} layer
* @returns The map in string format
*
* @example
* {"key1": "val1", "key2": "key3": {"key4": "Val4"}} ==>>
* `
* |-- key1
* | |-- val1
* |
* |-- key2
* | |-- val3
* | | |-- key4
* | | | |--val4
* `
*/
function mapToTableRecursive(inp, layer = 1) {
var temp = '';
if (typeof(inp) != 'object') {
// return `?[${inp}]`;
return '';
}
Array.from(inp.keys()).forEach((key) => {
var keyTemp = ('| ').repeat(layer);
temp += `${keyTemp}- - ${key}\n`.replaceAll(' - -', '- -');
temp += mapToTable(inp.get(key), layer + 1);
});
temp += ('| ').repeat(layer - 1) + '\n';
if (layer == 1) {
var links = new Array();
//Post-processing
var l = temp.split('\n')
l = l.filter((entry, ind) => {
return entry.trim() == '|' || !((/[^A-Za-z0-9 ]+$/).test(entry.trim()) && (/[^A-Za-z0-9 ]+$/).test(l[ind + 1].trim()));
});
temp = l.join('\n')
//Get the links
Array.from(inp.keys()).forEach((key) => {
links.push(key);
});
return [temp, links];
}
return temp;
}