Skip to content

Commit

Permalink
Reduce code density / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wrussell1999 committed May 2, 2019
1 parent 531813a commit 5f68a8e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def check_time_periods():
logger.info("1 hour left for voting")
embed = await embed_scoreboard(daily_data['submissions'], daily_data['votes'], "1 hour left for voting", "There's still time to vote! Here are the current scores")
embed.set_footer(text="Remember to vote for your submission to be valid!")
await vote_reminder()
await private_vote_reminder()
await voting_channel.send(embed=embed)
elif hour == 12 and minute == 00 and len(daily_data['submissions']) > 1 and len(daily_data['voters']) > 0:
await results_period(voting_channel, submission_channel, results_channel)
Expand Down
26 changes: 13 additions & 13 deletions results.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ async def results_period(voting_channel, submission_channel, results_channel):
await bot.change_presence(status=discord.Status.idle, activity=activity)
winner_message = await get_winner(results_channel)
logger.debug(winner_message)

embed = discord.Embed(title="RESULTS", description="", colour=0xff0000)
embed.set_author(name=winner_message)
embed.set_footer(text=random.choice(quotes['rude']))
sorted_submissions_dict = sort_submissions()

sorted_submissions_dict = sort_submissions()
for index, val in enumerate(sorted_submissions_dict['votes']):
votes_str = "Votes: "
votes_str = votes_str + str(val)
user_obj = bot.get_guild(config.config['server_id']).get_member(sorted_submissions_dict['submissions'][index])
embed.add_field(name=user_obj.nick, value=votes_str, inline=False)
votes = "Votes: " + str(val)
user = bot.get_guild(config.config['server_id']).get_member(sorted_submissions_dict['submissions'][index])
embed.add_field(name=user.nick, value=votes, inline=False)
await results_channel.send(embed=embed)

reset_daily_data()
data_dict_to_json()
await channel_permissions(False, False, voting_channel, submission_channel)

async def get_winner(results_channel):
for index, value in enumerate(daily_data['submissions']):
if not check_winner_vote(value):
disqualify_winner(value, index)
disqualify_winner(value, index, results_channel)

if len(daily_data['submissions']) == 0:
embed = discord.Embed(
Expand All @@ -46,10 +47,9 @@ async def get_winner(results_channel):

max_vote = max(daily_data['votes'])
winner_message = "Winner: "
winner_indexes = [i for i, j in enumerate(
max_index = [i for i, j in enumerate(
daily_data['votes']) if j == max_vote]

if len(winner_indexes) > 1:
if len(max_index) > 1:
winner_message = "Winners: "

for index, value in enumerate(daily_data['submissions']):
Expand All @@ -68,11 +68,11 @@ def check_winner_vote(winner):
logger.warning("Winner disqualified")
return False

async def disqualify_winner(winner, index):
async def disqualify_winner(winner, index, channel):
winner_message = "Winner disqualified: " + str(winner.nick)
embed = discord.Embed(title=winner_message, description="Winner did not vote, therefore their submission is invalid", colour=0xff0000)
await bot.get_channel(config.config['results_channel_id']).send(embed=embed)
logger.debug("New winner selected" + str(daily_data['submissions'][index]))
embed = discord.Embed(
title=winner_message, description="Winner did not vote, therefore their submission is invalid", colour=0xff0000)
await channel.send(embed=embed)
del daily_data['votes'][index]
del daily_data['submissions'][index]
await asyncio.sleep(5)
Expand Down
6 changes: 0 additions & 6 deletions scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ async def scoreboard(channel):
sorted_scoreboard_dict = sort_scoreboard()
embed = await embed_scoreboard(sorted_scoreboard_dict['users'], sorted_scoreboard_dict['scores'], "SCOREBOARD", "Scoreboard for this term")
await channel.send(embed=embed)

async def auto_scoreboard():
sorted_scoreboard_dict = sort_scoreboard()
embed = await embed_scoreboard(sorted_scoreboard_dict['users'], sorted_scoreboard_dict['scores'], "SCOREBOARD", "Scoreboard for this term")
await bot.get_channel(config.config['results_channel_id']).send(embed=embed)


@bot.command(description="Shows the overall score for the food flex")
async def score(ctx):
Expand Down
29 changes: 9 additions & 20 deletions voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,34 @@ async def voting_period(submission_channel, voting_channel):
data_dict_to_json()
await channel_permissions(False, True, submission_channel, voting_channel)

async def vote_reminder():
logger.debug("Reminding users who submitted")
async def private_vote_reminder():
for member_id in daily_data['submissions']:
if member_id in daily_data['voters']:
logger.debug(str(member_id) + " has voted - no reminder")
else:
if member_id not in daily_data['voters']:
user = bot.get_guild(config.config['server_id']).get_member(member_id)
await user.send("Remember to vote for your submission to be valid!!!")
logger.debug("Vote reminder sent for " + str(user.nick))

async def check_vote(message, voting_channel):
logger.info("Vote by: " + str(message.author.nick))
raw = str(message.clean_content)
vote = raw[0]
vote_index = ord(vote) - 65
vote_index = ord(message.clean_content[0]) - 65
logger.debug("vote_index: " + str(vote_index)) # this is an index
if vote_index < len(daily_data['submissions']) and vote_index >= 0: # Checks if it's in range
duplicate = check_duplicate(vote_index, voting_channel, message)
if (duplicate == True):
logger.info("Vote invalid")
elif duplicate == False:
if duplicate == False:
await is_valid(vote_index, voting_channel, message)

def check_duplicate(vote_index, voting_channel, message):
if check_self_vote(vote_index, voting_channel, message) == True:
return True
elif message.author.id in daily_data['voters']:
if check_self_vote(vote_index, voting_channel, message) == True or message.author.id in daily_data['voters']:
return True
else:
return False

def check_self_vote(vote_index, voting_channel, message):
if message.author.id in daily_data['submissions']:
voter_index = daily_data['submissions'].index(message.author.id)
else:
voter_index = -1
if voter_index == vote_index:
logger.warn("Invalid vote: same submission")
return True
if voter_index == vote_index:
logger.warn("Invalid vote: same submission")
return True
else:
return False

Expand All @@ -81,4 +70,4 @@ async def voting(ctx):
if await bot.is_owner(ctx.author):
await voting_period(bot.get_channel(config.config['submission_channel_id']), bot.get_channel(config.config['voting_channel_id']))
logger.debug("Voting started manually")
await ctx.message.delete()
await ctx.message.delete()

0 comments on commit 5f68a8e

Please sign in to comment.