1
0
Fork 0
mirror of synced 2024-06-22 16:20:23 +12:00
Bonfire/cogs/owner.py

143 lines
5.1 KiB
Python
Raw Normal View History

2016-07-09 13:27:19 +12:00
from discord.ext import commands
from .utils import config
2016-07-09 13:27:19 +12:00
from .utils import checks
2016-12-20 05:45:02 +13:00
from .utils import utilities
2016-07-09 13:27:19 +12:00
import re
import glob
2016-07-09 13:27:19 +12:00
import discord
import inspect
import aiohttp
2016-10-08 09:39:41 +13:00
import pendulum
2016-12-17 08:55:05 +13:00
import asyncio
2016-07-09 13:27:19 +12:00
getter = re.compile(r'`(?!`)(.*?)`')
multi = re.compile(r'```(.*?)```', re.DOTALL)
class Owner:
"""Commands that can only be used by Phantom, bot management commands"""
2016-07-09 13:27:19 +12:00
def __init__(self, bot):
self.bot = bot
2016-08-31 10:33:46 +12:00
2016-10-08 09:39:41 +13:00
@commands.command()
@commands.check(checks.is_owner)
async def motd_push(self, *, message):
"""Used to push a new message to the message of the day"""
date = pendulum.utcnow().to_date_string()
r_filter = {'date': date}
entry = {'motd': message, 'date': date}
# Try to add this, if there's an entry for that date, lets update it to make sure only one motd is sent a day
# I should be managing this myself, more than one should not be sent in a day
2016-10-08 09:40:29 +13:00
if await config.add_content('motd', entry, r_filter):
2016-10-08 09:39:41 +13:00
await config.update_content('motd', entry, r_filter)
await self.bot.say("New motd update for {}!".format(date))
2016-07-31 10:15:59 +12:00
@commands.command(pass_context=True)
@commands.check(checks.is_owner)
async def debug(self, ctx):
2016-07-09 13:27:19 +12:00
"""Executes code"""
# Eval and exec have different useful purposes, so use both
try:
2016-08-17 03:22:32 +12:00
# `Get all content in this format`
match_single = getter.findall(ctx.message.content)
# ```\nGet all content in this format```
match_multi = multi.findall(ctx.message.content)
2016-08-17 03:22:32 +12:00
if match_single:
result = eval(match_single[0])
2016-08-17 03:22:32 +12:00
# In case the result needs to be awaited, handle that
if inspect.isawaitable(result):
result = await result
await self.bot.say("```\n{0}```".format(result))
elif match_multi:
# Internal method to send the message to the channel, of whatever is passed
def r(v):
self.bot.loop.create_task(self.bot.say("```\n{}```".format(v)))
2016-08-17 03:22:32 +12:00
exec(match_multi[0])
except Exception as error:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await self.bot.say(fmt.format(type(error).__name__, error))
2016-07-09 13:27:19 +12:00
@commands.command(pass_context=True)
@commands.check(checks.is_owner)
2016-07-09 13:27:19 +12:00
async def shutdown(self, ctx):
"""Shuts the bot down"""
fmt = 'Shutting down, I will miss you {0.author.name}'
await self.bot.say(fmt.format(ctx.message))
await self.bot.logout()
await self.bot.close()
2016-07-09 13:27:19 +12:00
@commands.command()
@commands.check(checks.is_owner)
async def name(self, newNick: str):
2016-07-09 13:27:19 +12:00
"""Changes the bot's name"""
await self.bot.edit_profile(username=newNick)
await self.bot.say('Changed username to ' + newNick)
2016-07-09 13:27:19 +12:00
@commands.command()
@commands.check(checks.is_owner)
async def status(self, *, status: str):
2016-07-09 13:27:19 +12:00
"""Changes the bot's 'playing' status"""
await self.bot.change_status(discord.Game(name=status, type=0))
2016-08-17 03:22:32 +12:00
await self.bot.say("Just changed my status to '{0}'!".format(status))
2016-07-09 13:27:19 +12:00
@commands.command()
@commands.check(checks.is_owner)
async def load(self, *, module: str):
"""Loads a module"""
2016-08-17 03:22:32 +12:00
# Do this because I'm too lazy to type cogs.module
2016-07-28 23:45:27 +12:00
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
2016-08-17 03:22:32 +12:00
# This try catch will catch errors such as syntax errors in the module we are loading
try:
self.bot.load_extension(module)
await self.bot.say("I have just loaded the {} module".format(module))
except Exception as error:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await self.bot.say(fmt.format(type(error).__name__, error))
2016-08-17 03:22:32 +12:00
@commands.command()
@commands.check(checks.is_owner)
async def unload(self, *, module: str):
"""Unloads a module"""
2016-08-17 03:22:32 +12:00
# Do this because I'm too lazy to type cogs.module
2016-07-28 23:45:27 +12:00
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
2016-08-17 03:22:32 +12:00
self.bot.unload_extension(module)
await self.bot.say("I have just unloaded the {} module".format(module))
@commands.command()
@commands.check(checks.is_owner)
async def reload(self, *, module: str):
"""Reloads a module"""
2016-08-17 03:22:32 +12:00
# Do this because I'm too lazy to type cogs.module
2016-07-28 23:45:27 +12:00
module = module.lower()
if not module.startswith("cogs"):
module = "cogs.{}".format(module)
self.bot.unload_extension(module)
2016-08-17 03:22:32 +12:00
# This try block will catch errors such as syntax errors in the module we are loading
try:
self.bot.load_extension(module)
await self.bot.say("I have just reloaded the {} module".format(module))
except Exception as error:
fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
await self.bot.say(fmt.format(type(error).__name__, error))
2016-07-09 13:27:19 +12:00
def setup(bot):
bot.add_cog(Owner(bot))