DiscordClient.checkPastMessagesForLinks();//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
module.exports={
onReady:()=>{
Actions.checkPastMessagesForLinks();//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
//set the interval function to check the feed
intervalFunc=()=>{
Feed.check((err,articles)=>{
Links.validate(err,articles,DiscordClient.post);
Links.validate(err,articles,Actions.post);
});
};
},
onDisconnect:function(err,code){
Log.event("Bot was disconnected! "+(err?err:"")+(code?code:"No disconnect code provided.")+"\nClearing the feed timer and starting reconnect timer","Discord.io");
intervalFunc=DiscordClient.startup;//reassign the interval function to try restart the bot every 5 sec
if(err)Log.raw(err);//log the error if there is an error
if(err)Console.log(err);//log the error if there is an error
else{//else go ahead and confirm subscription
Log.event("Subscribed user "+(user?user+"("+userID+")":userID));
Console.info("Subscribed user "+(user?user+"("+userID+")":userID));
DiscordClient.bot.sendMessage({
bot.sendMessage({
to:channelID,
message:"You have successfully subscribed"
},(err,response)=>{setTimeout(()=>{DiscordClient.bot.deleteMessage({channelID:channelID,messageID:response.id});},Config.messageDeleteDelay);});//delete the subscription confirmation message after a delay
},(err,response)=>{setTimeout(()=>{bot.deleteMessage({channelID:channelID,messageID:response.id});},Config.messageDeleteDelay);});//delete the subscription confirmation message after a delay
if(err)Log.raw(err);//log the error if there is an error
if(err)Console.log(err);//log the error if there is an error
else{//else go ahead and confirm un-subscription
Log.event("Unsubscribed user "+(user?user+"("+userID+")":userID));
Console.info("Unsubscribed user "+(user?user+"("+userID+")":userID));
DiscordClient.bot.sendMessage({
bot.sendMessage({
to:channelID,
message:"You have successfully unsubscribed"
},(err,response)=>{setTimeout(()=>{DiscordClient.bot.deleteMessage({channelID:channelID,messageID:response.id});},Config.messageDeleteDelay);});//delete the un-subscription confirmation message after a delay
},(err,response)=>{setTimeout(()=>{bot.deleteMessage({channelID:channelID,messageID:response.id});},Config.messageDeleteDelay);});//delete the un-subscription confirmation message after a delay
}
});
},
@ -236,7 +205,7 @@ var Links = {
//store the new link if not stored already
if(!Links.isCached(link)){
Links.cached.push(link);
Log.info("Cached URL: "+link);
Console.info("Cached URL: "+link);
}
if(Links.cached.length>Config.numLinksToCache)Links.cached.shift();//get rid of the first array element if we have reached our cache limit
//make sure the latest link hasn't been posted already
if(Links.isCached(latestLink)){
Log.info("Didn't post new feed link because already detected as posted "+latestLink);
Console.info("Didn't post new feed link because already detected as posted "+latestLink);
}
else{
callback(latestLink);
@ -278,16 +247,10 @@ var Feed = {
urlObj:Url.parse(Config.feedUrl),
check:function(callback){
Dns.resolve(Feed.urlObj.host,function(err){//check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl)