]> git.aero2k.de Git - urlbot-v3.git/commitdiff
poll: add prefixes
authorThorsten <mail@aero2k.de>
Sun, 8 Sep 2024 11:15:12 +0000 (13:15 +0200)
committerThorsten <mail@aero2k.de>
Sun, 8 Sep 2024 11:15:12 +0000 (13:15 +0200)
src/distbot/plugins/votepoll.py

index 0111cf332e13dbe349c6631592bc8f38597dcd9b..9768df353389f2ff2230eb301a3fea6952f810f6 100644 (file)
@@ -84,6 +84,15 @@ class Poll:
     def to_json(self):
         return json.dumps(self, default=lambda o: o.__dict__)
 
+    def option_listing(self, with_votes=False):
+        lines = []
+        for i, option in enumerate(self.options):
+            line = f" * {chr(65 + i)} - {option}"
+            if with_votes:
+                line += f": {self.get_votes(option)}"
+            lines.append(line)
+        return "\n".join(lines)
+
     def status_report(self):
         # Calculate winner and format result message
         winner = "It's a tie!"
@@ -99,8 +108,7 @@ class Poll:
             winner = f"**Winners:** {', '.join(winners)}"
 
         status_message = f"**Poll status:**\n"
-        for option in self.options:
-            status_message += f"  * {option}: {self.get_votes(option)}\n"
+        status_message += self.option_listing(with_votes=True)
         status_message += (f"\n{winner}" if self.due() else f"\nEnding at: {self.end_date}")
         return status_message
 
@@ -177,11 +185,11 @@ class VotePoll(Worker):
             # setup timeout to disable poll and present results
             bot_nick = conf_get("bot_nickname")
 
-            option_listing = "\n".join(" * {} - {}".format(chr(65 + i), option) for i, option in enumerate(options))
+
             poll_message = (f"**New Vote:** {sender} started a vote! "
                             f"Vote within {vote_duration}s "
                             f"(reply with '{bot_nick}: vote A/B/foo' within {vote_duration}s)"
-                            f"\nOptions are:\n") + option_listing
+                            f"\nOptions are:\n") + poll.option_listing()
             return Action(
                 msg=poll_message,
                 event=Action(time=now + vote_duration, command="nick.pollstatus",