2021-06-28 03:20:21 +00:00
|
|
|
const Discord = require('discord.js');
|
|
|
|
const fs = require('fs');
|
|
|
|
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
2021-07-19 18:54:08 +00:00
|
|
|
const config = require('./config.json');
|
2021-07-22 02:00:17 +00:00
|
|
|
const pg = require('pg');
|
|
|
|
let dbConnected = false;
|
|
|
|
const db = new pg.Client({
|
|
|
|
connectionString: process.env.DATABASE_URL,
|
|
|
|
ssl: {
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.connect();
|
2021-06-28 03:20:21 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2021-07-19 18:54:08 +00:00
|
|
|
setValidExtensions(client) {
|
|
|
|
for (const entry of client.commands.map(command => command.name)) {
|
|
|
|
config.validExtensions.push(entry);
|
|
|
|
}
|
|
|
|
},
|
2021-06-28 03:20:21 +00:00
|
|
|
getCommandFiles(client) {
|
2021-07-24 19:44:58 +00:00
|
|
|
if (!client.commands) client.commands = new Discord.Collection();
|
|
|
|
client.commands.clear();
|
2021-06-28 03:20:21 +00:00
|
|
|
for (const file of commandFiles) {
|
|
|
|
const command = require(`./commands/${file}`);
|
|
|
|
client.commands.set(command.name, command);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getGifFiles(client) {
|
2021-07-24 19:44:58 +00:00
|
|
|
if (!client.gifs) client.gifs = new Discord.Collection();
|
|
|
|
client.gifs.clear();
|
2021-07-22 02:00:17 +00:00
|
|
|
const query = "SELECT name, embed_url FROM gifs";
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.query(query)
|
|
|
|
.then(res => {
|
|
|
|
for (let row of res.rows) {
|
|
|
|
const gif = {
|
|
|
|
name: row.name,
|
|
|
|
embed_url: row.embed_url
|
|
|
|
};
|
|
|
|
client.gifs.set(gif.name, gif);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getPotPhrases(client) {
|
2021-07-24 19:44:58 +00:00
|
|
|
if (!client.potphrases) client.potphrases = new Discord.Collection();
|
|
|
|
client.potphrases.clear();
|
2021-07-22 02:00:17 +00:00
|
|
|
const query = "SELECT id, content FROM potphrases";
|
|
|
|
db.query(query)
|
|
|
|
.then(res => {
|
|
|
|
for (let row of res.rows) {
|
|
|
|
const potphrase = {
|
|
|
|
id: row.id,
|
|
|
|
content: row.content
|
|
|
|
};
|
|
|
|
client.potphrases.set(potphrase.id, potphrase);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
2021-06-28 03:20:21 +00:00
|
|
|
},
|
2021-06-28 19:29:35 +00:00
|
|
|
getPastaFiles(client) {
|
2021-07-24 19:44:58 +00:00
|
|
|
if (!client.pastas) client.pastas = new Discord.Collection();
|
|
|
|
client.pastas.clear();
|
2021-07-22 02:00:17 +00:00
|
|
|
const query = "SELECT name, content FROM pastas";
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.query(query)
|
|
|
|
.then(res => {
|
|
|
|
for (let row of res.rows) {
|
|
|
|
const pasta = {
|
|
|
|
name: row.name,
|
|
|
|
content: row.content
|
|
|
|
};
|
|
|
|
client.pastas.set(pasta.name, pasta);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
|
|
|
});
|
|
|
|
|
2021-06-28 19:29:35 +00:00
|
|
|
},
|
2021-07-24 19:44:58 +00:00
|
|
|
reload(client) {
|
|
|
|
this.getCommandFiles(client);
|
|
|
|
this.getGifFiles(client);
|
|
|
|
this.getPastaFiles(client);
|
|
|
|
this.getPotPhrases(client);
|
|
|
|
},
|
2021-06-30 22:59:24 +00:00
|
|
|
getFileInfo(content) {
|
2021-07-22 02:00:17 +00:00
|
|
|
// Split the message content at the final instance of a period
|
2021-07-01 22:12:21 +00:00
|
|
|
const finalPeriod = content.lastIndexOf('.');
|
2021-06-30 22:59:24 +00:00
|
|
|
if (finalPeriod < 0) return false;
|
|
|
|
const extension = content.slice(finalPeriod).replace('.','').toLowerCase();
|
|
|
|
const filename = content.slice(0,finalPeriod).toLowerCase();
|
|
|
|
const file = {
|
|
|
|
'name': filename,
|
|
|
|
'extension': extension
|
|
|
|
};
|
2021-06-28 22:11:32 +00:00
|
|
|
return file;
|
2021-06-28 19:29:35 +00:00
|
|
|
},
|
2021-06-30 22:59:24 +00:00
|
|
|
extIsValid(extension) {
|
2021-07-01 22:12:21 +00:00
|
|
|
const extensions = require('./config.json').validExtensions;
|
|
|
|
return extensions.includes(extension);
|
2021-07-06 21:37:29 +00:00
|
|
|
},
|
2021-07-22 02:00:17 +00:00
|
|
|
cleanInput(string) {
|
|
|
|
return string.replace(/'/g, "''").replace(/\n/g, '\\n');
|
2021-07-06 21:37:29 +00:00
|
|
|
},
|
2021-07-14 00:52:54 +00:00
|
|
|
createGifEmbed(data, author, command) {
|
2021-07-06 21:37:29 +00:00
|
|
|
return new Discord.MessageEmbed()
|
2021-07-14 00:57:37 +00:00
|
|
|
.setAuthor(command)
|
2021-07-06 21:37:29 +00:00
|
|
|
.setImage(data.embed_url)
|
|
|
|
.setTimestamp()
|
2021-07-14 00:52:54 +00:00
|
|
|
.setFooter(`@${author.username}#${author.discriminator}`);
|
2021-07-10 03:24:13 +00:00
|
|
|
},
|
|
|
|
saveGif(message, name, embed_url) {
|
2021-07-22 02:40:36 +00:00
|
|
|
const gif = {
|
|
|
|
name: name,
|
|
|
|
embed_url: embed_url
|
|
|
|
};
|
|
|
|
message.client.gifs.set(gif.name, gif);
|
|
|
|
this.uploadGIF(name, embed_url);
|
2021-07-14 00:15:57 +00:00
|
|
|
},
|
2021-07-16 14:01:44 +00:00
|
|
|
savePasta(message, name, content) {
|
2021-07-22 02:40:36 +00:00
|
|
|
const pasta = {
|
|
|
|
name: name,
|
|
|
|
content: content
|
|
|
|
};
|
|
|
|
message.client.pastas.set(pasta.name, pasta);
|
|
|
|
|
|
|
|
const query = `INSERT INTO pastas (name, content) VALUES ('${name}','${content}')`;
|
|
|
|
db.query(query);
|
|
|
|
|
|
|
|
return "Success";
|
2021-07-16 14:01:44 +00:00
|
|
|
},
|
2021-07-14 00:52:54 +00:00
|
|
|
createAirportEmbed(data, author, command) {
|
2021-07-14 00:15:57 +00:00
|
|
|
const airport = data.airport[0];
|
|
|
|
return new Discord.MessageEmbed()
|
2021-07-14 00:52:54 +00:00
|
|
|
.setAuthor(command)
|
2021-07-14 00:15:57 +00:00
|
|
|
.setTitle(airport.airport_name)
|
|
|
|
.addFields(
|
|
|
|
{ name: 'Location', value: `${airport.city}, ${airport.state_abbrev}`, inline: true },
|
|
|
|
{ name: 'Coordinates', value: `${airport.latitude}, ${airport.longitude}`, inline: true },
|
|
|
|
{ name: 'Elevation', value: `${airport.elevation}ft`, inline: true },
|
|
|
|
{ name: 'More Information', value: airport.link_path }
|
|
|
|
)
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${author.username}#${author.discriminator}`);
|
2021-07-14 00:52:54 +00:00
|
|
|
},
|
|
|
|
createWeatherEmbed(data, author, command) {
|
|
|
|
const loc = data.location;
|
|
|
|
const weather = data.current;
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor(command)
|
|
|
|
.setTitle(`${loc.name}, ${loc.region}, ${loc.country} Weather`)
|
|
|
|
.setDescription(`The weather is currently ${weather.condition.text}`)
|
|
|
|
.addFields(
|
|
|
|
{ name: 'Temperature', value: `${weather.temp_f}°F (Feels like: ${weather.feelslike_f}°F)`, inline: true },
|
|
|
|
{ name: 'Winds', value: `${weather.wind_mph} ${weather.wind_dir}`, inline: true },
|
|
|
|
{ name: 'Pressure', value: `${weather.pressure_in}inHg`, inline: true },
|
|
|
|
{ name: 'Relative Humidity', value: `${weather.humidity}%`, inline: true }
|
|
|
|
)
|
2021-07-14 01:02:29 +00:00
|
|
|
.setThumbnail(`https:${weather.condition.icon}`)
|
2021-07-14 00:52:54 +00:00
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${author.username}#${author.discriminator}`);
|
2021-07-14 01:13:46 +00:00
|
|
|
},
|
|
|
|
createTextEmbed(data, author, command) {
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor(command)
|
2021-07-19 01:11:03 +00:00
|
|
|
.setDescription(data.content)
|
2021-07-14 01:13:46 +00:00
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${author.username}#${author.discriminator}`);
|
2021-07-14 01:55:14 +00:00
|
|
|
},
|
|
|
|
createStockEmbed(data, author, command) {
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor(command)
|
|
|
|
.setTitle()
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${author.username}#${author.discriminator}`);
|
2021-07-18 18:56:45 +00:00
|
|
|
},
|
2021-07-19 02:23:40 +00:00
|
|
|
createHelpEmbed(message) {
|
|
|
|
const { commands } = message.client;
|
|
|
|
let fields = [];
|
|
|
|
for (const entry of commands.map(command => [command.name, command.description, command.usage])) {
|
|
|
|
const name = entry[0];
|
|
|
|
const description = entry[1];
|
|
|
|
let usage;
|
|
|
|
if (entry[2] == undefined) {
|
|
|
|
usage = '';
|
|
|
|
} else {
|
|
|
|
usage = entry[2];
|
|
|
|
}
|
|
|
|
const excludeList = [
|
|
|
|
'kill',
|
|
|
|
'mapcommands',
|
|
|
|
'newgif',
|
|
|
|
'newpng',
|
|
|
|
'oldgif',
|
|
|
|
'strain',
|
|
|
|
'stonk',
|
|
|
|
'wrongbad'
|
|
|
|
];
|
|
|
|
if (excludeList.includes(name)) continue;
|
|
|
|
fields.push({
|
|
|
|
name: name,
|
|
|
|
value: `${description}\n**Usage:** \`${usage}.${name}\``
|
|
|
|
});
|
2021-07-18 18:56:45 +00:00
|
|
|
}
|
2021-07-19 02:23:40 +00:00
|
|
|
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor('NodBot Help')
|
|
|
|
.setDescription('All commands are provided as "file extensions" instead of prefixes to the message.')
|
|
|
|
.addFields(fields)
|
|
|
|
.setTimestamp();
|
2021-07-19 18:54:08 +00:00
|
|
|
},
|
|
|
|
createGIFList(message) {
|
|
|
|
let list = [];
|
|
|
|
const { gifs } = message.client;
|
|
|
|
for (const entry of gifs.map(gif => [gif.name])) {
|
|
|
|
list.push(entry[0] + '.gif');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor('NodBot GIF List')
|
|
|
|
.setTitle('List of Currently Saved GIFs')
|
|
|
|
.setDescription(list.join('\n'))
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${message.author.username}#${message.author.discriminator}`);
|
|
|
|
},
|
|
|
|
createPastaList(message) {
|
|
|
|
let list = [];
|
|
|
|
const { pastas } = message.client;
|
|
|
|
for (const entry of pastas.map(pasta => [pasta.name])) {
|
|
|
|
list.push(entry[0] + '.pasta');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Discord.MessageEmbed()
|
|
|
|
.setAuthor('NodBot Pasta List')
|
|
|
|
.setTitle('List of Currently Saved Copypastas')
|
|
|
|
.setDescription(list.join('\n'))
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter(`@${message.author.username}#${message.author.discriminator}`);
|
2021-07-22 02:00:17 +00:00
|
|
|
},
|
|
|
|
uploadGIF(name, embed_url) {
|
2021-07-22 02:40:36 +00:00
|
|
|
const query = `INSERT INTO gifs (name, embed_url) VALUES ('${name}','${embed_url}')`;
|
2021-07-22 02:00:17 +00:00
|
|
|
db.query(query)
|
|
|
|
.then()
|
|
|
|
.catch(e => console.error(e));
|
2021-07-24 19:44:58 +00:00
|
|
|
},
|
|
|
|
uploadPotPhrase(content) {
|
|
|
|
const query = `INSERT INTO potphrases (content) VALUES ('${content}')`;
|
|
|
|
db.query(query)
|
|
|
|
.then()
|
|
|
|
.catch(e => console.error(e));
|
2021-07-14 01:13:46 +00:00
|
|
|
}
|
2021-06-28 03:20:21 +00:00
|
|
|
}
|