Made bot modular

This commit is contained in:
cameron 2018-06-13 02:31:48 -04:00
parent e85d40cedc
commit 66535142b2
6 changed files with 1419 additions and 209 deletions

View File

@ -1,10 +0,0 @@
module.exports.matches = [ { "!8ball" } ];
function basicRoute(str)
{
if(str.startsWith("!8ball"))
{
}
}

View File

@ -0,0 +1,43 @@
class Eightball {
constructor() {
this.matches = [ "8ball" ];
}
magic8ball()
{
const answers = [
'It is certain',
'It is decidedly so',
'Without a doubt',
'Yes definitely',
'You may rely on it',
'As I see it, yes',
'Most likely',
'Outlook good',
'Yes',
'Signs point to yes',
'Reply hazy try again',
'Ask again later',
'Better not tell you now',
'Cannot predict now',
'Concentrate and ask again',
"Don't count on it",
'My reply is no',
'My sources say no',
'Outlook not so good',
'Very doubtful' ];
let selection = Math.floor(Math.random() * (answers.length));
return answers[selection];
}
async action(msg) {
if(msg.content.startsWith('!8ball')) {
msg.reply(magic8ball());
}
}
}
module.exports = Eightball;

View File

@ -0,0 +1,60 @@
const Discord = require("discord.js");
const Gamedig = require('gamedig');
const fs = require('fs');
class GameServer {
constructor() {
this.matches = [ "gm", "mc" ];
}
async action(msg) {
if(msg.content.startsWith('!mc')) {
Gamedig.query({
type: 'minecraftping',
host: 'americastrong.jumpingcrab.com'
}).then((state) => {
let str = "MINECRAFT SERVER INFO\n";
console.log(state);
str += "Server Name: " + state.raw.description + "\n";
str += "Players: " + state.players.length + "/" + state.maxplayers + "\n";
str += "Currently Online: "
for (let i = 0; i < state.players.length; i++) {
str += state.players[i].name + ", ";
}
str = str.slice(0, -2);
str += "\n";
msg.reply(str);
}).catch((error) => {
msg.reply("Server is offline!");
});
} else if(msg.content.startsWith('!gm') {
try {
let gameinfo = await Gamedig.query({type: 'garrysmod',
host: 'construct420.game.nfoservers.com'});
let str = "GARRY'S MOD SERVER INFO\n";
str += "Server name: " + gameinfo.name + "\n";
str += "Current Map: " + gameinfo.map + "\n";
str += "Players: " + gameinfo.players.length + "/" + gameinfo.maxplayers + "\n";
str += "Currently Online: "
for (let i = 0; i < gameinfo.players.length; i++) {
str += gameinfo.players[i].name + ", ";
}
str = str.slice(0, -2);
str += "\n";
str += "Server IP: 192.223.30.22\n"
msg.reply(str);
} catch (err) {
console.log(err);
msg.reply("Server is offline!");
}
}
}
}
module.exports = GameServer;

View File

@ -0,0 +1,36 @@
const Discord = require("discord.js");
const fs = require('fs');
class Pepe {
constructor() {
this.matches = [ "pepe" ];
this.loadPepes();
}
loadPepes()
{
let pepes = fs.readFileSync('pepes.txt').toString();
this.pepesList = pepes.split("\n");
}
//returns a dank pepe
pepe()
{
let dankPepe = new Discord.RichEmbed();
let selection = Math.floor(Math.random() * (this.pepesList.length));
dankPepe.setImage(this.pepesList[selection]);
return dankPepe;
}
async action(msg) {
let chan = msg.channel;
let pepeImg = this.pepe();
chan.send(pepeImg);
}
}
module.exports = Pepe;

1251
bot-modules/pepe/pepes.txt Normal file

File diff suppressed because it is too large Load Diff

228
main.js
View File

@ -9,217 +9,47 @@ const glob = require('glob');
const path = require('path'); const path = require('path');
const twitter = require('twitter'); const twitter = require('twitter');
var pepesList = [];
var markov = require('markov'); var markov = require('markov');
var m = markov(2); var m = markov(2);
const twitterClient = new twitter({ let bot_modules = [];
consumer_key: 'd1QtB67GohqcCKSD0GdYF1nkh', let triggers = [];
consumer_secret: 'tYkCxEKIahoykUSS71DgWjK7O1IX5BgGLxOSFE7v0uXLGGviCS',
access_token_key: '384120881-IETrcVQHmwWec3jtifY7FhyODRY944ufeEh2pwcT',
access_token_secret: 'EtnE7bggaKOrk0pfxkAkQEb7iLe8yyqYm8M7GulAbbwTt'
});
async function main() {
console.log("Loading modules...");
let modules = glob.sync('./bot-modules/**/module.js');
for(const mod of modules) {
const parts = mod.split('/');
const mod_name = parts[parts.length-2];
const mod_cls = require(mod);
const mod_obj = new mod_cls();
bot_modules[mod_name] = mod_obj;
async function generateTwitterEmbed(tweetData) for(const t of mod_obj.matches) {
{ triggers[t] = mod_name;
} }
async function getLatestPost(user) console.log(`## Loaded ${mod_name}`);
{
//GET https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
try {
let post = await twitterClient.get('statuses/user_timeline', { count: 1, screen_name: user });
console.log(post[0].id_str);
return post[0].id_str;
} catch(e) {
return 'reee';
} }
}
//returns a word definition from urban dictionary console.log(triggers);
async function ud(word)
{
let page = await rp('http://api.urbandictionary.com/v0/define?term=' + word, { json: true });
if ( page.result_type === 'exact' )
{
let defStr = "";
defStr += page.list[0].word + "\n\n" + page.list[0].definition + "\n\n" + "Example: " + page.list[0].example;
console.log("defStr: " + defStr);
return defStr;
} else {
return "No results.";
}
}
function initMarkov() client.login('MzYyNzU1MzE3MTczNTgzODcy.DK3R1A.8M22ZkNaaoHy-ifBQwVivSU7svY');
{ client.on('ready', () => {
//get stuff from pol console.log(`Logged into Discord as ${client.user.tag}`);
request('https://a.4cdn.org/pol/catalog.json', { json: true }, });
(err, res, body) => { client.on('message', async (msg) => {
if (err) { return console.log(err); } for(const t in triggers) {
for(let i = 0; i < body.length; i++) console.log(t);
{ if (msg.content.startsWith(`!${t}`)) {
for(let j = 0; j < body[i].threads.length; j++) console.log(`+ Bot module ${triggers[t]} triggered.`);
{ await bot_modules[triggers[t]].action(msg);
var post = striptags(body[i].threads[j].com); break;
post = post.toLowerCase();
m.seed(post);
if(body[i].threads[j].last_replies == undefined) { continue; }
for(let k = 0; k < body[i].threads[j].last_replies.length; k++)
{
var post2 = striptags(body[i].threads[j].last_replies[k].com);
post2 = post2.toLowerCase();
m.seed(post2);
}
} }
} }
}); });
} }
function loadPepes() main();
{
fs.readFile('pepes.txt', 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
pepesList = data.split("\n");
});
}
//returns a dank pepe
function pepe()
{
let dankPepe = new Discord.RichEmbed();
let selection = Math.floor(Math.random() * (pepesList.length));
dankPepe.setImage(pepesList[selection]);
return dankPepe;
}
function magic8ball()
{
const answers = [
'It is certain',
'It is decidedly so',
'Without a doubt',
'Yes definitely',
'You may rely on it',
'As I see it, yes',
'Most likely',
'Outlook good',
'Yes',
'Signs point to yes',
'Reply hazy try again',
'Ask again later',
'Better not tell you now',
'Cannot predict now',
'Concentrate and ask again',
"Don't count on it",
'My reply is no',
'My sources say no',
'Outlook not so good',
'Very doubtful' ];
let selection = Math.floor(Math.random() * (answers.length));
return answers[selection];
}
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
loadPepes();
});
client.on('message', async (msg) => {
if (msg.content === '!ping') {
msg.reply('Pong!');
} else if (msg.content === '!8ball') {
msg.reply(magic8ball());
} else if (msg.content.startsWith('<@' + client.user.id)) {
if (msg.content.trim().endsWith('?')) {
msg.reply(magic8ball());
}
} else if (msg.content === '!mc') {
Gamedig.query({
type: 'minecraftping',
host: 'americastrong.jumpingcrab.com'
}).then((state) => {
let str = "MINECRAFT SERVER INFO\n";
console.log(state);
str += "Server Name: " + state.raw.description + "\n";
str += "Players: " + state.players.length + "/" + state.maxplayers + "\n";
str += "Currently Online: "
for (let i = 0; i < state.players.length; i++) {
str += state.players[i].name + ", ";
}
str = str.slice(0, -2);
str += "\n";
msg.reply(str);
}).catch((error) => {
msg.reply("Server is offline!");
});
} else if (msg.content === '!gm') {
try {
let gameinfo = await Gamedig.query({type: 'garrysmod',
host: 'construct420.game.nfoservers.com'});
let str = "GARRY'S MOD SERVER INFO\n";
str += "Server name: " + gameinfo.name + "\n";
str += "Current Map: " + gameinfo.map + "\n";
str += "Players: " + gameinfo.players.length + "/" + gameinfo.maxplayers + "\n";
str += "Currently Online: "
for (let i = 0; i < gameinfo.players.length; i++) {
str += gameinfo.players[i].name + ", ";
}
str = str.slice(0, -2);
str += "\n";
str += "Server IP: 192.223.30.22\n"
msg.reply(str);
} catch (err) {
console.log(err);
msg.reply("Server is offline!");
}
} else if (msg.content === '!pepe') {
let chan = msg.channel;
let pepeImg = pepe();
chan.send(pepeImg);
} else if (msg.content.startsWith('!ud')) {
let chan = msg.channel;
let words = msg.content.substring(msg.content.indexOf(" ") + 1);
let def = await ud(words);
console.log(def);
chan.send("```\n" + def + "```");
} else if (msg.content.startsWith('!markov')) {
let msgArray = msg.content.split(" ");
console.log(msgArray);
let chan = msg.channel;
if(msgArray[1] && msgArray[2] != null) {
let msg2 = m.forward([msgArray[1] + '_' + msgArray[2]], 20).join(' ');
chan.send(msgArray[1] + ' ' + msgArray[2] + ' ' + msg2);
} else {
msg.reply("Usage: !markov <word1> <word2>");
}
} else if (msg.content.startsWith('!bsp')) {
let chan = msg.channel;
let postId = await getLatestPost('dot_bsp');
chan.send("https://www.twitter.com/dot_bsp/status/" + postId);
}
});
client.login('MzYyNzU1MzE3MTczNTgzODcy.DK3R1A.8M22ZkNaaoHy-ifBQwVivSU7svY');