2016-07-08 01:05:42 +12:00
|
|
|
#!/usr/local/bin/python3.5
|
2016-07-09 11:26:43 +12:00
|
|
|
import discord
|
2016-07-19 07:57:23 +12:00
|
|
|
import traceback
|
2016-07-31 02:59:35 +12:00
|
|
|
import logging
|
|
|
|
import datetime
|
2016-08-07 10:20:16 +12:00
|
|
|
import pendulum
|
2016-08-12 14:13:02 +12:00
|
|
|
import os
|
2017-02-13 11:40:28 +13:00
|
|
|
import aiohttp
|
2016-08-12 14:13:02 +12:00
|
|
|
|
2016-08-26 16:56:16 +12:00
|
|
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
2016-07-09 11:26:43 +12:00
|
|
|
|
2016-08-27 01:23:02 +12:00
|
|
|
from discord.ext import commands
|
2017-02-26 10:45:59 +13:00
|
|
|
from cogs import utils
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2017-02-26 10:45:59 +13:00
|
|
|
opts = {'command_prefix': utils.command_prefix,
|
|
|
|
'description': utils.bot_description,
|
2016-08-31 07:16:24 +12:00
|
|
|
'pm_help': None,
|
2017-02-26 10:45:59 +13:00
|
|
|
'shard_count': utils.shard_count,
|
|
|
|
'shard_id': utils.shard_id,
|
2016-10-31 16:14:30 +13:00
|
|
|
'command_not_found': ''}
|
2016-08-31 07:51:04 +12:00
|
|
|
|
2016-08-31 07:16:24 +12:00
|
|
|
bot = commands.Bot(**opts)
|
2016-11-13 13:10:22 +13:00
|
|
|
logging.basicConfig(level=logging.WARNING, filename='bonfire.log')
|
2016-07-08 10:10:24 +12:00
|
|
|
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2016-07-08 10:10:24 +12:00
|
|
|
# Change the status upon connection to the default status
|
2017-02-26 10:45:59 +13:00
|
|
|
await bot.change_presence(game=discord.Game(name=utils.default_status, type=0))
|
2016-08-11 12:46:23 +12:00
|
|
|
|
2016-09-01 09:19:39 +12:00
|
|
|
if not hasattr(bot, 'uptime'):
|
|
|
|
bot.uptime = pendulum.utcnow()
|
2017-02-26 10:45:59 +13:00
|
|
|
await utils.db_check()
|
2016-08-04 13:31:38 +12:00
|
|
|
|
2016-07-10 04:40:27 +12:00
|
|
|
@bot.event
|
|
|
|
async def on_message(message):
|
|
|
|
if message.author.bot:
|
|
|
|
return
|
|
|
|
await bot.process_commands(message)
|
2016-07-08 10:10:24 +12:00
|
|
|
|
2016-09-24 21:55:14 +12:00
|
|
|
|
2016-09-24 14:38:58 +12:00
|
|
|
@bot.event
|
|
|
|
async def on_command_completion(command, ctx):
|
2016-09-29 12:52:33 +13:00
|
|
|
# There's no reason to continue waiting for this to complete, so lets immediately launch this in a new future
|
2016-10-01 18:40:03 +13:00
|
|
|
bot.loop.create_task(process_command(ctx))
|
2016-10-01 07:02:41 +13:00
|
|
|
|
2016-09-24 14:38:58 +12:00
|
|
|
|
2016-10-01 18:40:03 +13:00
|
|
|
async def process_command(ctx):
|
2016-09-29 12:52:33 +13:00
|
|
|
author = ctx.message.author
|
|
|
|
server = ctx.message.server
|
2016-10-01 18:40:03 +13:00
|
|
|
command = ctx.command
|
2016-09-29 12:52:33 +13:00
|
|
|
|
|
|
|
r_filter = {'command': command.qualified_name}
|
2017-02-26 10:45:59 +13:00
|
|
|
command_usage = await utils.get_content('command_usage', r_filter)
|
2016-09-29 12:52:33 +13:00
|
|
|
if command_usage is None:
|
2016-09-29 18:08:17 +13:00
|
|
|
command_usage = {'command': command.qualified_name}
|
2016-09-29 12:52:33 +13:00
|
|
|
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 server's usage for this command
|
2017-01-28 10:42:46 +13:00
|
|
|
if ctx.message.server is not None:
|
2017-01-22 15:46:56 +13:00
|
|
|
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
|
2016-09-29 12:52:33 +13:00
|
|
|
|
|
|
|
# Save all the changes
|
2017-02-26 10:45:59 +13:00
|
|
|
if not await utils.update_content('command_usage', command_usage, r_filter):
|
|
|
|
await utils.add_content('command_usage', command_usage, r_filter)
|
2016-08-04 13:31:38 +12:00
|
|
|
|
2016-09-01 07:09:47 +12:00
|
|
|
|
2016-07-09 13:42:33 +12:00
|
|
|
@bot.event
|
|
|
|
async def on_command_error(error, ctx):
|
2016-09-18 13:29:48 +12:00
|
|
|
if isinstance(error, commands.CommandNotFound):
|
|
|
|
return
|
2016-11-12 15:22:35 +13:00
|
|
|
if isinstance(error, commands.DisabledCommand):
|
|
|
|
return
|
2016-09-23 13:48:18 +12:00
|
|
|
try:
|
|
|
|
if isinstance(error.original, discord.Forbidden):
|
|
|
|
return
|
2016-11-02 08:28:47 +13:00
|
|
|
elif isinstance(error.original, discord.HTTPException) and 'empty message' in str(error.original):
|
|
|
|
return
|
2017-02-13 11:40:28 +13:00
|
|
|
elif isinstance(error.original, aiohttp.ClientOSError):
|
|
|
|
return
|
2016-09-23 13:48:18 +12:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2016-08-02 01:10:52 +12:00
|
|
|
if isinstance(error, commands.BadArgument):
|
2016-07-12 03:01:59 +12:00
|
|
|
fmt = "Please provide a valid argument to pass to the command: {}".format(error)
|
2016-07-12 00:40:03 +12:00
|
|
|
await bot.send_message(ctx.message.channel, fmt)
|
2016-07-14 09:16:47 +12:00
|
|
|
elif isinstance(error, commands.CheckFailure):
|
2016-07-14 09:24:21 +12:00
|
|
|
fmt = "You can't tell me what to do!"
|
2016-07-14 09:16:47 +12:00
|
|
|
await bot.send_message(ctx.message.channel, fmt)
|
2016-07-24 04:39:25 +12:00
|
|
|
elif isinstance(error, commands.CommandOnCooldown):
|
2016-08-04 13:40:35 +12:00
|
|
|
m, s = divmod(error.retry_after, 60)
|
2016-08-07 10:20:16 +12:00
|
|
|
fmt = "This command is on cooldown! Hold your horses! >:c\nTry again in {} minutes and {} seconds" \
|
2016-08-04 13:40:35 +12:00
|
|
|
.format(round(m), round(s))
|
2016-07-24 04:39:25 +12:00
|
|
|
await bot.send_message(ctx.message.channel, fmt)
|
2016-08-07 23:53:41 +12:00
|
|
|
elif isinstance(error, commands.NoPrivateMessage):
|
2016-08-07 23:52:20 +12:00
|
|
|
fmt = "This command cannot be used in a private message"
|
|
|
|
await bot.send_message(ctx.message.channel, fmt)
|
2016-08-09 11:57:33 +12:00
|
|
|
elif isinstance(error, commands.MissingRequiredArgument):
|
|
|
|
await bot.send_message(ctx.message.channel, error)
|
2016-09-12 10:34:28 +12:00
|
|
|
else:
|
2016-08-07 00:16:49 +12:00
|
|
|
now = datetime.datetime.now()
|
2016-08-12 14:13:02 +12:00
|
|
|
with open("error_log", 'a') as f:
|
2016-08-07 10:20:16 +12:00
|
|
|
print("In server '{0.message.server}' at {1}\nFull command: `{0.message.content}`".format(ctx, str(now)),
|
|
|
|
file=f)
|
2016-08-07 23:47:42 +12:00
|
|
|
try:
|
|
|
|
traceback.print_tb(error.original.__traceback__, file=f)
|
2016-08-07 23:49:01 +12:00
|
|
|
print('{0.__class__.__name__}: {0}'.format(error.original), file=f)
|
2016-08-07 23:47:42 +12:00
|
|
|
except:
|
2016-08-07 23:52:20 +12:00
|
|
|
traceback.print_tb(error.__traceback__, file=f)
|
2016-08-07 23:49:01 +12:00
|
|
|
print('{0.__class__.__name__}: {0}'.format(error), file=f)
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
if __name__ == '__main__':
|
2016-11-11 16:08:01 +13:00
|
|
|
bot.remove_command('help')
|
2016-11-11 16:15:23 +13:00
|
|
|
|
2017-02-26 10:45:59 +13:00
|
|
|
for e in utils.extensions:
|
2016-07-09 13:27:19 +12:00
|
|
|
bot.load_extension(e)
|
2017-02-26 10:45:59 +13:00
|
|
|
bot.run(utils.bot_token)
|