1
0
Fork 0
mirror of synced 2024-05-17 19:12:33 +12:00
Bonfire/bot.py

136 lines
4.9 KiB
Python
Raw Normal View History

2016-07-08 01:05:42 +12:00
#!/usr/local/bin/python3.5
2016-07-09 11:26:43 +12:00
import discord
import traceback
2016-07-31 02:59:35 +12:00
import logging
import datetime
import pendulum
import os
import aiohttp
os.chdir(os.path.dirname(os.path.realpath(__file__)))
2016-07-09 11:26:43 +12:00
from discord.ext import commands
import utils
2018-10-05 08:38:15 +13:00
opts = {
'command_prefix': utils.command_prefix,
'description': utils.bot_description,
'pm_help': None,
'command_not_found': '',
'activity': discord.Activity(name=utils.default_status, type=0)
}
2017-03-06 09:40:00 +13:00
bot = commands.AutoShardedBot(**opts)
2017-04-11 17:22:11 +12:00
logging.basicConfig(level=logging.INFO, filename='bonfire.log')
2016-07-08 10:10:24 +12:00
2016-07-31 12:20:55 +12:00
2016-09-24 14:38:58 +12:00
@bot.event
async def on_command_completion(ctx):
2016-09-29 12:52:33 +13:00
author = ctx.message.author
2017-03-08 12:28:07 +13:00
server = ctx.message.guild
command = ctx.command
2016-09-29 12:52:33 +13:00
command_usage = await bot.db.actual_load(
'command_usage', key=command.qualified_name
) or {'command': command.qualified_name}
2016-09-29 12:52:33 +13:00
# 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(str(author.id), 0) + 1
total_member_usage[str(author.id)] = member_usage
2016-09-29 12:52:33 +13:00
command_usage['member_usage'] = total_member_usage
# Add one to the server's usage for this command
2017-03-08 12:28:07 +13:00
if ctx.message.guild is not None:
total_server_usage = command_usage.get('server_usage', {})
server_usage = total_server_usage.get(str(server.id), 0) + 1
total_server_usage[str(server.id)] = server_usage
command_usage['server_usage'] = total_server_usage
2016-09-29 12:52:33 +13:00
# Save all the changes
await bot.db.save('command_usage', command_usage)
# Now add credits to a users amount
# user_credits = bot.db.load('credits', key=ctx.author.id, pluck='credits') or 1000
# user_credits = int(user_credits) + 5
# update = {
# 'member_id': str(ctx.author.id),
# 'credits': user_credits
# }
# await bot.db.save('credits', update)
@bot.event
async def on_command_error(ctx, error):
2016-09-18 13:29:48 +12:00
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.DisabledCommand):
return
try:
if isinstance(error.original, discord.Forbidden):
return
2017-07-12 05:54:30 +12:00
elif isinstance(error.original, discord.HTTPException) and (
'empty message' in str(error.original) or
'INTERNAL SERVER ERROR' in str(error.original) or
2017-07-16 11:00:41 +12:00
'REQUEST ENTITY TOO LARGE' in str(error.original) or
2017-11-09 06:00:05 +13:00
'Unknown Message' in str(error.original) or
'Origin Time-out' in str(error.original) or
'Bad Gateway' in str(error.original) or
2018-10-15 06:23:29 +13:00
'Gateway Time-out' in str(error.original) or
2017-11-09 06:00:05 +13:00
'Explicit content' in str(error.original)):
return
elif isinstance(error.original, aiohttp.ClientOSError):
return
2017-11-09 06:00:05 +13:00
elif isinstance(error.original, discord.NotFound) and 'Unknown Channel' in str(error.original):
return
except AttributeError:
pass
try:
if isinstance(error, commands.BadArgument):
fmt = "Please provide a valid argument to pass to the command: {}".format(error)
await ctx.message.channel.send(fmt)
elif isinstance(error, commands.CheckFailure):
fmt = "You can't tell me what to do!"
2017-07-12 05:54:30 +12:00
# await ctx.message.channel.send(fmt)
elif isinstance(error, commands.CommandOnCooldown):
m, s = divmod(error.retry_after, 60)
fmt = "This command is on cooldown! Hold your horses! >:c\nTry again in {} minutes and {} seconds" \
.format(round(m), round(s))
# await ctx.message.channel.send(fmt)
elif isinstance(error, commands.NoPrivateMessage):
fmt = "This command cannot be used in a private message"
await ctx.message.channel.send(fmt)
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.message.channel.send(error)
else:
now = datetime.datetime.now()
with open("error_log", 'a') as f:
print("In server '{0.message.guild}' at {1}\nFull command: `{0.message.content}`".format(ctx, str(now)),
file=f)
try:
traceback.print_tb(error.original.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error.original), file=f)
except Exception:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
except discord.HTTPException:
pass
2016-07-31 12:20:55 +12:00
2016-07-09 13:27:19 +12:00
if __name__ == '__main__':
bot.loop.create_task(utils.db_check())
bot.remove_command('help')
2017-06-28 14:01:36 +12:00
bot.db = utils.DB()
for e in utils.extensions:
2016-07-09 13:27:19 +12:00
bot.load_extension(e)
2017-06-07 20:30:19 +12:00
2018-09-24 10:41:05 +12:00
bot.uptime = pendulum.now(tz="UTC")
bot.run(utils.bot_token)