async def election_cycle(): results = tally_election() general_chat = d_bot.guilds[0].get_channel(config.get('chat_id_general')) if len(results) > 0: await general_chat.send(tally_to_str(results)) role_el_presidente = d_bot.guilds[0].get_role( config.get('role_id_el_presidente')) top_candidate = results[0] winner = None tied = [] #check for ties for candidate in results: if candidate[1] == top_candidate[ 1]: ## if this candidate got the same number of votes as the winner tied.append(candidate) #if the president is in the tied group, they win tied_ids = [i[0] for i in tied] for m in role_el_presidente.members: if m.id in tied_ids: winner = m break if winner: # the current president was in the tied list.. we have our winner print(f'The current EL PRESIDENTE has won re-election') await general_chat.send(winner.mention + ' has won re-election!!!') else: # keep searching if len(tied) > 1: await general_chat.send( 'A tie has occurred! Picking a random EL PRESIDENTE') top_candidate = random.choice(tied) # chaos print(f'Top: {top_candidate}') winner = d_bot.guilds[0].get_member(top_candidate[0]) #remove current EL PRESIDENTE and check if the president is one of the tied memebers for m in role_el_presidente.members: # if the president is a tied member, they win regardless of random selection await m.remove_roles(role_el_presidente) print(f'{m.display_name} is removed from EL PRESIDENTE') await winner.add_roles(role_el_presidente) print(f'{winner.display_name} is added to EL PRESIDENTE') await general_chat.send(winner.mention + ' has been elected EL PRESIDENTE!!!') data.get()['history'].append(winner.id) data.save() else: await general_chat.send('No votes. EL PRESIDENTE remains.') clear_votes()
def clear_votes(): try: data.get()['current_election']['votes'] = {} data.save() except Exception as e: print('Clear votes error:') print(e)
def tally_election(): tally = {} for vote in data.get()['current_election']['votes'].values(): cast_for = vote['cast_for'] if cast_for in tally: tally[cast_for] += 1 else: tally[cast_for] = 1 return sorted(tally.items(), key=lambda kv: kv[1], reverse=True)
async def on_command_vote(ctx, cast_for: discord.Member = None): if ctx.channel.id == config.get('chat_id_bot_commands'): voter_id = str( ctx.author.id ) # this must be cast to a string. the one read in from json will be interpreted as a string and if this is a num then 1 person can have 2 votes cur = data.get()['current_election'] votes = cur['votes'] if cast_for: cast_for_id = cast_for.id if cur['running']: if voter_id in votes: await ctx.send('Vote updated!') else: await ctx.send('Vote cast!') print( f'({ctx.author.id}, {ctx.author.display_name}): vote for ({cast_for_ir}, {cast_for.display_name})' ) votes[voter_id] = {"cast_for": cast_for_id} data.save() else: await ctx.send('No election is currently running') else: if cur['running']: if voter_id in votes: await ctx.send( f'{ctx.author.mention}\'s current vote: {d_bot.guilds[0].get_member(votes[voter_id]["cast_for"]).display_name}' ) else: await ctx.send( f'{ctx.author.mention}, you have yet to vote for someone' ) data.save() else: await ctx.send('No election is currently running') else: await ctx.message.delete() await ctx.author.send( 'Please keep all commands to the "bot-commands" channel')
import data_handler a = {'name': 'Joe Doe', 'email': '*****@*****.**'} data_handler.store('redis', 'pickle', a, 'pswd') b = data_handler.get('redis', 'pickle', 'pswd') print(b) x = {'current_year': 2019, 'next_year': 2020} data_handler.store('file', 'json', x, 'test.txt') y = data_handler.get('file', 'json', 'test.txt') print(y)