diff --git a/src/bot/genericBot.js b/src/bot/genericBot.js index 1c3a4c2..25e8b63 100644 --- a/src/bot/genericBot.js +++ b/src/bot/genericBot.js @@ -28,16 +28,16 @@ module.exports = class GenericBot { this.logger.info("MessageId is " + msg.id); if (messageHandler.isMention(this.botConfig.name)) { this.logger.info("message is a mention, gonna reply to it"); - messageHandler.getTextToRespond().then(async text => { - this.#getModifiedText(text).then(async modifiedText => { - const comment = await this.streamHandler.getComment(msg.id); - const replySuccessful = await new CommentHandler(comment, this.logger).reply(modifiedText); - if (replySuccessful) { - this.logger.info("marking message as read"); - messageHandler.markMessageAsRead(); - } - }).catch(this.logger.error); - }).catch(this.logger.error); + const comment = await this.streamHandler.getComment(msg.id); + const commentHandler = new CommentHandler(comment, this.logger); + messageHandler.getTextToRespond() + .then(text => this.#getModifiedText(text)) + .then(modifiedText => commentHandler.reply(modifiedText)) + .then(reply => { + this.logger.info("Text of reply was: " + reply); + messageHandler.markMessageAsRead(); + }) + .catch(this.logger.error); } }); } @@ -46,19 +46,20 @@ module.exports = class GenericBot { this.streamHandler.postStream(async (post) => { const postHandler = new PostHandler(post, this.logger, this.botConfig.respondToID, this.streamHandler); postHandler.logPost(); - postHandler.shouldReplyTo(this.botConfig.name).then(async (comment) => { - this.logger.info("Post should be replied to."); - this.#getModifiedText(postHandler.getText()).then(modifiedText => { + postHandler.shouldReply(this.botConfig.name) + .then(comment => { const commentHandler = new CommentHandler(comment, this.logger); - commentHandler.reply(modifiedText); - }).catch(this.logger.error); - }).catch(this.logger.info); + this.logger.info("Post should be replied to."); + this.#getModifiedText(postHandler.getText()) + .then(modifiedText => commentHandler.reply(modifiedText)) + .then(reply => this.logger.info("Text of reply was" + reply)) + .catch(this.logger.error); + }).catch(this.logger.info); }); } async #getModifiedText(text) { return new Promise((resolve, reject) => { - const url = this.botConfig.restURL; const textObject = { text: text, diff --git a/src/bot/handlers/commentHandler.js b/src/bot/handlers/commentHandler.js index 795b25d..9da7417 100644 --- a/src/bot/handlers/commentHandler.js +++ b/src/bot/handlers/commentHandler.js @@ -7,20 +7,17 @@ module.exports = class CommentHandler { } async reply(text) { - if (debug) { - this.logger.info("Not replying because you are developing"); - return false; - } - let result = false; - this.logger.info("Replying now"); - this.comment - .reply(text) - .then(() => { - this.logger.info("Text of reply was: " + text); - result = true; - }) - .catch(this.logger.error); - return result; - } + return new Promise((resolve, reject) => { + if (debug) { + reject("Not replying because you are developing"); + return; + } + this.logger.info("Replying now"); + this.comment + .reply(text) + .then(() => resolve(text)) + .catch(reject); + }); + } } \ No newline at end of file diff --git a/src/bot/handlers/messageHandler.js b/src/bot/handlers/messageHandler.js index af66fb5..98bc2a7 100644 --- a/src/bot/handlers/messageHandler.js +++ b/src/bot/handlers/messageHandler.js @@ -26,6 +26,7 @@ module.exports = class MessageHandler { markMessageAsRead() { this.streamHandler.markMessagesAsRead([this.message]); + this.logger.info("message with id " + this.message.id + " was mark as read"); } } \ No newline at end of file diff --git a/src/bot/handlers/postHandler.js b/src/bot/handlers/postHandler.js index 2945418..ed1241e 100644 --- a/src/bot/handlers/postHandler.js +++ b/src/bot/handlers/postHandler.js @@ -10,7 +10,7 @@ module.exports = class PostHandler { this.streamHandler = streamHandler; } - async shouldReplyTo(botName, tries = 1) { + async shouldReply(botName, tries = 1) { return new Promise((resolve, reject) => { this.#findCommentToRespondTo().then(comment => { comment.expandReplies().then(c => { @@ -25,7 +25,7 @@ module.exports = class PostHandler { this.logger.info("this was try number: " + tries) await sleep(60000); await this.#renewPost(); - return resolve(this.shouldReplyTo(botName, tries + 1)); + return resolve(this.shouldReply(botName, tries + 1)); } else { return reject("max amount of tries (" + maxAmountOfTries + ") reached") }