Permalink
Browse files

Framework for the bot.

  • Loading branch information...
roncli committed Dec 29, 2016
1 parent d00ee8d commit 881f4d1d23d5243b7b10af92ada00f46fdd36172
Showing with 220 additions and 6 deletions.
  1. +65 −0 database.js
  2. +151 −0 fusion.js
  3. +2 −4 index.js
  4. +2 −2 package.json
@@ -0,0 +1,65 @@
var settings = require("./settings"),
sql = require("mssql"),
Database = {};
Database.query = (sqlStr, params, callback) => {
"use strict";
if (!callback) {
callback = params;
params = {};
}
var conn = new sql.Connection(settings.database, (err) => {
var paramKeys = Object.keys(params),
ps;
if (err) {
callback(err);
return;
}
ps = new sql.PreparedStatement(conn);
paramKeys.forEach((key) => {
ps.input(key, params[key].type);
});
ps.multiple = true;
ps.prepare(sqlStr, (err) => {
if (err) {
callback(err);
return;
}
ps.execute(paramKeys.reduce((acc, key) => {
acc[key] = params[key].value;
return acc;
}, {}), (err, data) => {
if (err) {
callback(err);
return;
}
ps.unprepare((err) => {
if (err) {
callback(err);
return;
}
callback(null, data);
});
});
});
});
};
Database.TYPES = sql.TYPES;
Object.keys(sql.TYPES).forEach((key) => {
"use strict";
Database[key] = sql.TYPES[key];
Database[key.toUpperCase()] = sql.TYPES[key];
});
module.exports = Database;
151 fusion.js
@@ -0,0 +1,151 @@
var pjson = require("./package.json"),
settings = require("./settings"),
db = require("./database"),
messageParse = /^!([^ ]+)(?: +(.+[^ ]))? *$/,
Fusion = {},
tmiCooldown = {},
tmi, discord, obsDescent, generalChannel;
Fusion.start = (_tmi, _discord) => {
"use strict";
tmi = _tmi;
discord = _discord;
var startup = () => {
var readied = false,
tmiConnect = () => {
console.log("Connecting to IRC...");
tmi.connect().then(() => {
console.log("Connected. Startup complete.");
}).catch((err) => {
console.log("Error connecting, will retry.");
console.log(err);
});
},
discordConnect = () => {
console.log("Connecting to Discord...");
discord.login(settings.discord.token).catch((err) => {
if (err) {
console.log(err);
discord.destroy().then(discordConnect).catch(discordConnect);
}
console.log("Connected.");
});
};
console.log("Starting up...");
// Get players from database.
// Setup events.
tmi.on("disconnected", (message) => {
console.log("DISCONNECTED", message);
tmi.disconnect().then(() => {
tmiConnect();
}).catch(() => {
tmiConnect();
});
});
tmi.on("message", (channel, userstate, text, self) => {
if (!self && channel === "#roncli") {
Fusion.tmiMessage(userstate["display-name"], text);
}
});
discord.on("ready", () => {
console.log("Discord ready.");
obsDescent = discord.guilds.find("name", "The Observatory");
generalChannel = obsDescent.channels.find("name", "general");
if (!readied) {
readied = true;
// Connect to IRC.
tmiConnect();
}
});
discord.on("message", (message) => {
if (message.guild && message.guild.name === "The Observatory") {
Fusion.discordMessage(message.author.username, message.author, message.content);
}
});
};
startup();
};
Fusion.tmiQueue = (message) => {
"use strict";
tmi.say("roncli", message);
};
Fusion.discordQueue = (message, channel) => {
"use strict";
if (!channel) {
channel = generalChannel;
}
channel.sendMessage(message);
};
Fusion.tmiMessage = (from, text) => {
"use strict";
var matches = messageParse.exec(text);
if (matches) {
if (Fusion.tmiMessages[matches[1]]) {
Fusion.tmiMessages[matches[1]].call(this, from, matches[2]);
}
}
};
Fusion.tmiMessages = {
version: (from, message) => {
"use strict";
if (tmiCooldown.version > new Date()) {
return;
}
Fusion.tmiQueue("FusionBot by roncli, Version " + pjson.version);
tmiCooldown.version = new Date(new Date().getTime() + 60000);
},
discord: (from, message) => {
"use strict";
if (tmiCooldown.discord > new Date()) {
return;
}
Fusion.tmiQueue("TODO: Include information about how to connect to Discord!");
tmiCooldown.discord = new Date(new Date().getTime() + 60000);
},
website: (from, message) => {
"use strict";
if (tmiCooldown.website > new Date()) {
return;
}
Fusion.tmiQueue("Visit The Observatory on the web at http://roncli.com/gaming/the-observatory!");
tmiCooldown.discord = new Date(new Date().getTime() + 60000);
},
};
module.exports = Fusion;
@@ -1,10 +1,8 @@
var Discord = require("discord.js"),
Tmi = require("tmi.js"),
TwitchApi = require("twitch-api"),
settings = require("./settings"),
fusion = require("./fusion"),
tmi = new Tmi.client(settings.tmi),
discord = new Discord.Client(settings.discord.options),
twitch = new TwitchApi(settings.twitch);
discord = new Discord.Client(settings.discord.options);
fusion.start(tmi, discord, twitch);
fusion.start(tmi, discord);
@@ -19,9 +19,9 @@
},
"dependencies": {
"discord.js": "10.0.1",
"glicko2": "0.8.4",
"mssql": "3.3.0",
"tmi.js": "1.1.2",
"twitch-api": "0.4.5"
"tmi.js": "1.1.2"
},
"engines": {
"node": ">=6.0.0"

0 comments on commit 881f4d1

Please sign in to comment.