Browse Source

Clean up code

Formatted code with correct spacing, removed some comments, and fixed
the random L on the ISS marker
pull/1/head
Abheek Dhawan 4 years ago
parent
commit
8a81879f46
  1. 157
      bot/awesomescibo.mjs

157
bot/awesomescibo.mjs

@ -6,20 +6,17 @@ const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION']
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import * as fs from 'fs'; import * as fs from 'fs';
/*const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
const fetch = require('node-fetch');
const fs = require('fs');*/
var hits = 0; var hits = 0;
if (fs.existsSync('numhits.txt')) { if (fs.existsSync('numhits.txt')) {
fs.readFile('numhits.txt', 'utf8', function(err, data){ fs.readFile('numhits.txt', 'utf8', function (err, data) {
hits = data; hits = data;
if (err) { if (err) {
console.log(err); console.log(err);
} }
}); });
} else {
fs.writeFile('numhits.txt', hits, (error) => { if (error) { console.log(error); } });
} }
@ -33,10 +30,10 @@ client.on('guildCreate', guild => {
}); });
client.on('message', async message => { client.on('message', async message => {
if (message.content.toLowerCase() === "do be hits") { if (message.content.toLowerCase() === "do be hits") {
message.channel.send(hits); message.channel.send(hits);
fs.writeFile('numhits.txt', hits, (error) => { if (error) { console.log(error); } }); fs.writeFile('numhits.txt', hits, (error) => { if (error) { console.log(error); } });
} }
if (message.content.toLowerCase() === ("do be helping")) { if (message.content.toLowerCase() === ("do be helping")) {
message.channel.send(new Discord.MessageEmbed().setTitle("Help").setDescription("`do be helping`: display this help message\n`do be roundgen html`: send a round to the channel\n`do be roundgen html dm`: dm a round to you\n`do be roundgen pdf`: send a pdf round to the channel\n`do be roundgen pdf dm`: dm a pdf round to you\n`do be scoring`: start a scoring session\n > `do be scoring (a/b)(4/10)`: add points to Team A or Team B\n > `do be scoring stop`: end scoring session and post final points\n`do be hits`: send the number of rounds generated\n`do be servers`: send the number of servers this bot is a part of\nSource Code: https://github.com/ADawesomeguy/AwesomeSciBo")); message.channel.send(new Discord.MessageEmbed().setTitle("Help").setDescription("`do be helping`: display this help message\n`do be roundgen html`: send a round to the channel\n`do be roundgen html dm`: dm a round to you\n`do be roundgen pdf`: send a pdf round to the channel\n`do be roundgen pdf dm`: dm a pdf round to you\n`do be scoring`: start a scoring session\n > `do be scoring (a/b)(4/10)`: add points to Team A or Team B\n > `do be scoring stop`: end scoring session and post final points\n`do be hits`: send the number of rounds generated\n`do be servers`: send the number of servers this bot is a part of\nSource Code: https://github.com/ADawesomeguy/AwesomeSciBo"));
@ -65,13 +62,13 @@ client.on('message', async message => {
bonus_question = data.question.bonus_question; bonus_question = data.question.bonus_question;
bonus_answer = data.question.bonus_answer; bonus_answer = data.question.bonus_answer;
bonus_format = data.question.bonus_format; bonus_format = data.question.bonus_format;
htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>"; htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>";
htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>"; htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent = htmlContent.replace(/\n/g, "<br>");
fs.appendFile('round.html', htmlContent, (error) => { if (error) { console.log(error); } }); fs.appendFile('round.html', htmlContent, (error) => { if (error) { console.log(error); } });
}); });
} }
generatingMsg.delete( {timeout: 1000} ); generatingMsg.delete({ timeout: 1000 });
setTimeout(function () { person.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.html")); }, 1000); setTimeout(function () { person.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.html")); }, 1000);
hits++; hits++;
} }
@ -98,17 +95,17 @@ client.on('message', async message => {
bonus_question = data.question.bonus_question; bonus_question = data.question.bonus_question;
bonus_answer = data.question.bonus_answer; bonus_answer = data.question.bonus_answer;
bonus_format = data.question.bonus_format; bonus_format = data.question.bonus_format;
htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>"; htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>";
htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>"; htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent = htmlContent.replace(/\n/g, "<br>");
fs.appendFile('round.html', htmlContent, (error) => { if (error) { console.log(error); } }); fs.appendFile('round.html', htmlContent, (error) => { if (error) { console.log(error); } });
}); });
} }
generatingMsg.delete( {timeout: 1000} ); generatingMsg.delete({ timeout: 1000 });
setTimeout(function () { message.channel.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.html")); }, 1000); setTimeout(function () { message.channel.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.html")); }, 1000);
hits++; hits++;
} }
if (message.content.toLowerCase() === ("do be roundgen pdf dm")) { if (message.content.toLowerCase() === ("do be roundgen pdf dm")) {
fs.writeFile('index.html', "<h1>Here's your round!</h1>", (error) => { if (error) { console.log(error); } }); fs.writeFile('index.html', "<h1>Here's your round!</h1>", (error) => { if (error) { console.log(error); } });
var person = client.users.cache.get(message.author.id); var person = client.users.cache.get(message.author.id);
var i; var i;
@ -132,14 +129,14 @@ client.on('message', async message => {
bonus_question = data.question.bonus_question; bonus_question = data.question.bonus_question;
bonus_answer = data.question.bonus_answer; bonus_answer = data.question.bonus_answer;
bonus_format = data.question.bonus_format; bonus_format = data.question.bonus_format;
htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>"; htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>";
htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>"; htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent = htmlContent.replace(/\n/g, "<br>");
fs.appendFile('index.html', htmlContent, (error) => { if (error) { console.log(error); } }); fs.appendFile('index.html', htmlContent, (error) => { if (error) { console.log(error); } });
}); });
} }
generatingMsg.delete( {timeout: 1000} ); generatingMsg.delete({ timeout: 1000 });
var output = execSync("curl --request POST --url http://localhost:3136/convert/html --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf", { encoding: 'utf-8' }); var output = execSync("curl --request POST --url http://localhost:3136/convert/html --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf", { encoding: 'utf-8' });
//console.log(output); //console.log(output);
setTimeout(function () { person.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.pdf")); }, 1000); setTimeout(function () { person.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.pdf")); }, 1000);
hits++; hits++;
@ -168,74 +165,74 @@ client.on('message', async message => {
bonus_question = data.question.bonus_question; bonus_question = data.question.bonus_question;
bonus_answer = data.question.bonus_answer; bonus_answer = data.question.bonus_answer;
bonus_format = data.question.bonus_format; bonus_format = data.question.bonus_format;
htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>"; htmlContent = `<br><br>${i}. Tossup\n<br><br>` + `<strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br><br>";
htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>"; htmlContent += "<br><br>Bonus\n<br><br>" + `<strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent = htmlContent.replace(/\n/g, "<br>");
fs.appendFile('index.html', htmlContent, (error) => { if (error) { console.log(error); } }); fs.appendFile('index.html', htmlContent, (error) => { if (error) { console.log(error); } });
}); });
} }
generatingMsg.delete( {timeout: 1000} ); generatingMsg.delete({ timeout: 1000 });
var output = execSync("curl --request POST --url http://localhost:3136/convert/html --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf", { encoding: 'utf-8' }); var output = execSync("curl --request POST --url http://localhost:3136/convert/html --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf", { encoding: 'utf-8' });
//console.log(output); //console.log(output);
message.channel.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.pdf")); message.channel.send(new Discord.MessageEmbed().setTitle("Here's your round!").attachFiles("round.pdf"));
hits++; hits++;
} }
if (message.content.toLowerCase() === "do be scoring") { if (message.content.toLowerCase() === "do be scoring") {
var scoreA = 0; var scoreA = 0;
var scoreB = 0; var scoreB = 0;
let scoreboard = await message.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`) let scoreboard = await message.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`)
.then((scoreboard) => { .then((scoreboard) => {
const filter = m => m.content.includes('do be'); const filter = m => m.content.includes('do be');
const collector = message.channel.createMessageCollector(filter, { time: 1500000 }); const collector = message.channel.createMessageCollector(filter, { time: 1500000 });
collector.on('collect', m => { collector.on('collect', m => {
if (m.content.toLowerCase() === "do be scoring a 4") { if (m.content.toLowerCase() === "do be scoring a 4") {
m.delete( { timeout: 1000 } ); m.delete({ timeout: 1000 });
scoreA += 4; scoreA += 4;
scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`); scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`);
} else if (m.content.toLowerCase() === "do be scoring a 10") { } else if (m.content.toLowerCase() === "do be scoring a 10") {
m.delete( { timeout: 1000 } ); m.delete({ timeout: 1000 });
scoreA += 10; scoreA += 10;
scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`); scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`);
} else if (m.content.toLowerCase() === "do be scoring b 4") { } else if (m.content.toLowerCase() === "do be scoring b 4") {
m.delete( { timeout: 1000 } ); m.delete({ timeout: 1000 });
scoreB += 4; scoreB += 4;
scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`); scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`);
} else if (m.content.toLowerCase() === "do be scoring b 10") { } else if (m.content.toLowerCase() === "do be scoring b 10") {
m.delete( { timeout: 1000 } ); m.delete({ timeout: 1000 });
scoreB += 10; scoreB += 10;
scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`); scoreboard.channel.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`);
} else if (m.content === "do be scoring stop") { } else if (m.content === "do be scoring stop") {
m.delete( { timeout: 1000 } ); m.delete({ timeout: 1000 });
scoreboard.delete( { timeout: 1000} ); scoreboard.delete({ timeout: 1000 });
m.channel.send(`**FINAL SCORE:**\nTeam A: ${scoreA}\nTeam B: ${scoreB}`); m.channel.send(`**FINAL SCORE:**\nTeam A: ${scoreA}\nTeam B: ${scoreB}`);
collector.stop(); collector.stop();
} }
}); });
}) })
} }
if (message.content.toLowerCase() === "do be happy") { if (message.content.toLowerCase() === "do be happy") {
message.channel.send(new Discord.MessageEmbed().setTitle(`Don't Worry Be Happy!`).setImage("https://media.giphy.com/media/7OKC8ZpTT0PVm/giphy.gif").setURL("https://youtu.be/d-diB65scQU")); message.channel.send(new Discord.MessageEmbed().setTitle(`Don't Worry Be Happy!`).setImage("https://media.giphy.com/media/7OKC8ZpTT0PVm/giphy.gif").setURL("https://youtu.be/d-diB65scQU"));
} }
if (message.content.toLowerCase() === "do be servers") { if (message.content.toLowerCase() === "do be servers") {
message.channel.send(client.guilds.cache.size); message.channel.send(client.guilds.cache.size);
} }
if (message.content.toLowerCase() === "do be iss") { if (message.content.toLowerCase() === "do be iss") {
await fetch("http://api.open-notify.org/iss-now.json") await fetch("http://api.open-notify.org/iss-now.json")
.then(request => request.json()) .then(request => request.json())
.then(data => { .then(data => {
//var astros = ''; //var astros = '';
//for (let i = 0; i < data.people.length; i++) { //for (let i = 0; i < data.people.length; i++) {
// console.log(data.people[i]); // console.log(data.people[i]);
// astros += `${data.people[i]}\n`; // astros += `${data.people[i]}\n`;
//} //}
message.channel.send(new Discord.MessageEmbed().setTitle("The current location of the ISS!").setImage(`https://api.mapbox.com/styles/v1/mapbox/light-v10/static/pin-s-l+000(${data.iss_position.longitude},${data.iss_position.latitude})/-87.0186,20,1/1000x1000?access_token=pk.eyJ1IjoiYWRhd2Vzb21lZ3V5IiwiYSI6ImNrbGpuaWdrYzJ0bGYydXBja2xsNmd2YTcifQ.Ude0UFOf9lFcQ-3BANWY5A`).setURL('https://spotthestation.nasa.gov/tracking_map.cfm')/*.addFields( message.channel.send(new Discord.MessageEmbed().setTitle("The current location of the ISS!").setImage(`https://api.mapbox.com/styles/v1/mapbox/light-v10/static/pin-s+000(${data.iss_position.longitude},${data.iss_position.latitude})/-87.0186,20,1/1000x1000?access_token=pk.eyJ1IjoiYWRhd2Vzb21lZ3V5IiwiYSI6ImNrbGpuaWdrYzJ0bGYydXBja2xsNmd2YTcifQ.Ude0UFOf9lFcQ-3BANWY5A`).setURL('https://spotthestation.nasa.gov/tracking_map.cfm')/*.addFields(
{ name: 'Current inhabitants', value: `${astros}` } { name: 'Current inhabitants', value: `${astros}` }
)*/); )*/);
}); });
} }
}); });
client.login(process.argv[2]).catch(error => console.log(error)); client.login(process.argv[2]).catch(error => console.log(error));

Loading…
Cancel
Save