Refactored some variable names to be more readable
This commit is contained in:
parent
72d4db6e25
commit
d15047f34a
42
feed-bot.js
42
feed-bot.js
|
@ -10,53 +10,53 @@ var Log = require("./log.js"); //some very simple logging functions I made
|
||||||
var BotConfig = require("./bot-config.json"); //bot config file containing bot token
|
var BotConfig = require("./bot-config.json"); //bot config file containing bot token
|
||||||
var Config = require("./config.json"); //config file containing other settings
|
var Config = require("./config.json"); //config file containing other settings
|
||||||
|
|
||||||
var IS_FIRST_RUN = true;
|
var isFirstRun = true;
|
||||||
|
|
||||||
var Bot = {
|
var DiscordClient = {
|
||||||
bot: null,
|
bot: null,
|
||||||
startup: function () {
|
startup: function () {
|
||||||
//check if we can connect to discordapp.com to authenticate the bot
|
//check if we can connect to discordapp.com to authenticate the bot
|
||||||
Dns.resolve("discordapp.com", function (err) {
|
Dns.resolve("discordapp.com", function (err) {
|
||||||
if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet).", err);
|
if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot", err);
|
||||||
else {
|
else {
|
||||||
//if there was no error, go ahead and create and authenticate the bot
|
//if there was no error, go ahead and create and authenticate the bot
|
||||||
Bot.bot = new Discord.Client({
|
DiscordClient.bot = new Discord.Client({
|
||||||
token: BotConfig.token,
|
token: BotConfig.token,
|
||||||
autorun: true
|
autorun: true
|
||||||
});
|
});
|
||||||
|
|
||||||
//set up the bot's event handlers
|
//set up the bot's event handlers
|
||||||
Bot.bot.on("ready", Bot.onReady);
|
DiscordClient.bot.on("ready", DiscordClient.onReady);
|
||||||
Bot.bot.on("disconnect", Bot.onDisconnect);
|
DiscordClient.bot.on("disconnect", DiscordClient.onDisconnect);
|
||||||
Bot.bot.on("message", Bot.onMessage);
|
DiscordClient.bot.on("message", DiscordClient.onMessage);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onReady: function () {
|
onReady: function () {
|
||||||
if (IS_FIRST_RUN) {
|
if (isFirstRun) {
|
||||||
IS_FIRST_RUN = false;
|
isFirstRun = false;
|
||||||
|
|
||||||
Log.info("Registered bot " + Bot.bot.username + " - (" + Bot.bot.id + ")");
|
Log.info("Registered bot " + DiscordClient.bot.username + " - (" + DiscordClient.bot.id + ")");
|
||||||
Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds");
|
Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds");
|
||||||
|
|
||||||
//set up the timer to check the feed
|
//set up the timer to check the feed
|
||||||
setInterval(Feed.checkAndPost, Config.pollingInterval);
|
setInterval(Feed.checkAndPost, Config.pollingInterval);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Log.info("Bot reconnected!");
|
Log.info("DiscordClient reconnected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
//we need to check past messages for links on startup, but also on reconnect because we don't know what has happened during the downtime
|
//we need to check past messages for links on startup, but also on reconnect because we don't know what has happened during the downtime
|
||||||
Bot.checkPastMessagesForLinks();
|
DiscordClient.checkPastMessagesForLinks();
|
||||||
},
|
},
|
||||||
onDisconnect: function (err, code) {
|
onDisconnect: function (err, code) {
|
||||||
//do a bunch of logging
|
//do a bunch of logging
|
||||||
Log.event("Bot was disconnected! " + code ? code : "No disconnect code provided", "Discord.io");
|
Log.event("DiscordClient was disconnected! " + code ? code : "No disconnect code provided", "Discord.io");
|
||||||
if (err) Log.error("Bot disconnected!", err);
|
if (err) Log.error("DiscordClient disconnected!", err);
|
||||||
Log.info("Trying to reconnect bot");
|
Log.info("Trying to reconnect bot");
|
||||||
|
|
||||||
//then actually attempt to reconnect
|
//then actually attempt to reconnect
|
||||||
Bot.bot.connect();
|
DiscordClient.bot.connect();
|
||||||
},
|
},
|
||||||
onMessage: function (user, userID, channelID, message) {
|
onMessage: function (user, userID, channelID, message) {
|
||||||
//check if the message contains a link, in the right channel, and not the latest link from the rss feed
|
//check if the message contains a link, in the right channel, and not the latest link from the rss feed
|
||||||
|
@ -75,7 +75,7 @@ var Bot = {
|
||||||
Log.info("Attempting to check past " + limit + " messages for links");
|
Log.info("Attempting to check past " + limit + " messages for links");
|
||||||
|
|
||||||
//get the last however many messsages from our discord channel
|
//get the last however many messsages from our discord channel
|
||||||
Bot.bot.getMessages({
|
DiscordClient.bot.getMessages({
|
||||||
channelID: Config.channelID,
|
channelID: Config.channelID,
|
||||||
limit: limit
|
limit: limit
|
||||||
}, function (err, messages) {
|
}, function (err, messages) {
|
||||||
|
@ -160,20 +160,20 @@ var Links = {
|
||||||
Log.info("Attempting to post new link: " + latestLink);
|
Log.info("Attempting to post new link: " + latestLink);
|
||||||
|
|
||||||
//send a messsage containing the new feed link to our discord channel
|
//send a messsage containing the new feed link to our discord channel
|
||||||
Bot.bot.sendMessage({
|
DiscordClient.bot.sendMessage({
|
||||||
to: Config.channelID,
|
to: Config.channelID,
|
||||||
message: latestLink
|
message: latestLink
|
||||||
}, function (err, message) {
|
}, function (err, message) {
|
||||||
if (err) {
|
if (err) {
|
||||||
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
||||||
//if there is an error posting the message, check if it is because the bot isn't connected
|
//if there is an error posting the message, check if it is because the bot isn't connected
|
||||||
if (Bot.bot.connected)
|
if (DiscordClient.bot.connected)
|
||||||
Log.info("Connectivity seems fine - I have no idea why the message didn't post");
|
Log.info("Connectivity seems fine - I have no idea why the message didn't post");
|
||||||
else {
|
else {
|
||||||
Log.error("Bot appears to be disconnected! Attempting to reconnect...", err);
|
Log.error("DiscordClient appears to be disconnected! Attempting to reconnect...", err);
|
||||||
|
|
||||||
//attempt to reconnect
|
//attempt to reconnect
|
||||||
Bot.bot.connect();
|
DiscordClient.bot.connect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -204,5 +204,5 @@ var Feed = {
|
||||||
|
|
||||||
//IIFE to kickstart the bot when the app loads
|
//IIFE to kickstart the bot when the app loads
|
||||||
(function(){
|
(function(){
|
||||||
Bot.startup();
|
DiscordClient.startup();
|
||||||
})();
|
})();
|
Loading…
Reference in a new issue