Skip to content

Commit 7faa31f

Browse files
committed
add polling tie detection
1 parent a249e87 commit 7faa31f

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

protobot.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#####################################
2828
#
2929
COMMAND_PREFIX = '!' #
30-
VERSION = "v0.5.0-alpha" #
30+
VERSION = "v0.5.1-alpha" #
3131
ACTIVITY = discord.Game("!help") #
3232
LOG_LEVEL = logging.INFO #
3333
#
@@ -392,10 +392,36 @@ async def count_poll_results(message_sent, poll_time_in_sec):
392392
if total_votes == 0:
393393
await message_sent.channel.send(f"Voting for \"{prompt}\" complete. Nobody voted!")
394394
else:
395-
# Display results
395+
# Find winner
396396
winner = max(results, key = results.get)
397-
winner_percentage = round((results[winner] / total_votes) * 100, 2)
398-
await message_sent.channel.send(f"Voting for \"{prompt}\" complete. {winner} is the winner with {results[winner]} ({winner_percentage}%) votes!")
397+
398+
# Check for ties
399+
tie = False
400+
for result in results.keys():
401+
if results[result] == results[winner]:
402+
tie = True
403+
404+
# If there's a tie, find out which results tied and make a string out of them, then print results
405+
if tie:
406+
tied_emojis = []
407+
tie_string = ""
408+
for result in results.keys():
409+
if results[result] == results[winner]:
410+
tied_emojis.append(result)
411+
num_ties = len(tied_emojis)
412+
for i in range(num_ties - 1):
413+
tie_string += f"{tied_emojis[i]}, "
414+
tie_string += f"and {tied_emojis[i + 1]}"
415+
# Remove comma for only 2 results
416+
if num_ties == 2:
417+
tie_string = tie_string.replace(',', '')
418+
419+
await message_sent.channel.send(f"Voting for \"{prompt}\" complete. There was a {num_ties}-way tie between {tie_string} with {results[winner]} ({winner_percentage}%) votes each.")
420+
421+
# Otherwise, show winner
422+
else:
423+
winner_percentage = round((results[winner] / total_votes) * 100, 2)
424+
await message_sent.channel.send(f"Voting for \"{prompt}\" complete. {winner} is the winner with {results[winner]} ({winner_percentage}%) votes!")
399425

400426
await count_poll_results(message_sent, poll_time_in_sec)
401427

@@ -405,7 +431,6 @@ def run_once_every_day():
405431
Runs a block of code every day.
406432
"""
407433

408-
logging.info("Running nightly operations.")
409434
pass
410435

411436

0 commit comments

Comments
 (0)