Ejemplo n.º 1
0
    def step3(self, state, input, subreddit):
        """PotW Step 3 - Re-interpret promotions."""
        
        r = self.r
        while True:
            message = ""
            removeIdxs = []  # remove those only after doing the other changes so that the indices dont get mixed up
            for line in input:
                if not line.strip():
                    continue

                parts = line.split(")")
                num = 0
                try:
                    num = int(parts[0])
                except:
                    message += "* Couldn't understand entry: " + line + "\n"
                    continue

                num-=1 # CONVERT TO INTERNAL REPRESENTATION

                if num >= len(state["finalWinnerInfo"]) or num < 0:
                    message += "* The entry id has to be one of the numbers in the last message in: " + line + "\n"
                    continue
                if len(parts) < 2:
                    message += "* The entry here lacks a command verb: " + line + "\n"
                    continue
                # get the verb
                cmd = parts[1].lower().strip()
                if cmd.startswith("remove"):
                    removeIdxs.append(num)
                elif cmd.startswith("change "):
                    rankNameLower = cmd[len("change "):].lower().strip()
                    newRank = RankUtils.getRankByName(rankNameLower)
                    if newRank is None:
                        message += "Couldn't find the rank " + \
                            rankNameLower + " in [{}]".format(num)
                        continue
                    state["finalWinnerInfo"][num]["promoteTo"] = newRank
                    if state["finalWinnerInfo"][num]["promoteTo"] != state["finalWinnerInfo"][num]["currentRank"]:
                        state["finalWinnerInfo"][num]["willBePromoted"] = True
                    else:
                        state["finalWinnerInfo"][num]["willBePromoted"] = False
                    logging.info("Changed to newRank.")
                else:
                    message += "* Warning: Did not understand the command '{}'".format(cmd)

            removeIdxs = sorted(removeIdxs, reverse=True)
            for idx in removeIdxs:
                del state["finalWinnerInfo"][idx]
                logging.info("Removed {}".format(idx))

            message += "\n\n"
            message += formatFinalWinnerInfo(state["finalWinnerInfo"])
            message += "\n\n"
        
            message += InfoMessages.promotionsCorrected
            self.messageHandler.dispatchOutput(message)
            message = ""
            inpt = self.waitForNewInstructions()
            
            if inpt is not None and len(inpt) > 0 and inpt[0].lower().startswith("okay"):
                break
            input = inpt

        message="Notes (If there's anything in this section, please heed the message):"

        # get the nominations
        nominationsThread = state["nominationsThread"]
        nominations = []
        for comment in nominationsThread.comments:
            if not comment.is_root:
                message += "* Ignored [comment]({}) because it was not a root comment in the nominations thread.\n\n".format(comment.permalink)
                continue
            if comment.author is None or comment.author.name is None:
                message += "* Ignored deleted [comment]({}) in the nominations thread.\n\n".format(comment.permalink)
                continue
            if comment.banned_by is not None:
                message += "* Ignored removed [comment]({}) in the nominations thread.\n\n".format(comment.permalink)
                continue

            winnerInfo = RedditUtils.findFirstLinkedObject(comment.body, subreddit, r)
            if winnerInfo is None:
                message += "* Couldn't find the thread/comment referenced by the [nomination entry]({}). **Please handle this manually LATER. DON'T Forget.\n\n** ".format(
                    comment.permalink)
                continue

            if winnerInfo[2].author is None:
                message += "* The comment linked to by this [nomination entry]({}) appears to have been deleted. **Please handle this manually LATER. DON'T Forget.\n\n** ".format(
                    comment.permalink)
                continue

            # get the user from the nomination comment
            username = ""
            match = re.search(r'\/u\/(.*?)\s', comment.body, re.IGNORECASE)
            if match is None:
                message += ("* The user in [this]({}) nomination comment didn't specify a username. Better double-check"
                            + " that they at least got the permalink right and didn't accidentially link the thread. "
                            + "If they did mess up, remove the entry from the bots nominations in the next step and add it to the vote thread after the bot has finished. **DON'T Forget**\n\n** ").format(comment.permalink)
                username = winnerInfo[2].author.name
            else:
                username = match.group(1).strip()

            # ensure that they are both the same - reddit usernames are case
            # insensitive
            if username.lower() != winnerInfo[2].author.name.lower():
                message += "This [nomination entry]{}'s link doesn't match the user they nominated. **Please handle this manually LATER (after the bot has finished). DON'T Forget.\n\n** ".format(comment.permalink)
                continue

            nominations.append({"name": winnerInfo[2].author.name, "reason": winnerInfo[
                               0], "permalink": winnerInfo[1]})

        state["nominations"] = nominations
        state["messageCarryover"] = message
        
        return state