refactor to more promise-style

This commit is contained in:
2022-03-05 22:33:19 +01:00
parent 6ffce4b848
commit 7ceb58e903
4 changed files with 33 additions and 34 deletions
+18 -17
View File
@@ -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,
+12 -15
View File
@@ -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);
});
}
}
+1
View File
@@ -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");
}
}
+2 -2
View File
@@ -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")
}