From 33739621f592d2ae5884f8ed206a6a5d3b41ce78 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Wed, 28 Sep 2016 18:52:33 -0500 Subject: [PATCH] Updated for new saving method --- bot.py | 93 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/bot.py b/bot.py index a4f7910..9cdbb9d 100644 --- a/bot.py +++ b/bot.py @@ -42,44 +42,42 @@ logging.basicConfig(level=logging.INFO, filename='bonfire.log') async def on_ready(): # Change the status upon connection to the default status await bot.change_status(discord.Game(name=config.default_status, type=0)) - channel_id = await config.get_content('restart_server') - channel_id = channel_id or 0 if not hasattr(bot, 'uptime'): bot.uptime = pendulum.utcnow() - # Just in case the bot was restarted while someone was battling, clear it so they do not get stuck - await config.save_content('battling', {}) - # Check if the bot was restarted, if so send a message to the channel the bot was restarted from - if channel_id != 0: - destination = discord.utils.find(lambda m: m.id == channel_id, bot.get_all_channels()) - await bot.send_message(destination, "I have just finished restarting!") - await config.save_content('restart_server', 0) - @bot.event async def on_member_join(member): - notifications = await config.get_content('user_notifications') - server_notifications = notifications.get(member.server.id) - - # By default, notifications should be off unless explicitly turned on - if not server_notifications: + r_filter = {'server_id': member.server.id} + notifications = await config.get_content('user_notifications', r_filter) + try: + channel_id = notifications[0]['channel_id'] + except TypeError: return - channel = discord.utils.get(member.server.channels, id=server_notifications) + # By default, notifications should be off unless explicitly turned on + if not channel_id: + return + + channel = discord.utils.get(member.server.channels, id=notifications) await bot.send_message(channel, "Welcome to the '{0.server.name}' server {0.mention}!".format(member)) @bot.event async def on_member_remove(member): - notifications = await config.get_content('user_notifications') - server_notifications = notifications.get(member.server.id) - - # By default, notifications should be off unless explicitly turned on - if not server_notifications: + r_filter = {'server_id': member.server.id} + notifications = await config.get_content('user_notifications', r_filter) + try: + channel_id = notifications[0]['channel_id'] + except TypeError: return - channel = discord.utils.get(member.server.channels, id=server_notifications) + # By default, notifications should be off unless explicitly turned on + if not channel_id: + return + + channel = discord.utils.get(member.server.channels, id=channel_id) await bot.send_message(channel, "{0} has left the server, I hope it wasn't because of something I said :c".format( member.display_name)) @@ -94,42 +92,41 @@ async def on_message(message): @bot.event async def on_command_completion(command, ctx): - # There's no reason to continue waiting for this to complete, so lets immediately lanch this in a new future + # There's no reason to continue waiting for this to complete, so lets immediately launch this in a new future bot.loop.create_task(process_command(command, ctx)) async def process_command(command, ctx): # This try catch is only here while this is first being implemented # It will be removed once I ensure this is working correctly - try: - author = ctx.message.author - server = ctx.message.server + author = ctx.message.author + server = ctx.message.server - total_command_usage = await config.get_content('command_usage') - command_usage = total_command_usage.get(command.qualified_name, {}) - # Add one to the total usage for this command, basing it off 0 to start with (obviously) - total_usage = command_usage.get('total_usage', 0) + 1 - command_usage['total_usage'] = total_usage + r_filter = {'command': command.qualified_name} + command_usage = await config.get_content('command_usage', r_filter) + if command_usage is None: + command_usage = {} + else: + command_usage = command_usage[0] + # Add one to the total usage for this command, basing it off 0 to start with (obviously) + total_usage = command_usage.get('total_usage', 0) + 1 + command_usage['total_usage'] = total_usage - # Add one to the author's usage for this command - total_member_usage = command_usage.get('member_usage', {}) - member_usage = total_member_usage.get(author.id, 0) + 1 - total_member_usage[author.id] = member_usage - command_usage['member_usage'] = total_member_usage + # Add one to the author's usage for this command + total_member_usage = command_usage.get('member_usage', {}) + member_usage = total_member_usage.get(author.id, 0) + 1 + total_member_usage[author.id] = member_usage + command_usage['member_usage'] = total_member_usage - # Add one to the server's usage for this command - total_server_usage = command_usage.get('server_usage', {}) - server_usage = total_server_usage.get(server.id, 0) + 1 - total_server_usage[server.id] = server_usage - command_usage['server_usage'] = total_server_usage + # Add one to the server's usage for this command + total_server_usage = command_usage.get('server_usage', {}) + server_usage = total_server_usage.get(server.id, 0) + 1 + total_server_usage[server.id] = server_usage + command_usage['server_usage'] = total_server_usage - # Save all the changes - total_command_usage[command.qualified_name] = command_usage - await config.save_content('command_usage', total_command_usage) - except Exception as error: - with open("error_log", 'a') as f: - traceback.print_tb(error.__traceback__, file=f) - print('{0.__class__.__name__}: {0}'.format(error), file=f) + # Save all the changes + if not await config.update_content('command_usage', command_usage, r_filter): + await config.add_content('command_usage', command_usage, r_filter) @bot.event