2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import config
|
|
|
|
from .utils import checks
|
|
|
|
import re
|
|
|
|
import os
|
2016-07-17 03:56:20 +12:00
|
|
|
import glob
|
2016-07-09 13:27:19 +12:00
|
|
|
import sys
|
|
|
|
import discord
|
2016-07-13 16:30:22 +12:00
|
|
|
import inspect
|
2016-07-22 04:23:55 +12:00
|
|
|
import aiohttp
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
getter = re.compile(r'`(?!`)(.*?)`')
|
|
|
|
multi = re.compile(r'```(.*?)```', re.DOTALL)
|
|
|
|
|
|
|
|
|
|
|
|
class Owner:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Commands that can only be used by Phantom, bot management commands"""
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-08-19 15:48:50 +12:00
|
|
|
|
|
|
|
@commands.command()
|
|
|
|
@commands.check(checks.is_owner)
|
|
|
|
async def testcommand(self, member: discord.Member):
|
2016-08-19 15:50:54 +12:00
|
|
|
roles = [discord.Object(id="183749087038930944")]
|
2016-08-19 15:48:50 +12:00
|
|
|
await self.bot.add_roles(member, *roles)
|
|
|
|
await self.bot.say("Just added the roles {} to {}".format(role, member.display_name))
|
|
|
|
|
2016-08-16 09:15:27 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.check(checks.is_owner)
|
|
|
|
async def saferestart(self, ctx):
|
|
|
|
"""This commands is used to check if there is anything playing in any servers at the moment
|
|
|
|
If there is, I'll tell you not to restart, if not I'll just go ahead and restart"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# I do not want to restart the bot if someone is playing music
|
|
|
|
# This gets all the exiting VoiceStates that are playing music right now
|
|
|
|
# If we are, say which server it
|
2016-08-17 03:22:32 +12:00
|
|
|
servers_playing_music = [server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if
|
|
|
|
state.is_playing()]
|
2016-08-17 14:19:44 +12:00
|
|
|
if len(servers_playing_music) > 0:
|
|
|
|
await self.bot.say("Sorry, it's not safe to restart. I am currently playing a song on {} servers".format(
|
|
|
|
len(servers_playing_music)))
|
|
|
|
else:
|
|
|
|
config.save_content('restart_server', ctx.message.channel.id)
|
|
|
|
await self.bot.say("Restarting; see you in the next life {0}!".format(ctx.message.author.mention))
|
|
|
|
python = sys.executable
|
|
|
|
os.execl(python, python, *sys.argv)
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
@commands.command(pass_context=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-09 13:27:19 +12:00
|
|
|
async def restart(self, ctx):
|
|
|
|
"""Forces the bot to restart"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# This command is left in so that we can invoke it from saferestart, or we need a restart no matter what
|
2016-08-15 14:10:12 +12:00
|
|
|
config.save_content('restart_server', ctx.message.channel.id)
|
2016-07-28 23:45:27 +12:00
|
|
|
await self.bot.say("Restarting; see you in the next life {0}!".format(ctx.message.author.mention))
|
2016-07-10 01:29:17 +12:00
|
|
|
python = sys.executable
|
|
|
|
os.execl(python, python, *sys.argv)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-18 09:10:12 +12:00
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-18 09:10:12 +12:00
|
|
|
async def adddoggo(self, url: str):
|
2016-07-17 04:48:10 +12:00
|
|
|
"""Saves a URL as an image to add for the doggo command"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Save the local path based on how many images there currently are
|
|
|
|
local_path = 'images/doggo{}.jpg'.format(len(glob.glob('images/doggo*')))
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# "read" the image and save as bytes
|
2016-07-22 04:23:55 +12:00
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get(url) as r:
|
|
|
|
val = await r.read()
|
|
|
|
with open(local_path, "wb") as f:
|
|
|
|
f.write(val)
|
2016-07-18 09:10:12 +12:00
|
|
|
await self.bot.say(
|
2016-08-16 15:30:52 +12:00
|
|
|
"Just saved a new doggo image! I now have {} doggo images!".format(len(glob.glob('images/doggo*'))))
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-08 06:04:52 +12:00
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-08-08 06:04:52 +12:00
|
|
|
async def addsnek(self, url: str):
|
|
|
|
"""Saves a URL as an image to add for the snek command"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Save the local path based on how many images there currently are
|
|
|
|
local_path = 'images/snek{}.jpg'.format(len(glob.glob('images/snek*')))
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# "read" the image and save as bytes
|
2016-08-08 06:04:52 +12:00
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get(url) as r:
|
|
|
|
val = await r.read()
|
|
|
|
with open(local_path, "wb") as f:
|
|
|
|
f.write(val)
|
|
|
|
await self.bot.say(
|
2016-08-16 15:30:52 +12:00
|
|
|
"Just saved a new snek image! I now have {} snek images!".format(len(glob.glob('images/snek*'))))
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-31 10:15:59 +12:00
|
|
|
@commands.command(pass_context=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-24 02:36:54 +12:00
|
|
|
async def debug(self, ctx):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Executes code"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Eval and exec have different useful purposes, so use both
|
2016-08-02 03:28:16 +12:00
|
|
|
try:
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# `Get all content in this format`
|
2016-08-02 03:28:16 +12:00
|
|
|
match_single = getter.findall(ctx.message.content)
|
2016-08-16 15:30:52 +12:00
|
|
|
# ```\nGet all content in this format```
|
2016-08-02 03:28:16 +12:00
|
|
|
match_multi = multi.findall(ctx.message.content)
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
if match_single:
|
2016-08-02 03:28:16 +12:00
|
|
|
result = eval(match_single[0])
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# In case the result needs to be awaited, handle that
|
2016-08-02 03:28:16 +12:00
|
|
|
if inspect.isawaitable(result):
|
|
|
|
result = await result
|
|
|
|
await self.bot.say("```\n{0}```".format(result))
|
|
|
|
elif match_multi:
|
2016-08-16 15:30:52 +12:00
|
|
|
# Internal method to send the message to the channel, of whatever is passed
|
2016-08-02 03:28:16 +12:00
|
|
|
def r(v):
|
|
|
|
self.bot.loop.create_task(self.bot.say("```\n{}```".format(v)))
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-02 03:28:16 +12:00
|
|
|
exec(match_multi[0])
|
|
|
|
except Exception as error:
|
2016-08-02 03:31:21 +12:00
|
|
|
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)
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-09 13:27:19 +12:00
|
|
|
async def shutdown(self, ctx):
|
|
|
|
"""Shuts the bot down"""
|
2016-07-10 00:57:25 +12:00
|
|
|
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()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def avatar(self, content: str):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Changes the avatar for the bot to the filename following the command"""
|
2016-08-12 14:13:35 +12:00
|
|
|
file = 'images/' + content
|
2016-07-10 00:57:25 +12:00
|
|
|
with open(file, 'rb') as fp:
|
|
|
|
await self.bot.edit_profile(avatar=fp.read())
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def name(self, newNick: str):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Changes the bot's name"""
|
2016-07-10 00:57:25 +12:00
|
|
|
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()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-08-16 15:30:52 +12:00
|
|
|
async def status(self, *, status: str):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Changes the bot's 'playing' status"""
|
2016-08-16 15:30:52 +12:00
|
|
|
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
|
|
|
|
2016-07-13 02:17:47 +12:00
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-13 02:17:47 +12:00
|
|
|
async def load(self, *, module: str):
|
|
|
|
"""Loads a module"""
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +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
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# This try catch will catch errors such as syntax errors in the module we are loading
|
2016-08-05 06:29:45 +12:00
|
|
|
try:
|
|
|
|
self.bot.load_extension(module)
|
2016-08-05 06:31:09 +12:00
|
|
|
await self.bot.say("I have just loaded the {} module".format(module))
|
2016-08-05 06:29:45 +12:00
|
|
|
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
|
|
|
|
2016-07-13 02:17:47 +12:00
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-13 02:17:47 +12:00
|
|
|
async def unload(self, *, module: str):
|
|
|
|
"""Unloads a module"""
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +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
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
self.bot.unload_extension(module)
|
|
|
|
await self.bot.say("I have just unloaded the {} module".format(module))
|
2016-07-13 02:17:47 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2016-08-15 14:10:12 +12:00
|
|
|
@commands.check(checks.is_owner)
|
2016-07-13 02:17:47 +12:00
|
|
|
async def reload(self, *, module: str):
|
|
|
|
"""Reloads a module"""
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-08-16 15:30:52 +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
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# This try block will catch errors such as syntax errors in the module we are loading
|
2016-08-05 06:29:45 +12:00
|
|
|
try:
|
|
|
|
self.bot.load_extension(module)
|
2016-08-05 06:31:09 +12:00
|
|
|
await self.bot.say("I have just reloaded the {} module".format(module))
|
2016-08-05 06:29:45 +12:00
|
|
|
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-13 02:17:47 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Owner(bot))
|