async def process_submission(message): user_id = str(message.author.id) logger.debug(f'Processing submission, msg id {user_id}') if data.weekly_data != None and user_id in data.weekly_data: logger.info('Submission invalid') try: url = message.attachments[0].proxy_url except IndexError: url = '' loggger.warn('Submission does not have an image attached') data.weekly_data[user_id][u'image_url'] = url await message.channel.send(random.choice(static.quotes['rude'])) logger.info('Submission overwritten') else: try: url = message.attachments[0].proxy_url except IndexError: url = '' loggger.warn('Submission does not have an image attached') data.weekly_data[user_id] = { u'nick': message.author.display_name, u'submitted': True, u'image_url': url, u'voted': False, u'votes': 0 } await message.channel.send(random.choice(static.quotes['rude'])) logger.info('Submission valid') data.update_weekly_document()
async def on_ready(): global main_channel, leaderboard_channel, guild, admin_role logger.debug('Finding text channels...') builtins.main_channel = bot.get_channel(int(os.getenv('MAIN_CHANNEL_ID'))) builtins.leaderboard_channel = bot.get_channel(int(os.getenv('LEADERBOARD_CHANNEL_ID'))) try: builtins.leaderboard_message = await builtins.leaderboard_channel.fetch_message(int(os.getenv("LEADERBOARD_MESSAGE_ID"))) except: logger.warning("No leaderboard message") if "leaderboard_message_id" not in data.state: builtins.leaderboard_message = None else: builtins.leaderboard_message = await builtins.leaderboard_channel.fetch_message(data.state['leaderboard_message_id']) builtins.guild = bot.get_guild(int(os.getenv('SERVER_ID'))) builtins.admin_role = guild.get_role(int(os.getenv('ADMIN_ROLE_ID'))) logger.info('Food Flex is online!')
async def individual_vote_reminder(): for user in data.weekly_data: if data.weekly_data[user]['submitted'] and \ not data.weekly_data[user]['voted']: member = builtins.guild.get_member(int(user)) if member is None: logger.warn(f'Unable to get user object for \'{user}\', skipping their vote reminder') continue embed = discord.Embed(title=static.strings['voting_dm_reminder_title'], description=static.strings['voting_dm_reminder'], colour=0xff0000) embed.set_footer(text=static.strings['voting_dm_reminder_footer']) await member.send(embed=embed) logger.debug(f'Vote reminder sent for \'{member.display_name}\' ({member.id})')
async def submission_period(): logger.info('// Now in SUBMISSIONS period //') activity = discord.Activity( name=static.strings['submission_open_activity'], type=discord.ActivityType.watching) await bot.change_presence(status=discord.Status.online, activity=activity) embed = discord.Embed(title=static.strings['submission_open_title'], description=static.strings['submission_open'], colour=0xff0000) await main_channel.send(embed=embed) logger.debug('Creating new weekly document') week_number = datetime.datetime.now().isocalendar()[1] data.create_new_weekly_document(week_number) data.weekly_data = {} data.update_weekly_document() data.voting_map = {} data.update_voting_map()
def build_voting_map(): counter = 0 logger.debug('Building voting_map map...') # ensure voting_map is empty if len(voting_map) != 0: logger.warn('voting_map map has not been cleared, clearing now') voting_map.clear() # assign a letter to every person who has submitted for user_id in weekly_data: if weekly_data[user_id]['submitted']: assigned_letter = chr(ord('A') + counter) counter += 1 logger.debug( f'Assigned letter \'{assigned_letter}\' to user_id \'{user_id}\'' ) voting_map[assigned_letter] = user_id logger.info(f'Map built, {len(voting_map)} letter(s)') db.collection(u'weekly-data').document(u'voting-map').set(voting_map)
def load(): global quotes, strings logger.debug('Loading quotes & strings...') try: with open(QUOTES_PATH) as file: try: quotes = json.load(file) except json.decoder.JSONDecodeError: fatal(f'↳ Cannot parse {QUOTES_PATH}') except OSError: fatal(f'↳ Cannot open {QUOTES_PATH}') try: with open(STRINGS_PATH) as file: try: strings = json.load(file) except json.decoder.JSONDecodeError: fatal(f'↳ Cannot parse {STRINGS_PATH}') except OSError: fatal(f'↳ Cannot open {STRINGS_PATH}') logger.info('↳ Quotes & strings loaded')
async def voting_period(): logger.info('// Now in VOTING period //') logger.info('Creating voting key...') # we need to build voting_map map first data.build_voting_map() logger.debug('Creating (url, letter) pairs for submissions...') submissions = [] # create (image_url, letter) pair list for letter in data.voting_map: user_id = data.voting_map[letter] image_url = data.weekly_data[user_id]['image_url'] submissions.append((image_url, letter)) # sort submissions by alphabetical key submissions.sort(key=lambda tuple: tuple[1], reverse=False) logger.debug('Generating images...') # process images image_objects = [] for (image_url, letter) in submissions: buffer = images.process_image(image_url, letter) image_objects.append(discord.File(buffer, filename=f'{letter}.png')) # announce voting is open await main_channel.send(static.strings['voting_open_title']) logger.debug('Uploading images...') # upload images for image in image_objects: await main_channel.send(file=image) # remind people how to vote and change presence await main_channel.send(static.strings['voting_open_footer']) activity = discord.Activity(name='people vote on shit food', type=discord.ActivityType.watching) await bot.change_presence(status=discord.Status.online, activity=activity) logger.info('Done, voting key posted')
async def on_command_error(ctx, error): # CheckFailure is an auth error which we already log if not isinstance(error, discord.ext.commands.CheckFailure): logger.debug(f'error: {error}')