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

133 lines
4.7 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
os.chdir(os.path.dirname(os.path.realpath(__file__)))
2016-07-09 11:26:43 +12:00
from discord.ext import commands
from cogs.utils import config
2016-07-09 13:27:19 +12:00
extensions = ['cogs.interaction',
'cogs.core',
'cogs.mod',
'cogs.owner',
'cogs.stats',
2016-07-11 05:28:16 +12:00
'cogs.playlist',
'cogs.twitch',
'cogs.picarto',
'cogs.overwatch',
'cogs.links',
'cogs.tags',
'cogs.roles',
'cogs.strawpoll',
'cogs.tictactoe',
'cogs.hangman',
'cogs.statsupdate']
2016-07-08 10:10:24 +12:00
2016-08-31 10:33:46 +12:00
opts = {'command_prefix': config.command_prefix,
'description': config.bot_description,
2016-08-31 07:16:24 +12:00
'pm_help': None,
'shard_count': config.shard_count,
'shard_id': config.shard_id}
2016-08-31 07:16:24 +12:00
bot = commands.Bot(**opts)
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-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
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', {})
2016-08-16 08:43:18 +12:00
# 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())
2016-07-09 13:27:19 +12:00
await bot.send_message(destination, "I have just finished restarting!")
await config.save_content('restart_server', 0)
2016-07-08 01:05:42 +12:00
@bot.event
async def on_member_join(member):
notifications = await config.get_content('user_notifications')
server_notifications = notifications.get(member.server.id)
2016-08-16 08:43:18 +12:00
# By default, notifications should be off unless explicitly turned on
if not server_notifications:
return
channel = discord.utils.get(member.server.channels, id=server_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)
2016-08-16 08:43:18 +12:00
# By default, notifications should be off unless explicitly turned on
if not server_notifications:
return
channel = discord.utils.get(member.server.channels, id=server_notifications)
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))
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
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, discord.Forbidden) or isinstance(error, commands.CommandNotFound):
return
2016-08-02 01:10:52 +12:00
if isinstance(error, commands.BadArgument):
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)
elif isinstance(error, commands.CheckFailure):
fmt = "You can't tell me what to do!"
await bot.send_message(ctx.message.channel, 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 bot.send_message(ctx.message.channel, fmt)
elif isinstance(error, commands.NoPrivateMessage):
fmt = "This command cannot be used in a private message"
await bot.send_message(ctx.message.channel, fmt)
elif isinstance(error, commands.MissingRequiredArgument):
await bot.send_message(ctx.message.channel, error)
else:
2016-08-07 00:16:49 +12:00
now = datetime.datetime.now()
with open("error_log", 'a') as f:
print("In server '{0.message.server}' 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:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
2016-07-31 12:20:55 +12:00
2016-07-09 13:27:19 +12:00
if __name__ == '__main__':
for e in extensions:
bot.load_extension(e)
bot.run(config.bot_token)