2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2016-10-06 07:44:37 +13:00
|
|
|
|
2017-02-13 12:45:19 +13:00
|
|
|
from . import utils
|
2016-10-06 07:44:37 +13:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
import re
|
2016-07-17 03:56:20 +12:00
|
|
|
import glob
|
2017-03-08 11:35:30 +13:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2016-07-09 13:27:19 +12:00
|
|
|
import discord
|
2016-07-13 16:30:22 +12:00
|
|
|
import inspect
|
2016-10-08 09:39:41 +13:00
|
|
|
import pendulum
|
2017-03-26 12:37:12 +13:00
|
|
|
import textwrap
|
|
|
|
import traceback
|
|
|
|
from contextlib import redirect_stdout
|
|
|
|
import io
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
2017-06-28 12:26:32 +12:00
|
|
|
def get_syntax_error(e):
|
|
|
|
if e.text is None:
|
|
|
|
return '```py\n{0.__class__.__name__}: {0}\n```'.format(e)
|
|
|
|
return '```py\n{0.text}{1:>{0.offset}}\n{2}: {0}```'.format(e, '^', type(e).__name__)
|
|
|
|
|
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
class Owner:
|
2017-06-30 07:49:52 +12:00
|
|
|
"""Commands that can only be used by the owner of the bot, 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
|
2017-03-26 12:37:12 +13:00
|
|
|
self._last_result = None
|
|
|
|
self.sessions = set()
|
|
|
|
|
|
|
|
def cleanup_code(self, content):
|
|
|
|
"""Automatically removes code blocks from the code."""
|
|
|
|
# remove ```py\n```
|
|
|
|
if content.startswith('```') and content.endswith('```'):
|
|
|
|
return '\n'.join(content.split('\n')[1:-1])
|
|
|
|
|
|
|
|
# remove `foo`
|
|
|
|
return content.strip('` \n')
|
|
|
|
|
2017-05-19 15:09:00 +12:00
|
|
|
async def on_guild_join(self, guild):
|
|
|
|
# Create our embed that we'll use for the information
|
|
|
|
embed = discord.Embed(title="Joined guild {}".format(guild.name), description="Created on: {}".format(guild.created_at.date()))
|
|
|
|
|
|
|
|
# Make sure we only set the icon url if it has been set
|
|
|
|
if guild.icon_url != "":
|
|
|
|
embed.set_thumbnail(url=guild.icon_url)
|
|
|
|
|
|
|
|
# Add our fields, these are self-explanatory
|
|
|
|
embed.add_field(name='Region', value=str(guild.region))
|
|
|
|
embed.add_field(name='Total Emojis', value=len(guild.emojis))
|
|
|
|
|
|
|
|
# Get the amount of online members
|
|
|
|
online_members = [m for m in guild.members if str(m.status) == 'online']
|
|
|
|
embed.add_field(name='Total members', value='{}/{}'.format(len(online_members), guild.member_count))
|
|
|
|
embed.add_field(name='Roles', value=len(guild.roles))
|
|
|
|
|
|
|
|
# Split channels into voice and text channels
|
|
|
|
voice_channels = [c for c in guild.channels if type(c) is discord.VoiceChannel]
|
|
|
|
text_channels = [c for c in guild.channels if type(c) is discord.TextChannel]
|
|
|
|
embed.add_field(name='Channels', value='{} text, {} voice'.format(len(text_channels), len(voice_channels)))
|
|
|
|
embed.add_field(name='Owner', value=guild.owner.display_name)
|
|
|
|
|
|
|
|
await self.bot.owner.send(embed=embed)
|
|
|
|
|
|
|
|
async def on_guild_remove(self, guild):
|
|
|
|
# Create our embed that we'll use for the information
|
|
|
|
embed = discord.Embed(title="Left guild {}".format(guild.name), description="Created on: {}".format(guild.created_at.date()))
|
|
|
|
|
|
|
|
# Make sure we only set the icon url if it has been set
|
|
|
|
if guild.icon_url != "":
|
|
|
|
embed.set_thumbnail(url=guild.icon_url)
|
|
|
|
|
|
|
|
# Add our fields, these are self-explanatory
|
|
|
|
embed.add_field(name='Region', value=str(guild.region))
|
|
|
|
embed.add_field(name='Total Emojis', value=len(guild.emojis))
|
|
|
|
|
|
|
|
# Get the amount of online members
|
|
|
|
online_members = [m for m in guild.members if str(m.status) == 'online']
|
|
|
|
embed.add_field(name='Total members', value='{}/{}'.format(len(online_members), guild.member_count))
|
|
|
|
embed.add_field(name='Roles', value=len(guild.roles))
|
|
|
|
|
|
|
|
# Split channels into voice and text channels
|
|
|
|
voice_channels = [c for c in guild.channels if type(c) is discord.VoiceChannel]
|
|
|
|
text_channels = [c for c in guild.channels if type(c) is discord.TextChannel]
|
|
|
|
embed.add_field(name='Channels', value='{} text, {} voice'.format(len(text_channels), len(voice_channels)))
|
|
|
|
embed.add_field(name='Owner', value=guild.owner.display_name)
|
|
|
|
|
|
|
|
await self.bot.owner.send(embed=embed)
|
|
|
|
|
|
|
|
|
2017-03-26 12:37:12 +13:00
|
|
|
@commands.command(hidden=True)
|
|
|
|
@commands.check(utils.is_owner)
|
|
|
|
async def repl(self, ctx):
|
|
|
|
msg = ctx.message
|
|
|
|
|
|
|
|
variables = {
|
|
|
|
'ctx': ctx,
|
|
|
|
'bot': self.bot,
|
|
|
|
'message': msg,
|
|
|
|
'guild': msg.guild,
|
2017-04-14 14:55:57 +12:00
|
|
|
'server': msg.guild,
|
2017-03-26 12:37:12 +13:00
|
|
|
'channel': msg.channel,
|
|
|
|
'author': msg.author,
|
2017-06-17 10:04:26 +12:00
|
|
|
'self': self,
|
2017-03-26 12:37:12 +13:00
|
|
|
'_': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.channel.id in self.sessions:
|
|
|
|
await ctx.send('Already running a REPL session in this channel. Exit it with `quit`.')
|
|
|
|
return
|
|
|
|
|
|
|
|
self.sessions.add(msg.channel.id)
|
|
|
|
await ctx.send('Enter code to execute or evaluate. `exit()` or `quit` to exit.')
|
|
|
|
|
|
|
|
def check(m):
|
|
|
|
return m.author.id == msg.author.id and \
|
|
|
|
m.channel.id == msg.channel.id and \
|
|
|
|
m.content.startswith('`')
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
response = await self.bot.wait_for('message', check=check, timeout=10.0 * 60.0)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
await ctx.send('Exiting REPL session.')
|
|
|
|
self.sessions.remove(msg.channel.id)
|
|
|
|
break
|
|
|
|
|
|
|
|
cleaned = self.cleanup_code(response.content)
|
|
|
|
|
|
|
|
if cleaned in ('quit', 'exit', 'exit()'):
|
|
|
|
await ctx.send('Exiting.')
|
|
|
|
self.sessions.remove(msg.channel.id)
|
|
|
|
return
|
|
|
|
|
|
|
|
executor = exec
|
|
|
|
if cleaned.count('\n') == 0:
|
|
|
|
# single statement, potentially 'eval'
|
|
|
|
try:
|
|
|
|
code = compile(cleaned, '<repl session>', 'eval')
|
|
|
|
except SyntaxError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
executor = eval
|
|
|
|
|
|
|
|
if executor is exec:
|
|
|
|
try:
|
|
|
|
code = compile(cleaned, '<repl session>', 'exec')
|
|
|
|
except SyntaxError as e:
|
2017-06-28 12:26:32 +12:00
|
|
|
await ctx.send(get_syntax_error(e))
|
2017-03-26 12:37:12 +13:00
|
|
|
continue
|
|
|
|
|
|
|
|
variables['message'] = response
|
|
|
|
|
|
|
|
fmt = None
|
|
|
|
stdout = io.StringIO()
|
|
|
|
|
|
|
|
try:
|
|
|
|
with redirect_stdout(stdout):
|
|
|
|
result = executor(code, variables)
|
|
|
|
if inspect.isawaitable(result):
|
|
|
|
result = await result
|
|
|
|
except Exception as e:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
fmt = '```py\n{}{}\n```'.format(value, traceback.format_exc())
|
|
|
|
else:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
if result is not None:
|
|
|
|
fmt = '```py\n{}{}\n```'.format(value, result)
|
|
|
|
variables['_'] = result
|
|
|
|
elif value:
|
|
|
|
fmt = '```py\n{}\n```'.format(value)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if fmt is not None:
|
|
|
|
if len(fmt) > 2000:
|
|
|
|
await ctx.send('Content too big to be printed.')
|
|
|
|
else:
|
|
|
|
await ctx.send(fmt)
|
|
|
|
except discord.Forbidden:
|
|
|
|
pass
|
|
|
|
except discord.HTTPException as e:
|
|
|
|
await ctx.send('Unexpected error: `{}`'.format(e))
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2017-05-06 08:53:12 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.check(utils.is_owner)
|
2017-05-06 08:58:17 +12:00
|
|
|
async def sendtochannel(self, ctx, cid: int, *, message):
|
2017-05-06 08:53:12 +12:00
|
|
|
"""Sends a message to a provided channel, by ID"""
|
|
|
|
channel = self.bot.get_channel(cid)
|
|
|
|
await channel.send(message)
|
|
|
|
try:
|
|
|
|
await ctx.message.delete()
|
|
|
|
except discord.Forbidden:
|
|
|
|
pass
|
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-05-10 11:21:05 +12:00
|
|
|
async def debug(self, ctx, *, body: str):
|
2017-02-18 08:52:25 +13:00
|
|
|
env = {
|
|
|
|
'bot': self.bot,
|
|
|
|
'ctx': ctx,
|
|
|
|
'channel': ctx.message.channel,
|
2017-05-10 11:21:05 +12:00
|
|
|
'author': ctx.message.author,
|
|
|
|
'server': ctx.message.guild,
|
2017-05-08 13:39:34 +12:00
|
|
|
'guild': ctx.message.guild,
|
2017-05-10 11:21:05 +12:00
|
|
|
'message': ctx.message,
|
2017-06-17 10:04:26 +12:00
|
|
|
'self': self,
|
2017-05-10 11:21:05 +12:00
|
|
|
'_': self._last_result
|
2017-02-18 08:52:25 +13:00
|
|
|
}
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2017-02-18 08:52:25 +13:00
|
|
|
env.update(globals())
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2017-05-10 11:21:05 +12:00
|
|
|
body = self.cleanup_code(body)
|
|
|
|
stdout = io.StringIO()
|
|
|
|
|
|
|
|
to_compile = 'async def func():\n%s' % textwrap.indent(body, ' ')
|
|
|
|
|
2017-02-18 08:52:25 +13:00
|
|
|
try:
|
2017-05-10 11:21:05 +12:00
|
|
|
exec(to_compile, env)
|
|
|
|
except SyntaxError as e:
|
2017-06-28 12:26:32 +12:00
|
|
|
return await ctx.send(get_syntax_error(e))
|
2017-05-10 11:21:05 +12:00
|
|
|
|
|
|
|
func = env['func']
|
2017-03-04 20:51:30 +13:00
|
|
|
try:
|
2017-05-10 11:21:05 +12:00
|
|
|
with redirect_stdout(stdout):
|
|
|
|
ret = await func()
|
|
|
|
except Exception as e:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
await ctx.send('```py\n{}{}\n```'.format(value, traceback.format_exc()))
|
|
|
|
else:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
try:
|
|
|
|
await ctx.message.add_reaction('\u2705')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2017-06-17 10:04:26 +12:00
|
|
|
try:
|
|
|
|
if ret is None:
|
|
|
|
if value:
|
|
|
|
await ctx.send('```py\n%s\n```' % value)
|
|
|
|
else:
|
|
|
|
self._last_result = ret
|
|
|
|
await ctx.send('```py\n%s%s\n```' % (value, ret))
|
|
|
|
except discord.HTTPException:
|
|
|
|
await ctx.send("Content too large for me to print!")
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.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}'
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(fmt.format(ctx.message))
|
2016-07-10 00:57:25 +12:00
|
|
|
await self.bot.logout()
|
|
|
|
await self.bot.close()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-03-09 08:25:52 +13:00
|
|
|
async def name(self, ctx, new_nick: str):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Changes the bot's name"""
|
2017-03-09 08:25:52 +13:00
|
|
|
await self.bot.user.edit(username=new_nick)
|
|
|
|
await ctx.send('Changed username to ' + new_nick)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-03-08 11:35:30 +13:00
|
|
|
async def status(self, ctx, *, status: str):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Changes the bot's 'playing' status"""
|
2017-03-09 08:25:52 +13:00
|
|
|
await self.bot.change_presence(game=discord.Game(name=status, type=0))
|
|
|
|
await ctx.send("Just changed my status to '{}'!".format(status))
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-13 02:17:47 +12:00
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-03-08 11:35:30 +13:00
|
|
|
async def load(self, ctx, *, module: str):
|
2016-07-13 02:17:47 +12:00
|
|
|
"""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)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("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```'
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(fmt.format(type(error).__name__, error))
|
2016-08-17 03:22:32 +12:00
|
|
|
|
2016-07-13 02:17:47 +12:00
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-03-08 11:35:30 +13:00
|
|
|
async def unload(self, ctx, *, module: str):
|
2016-07-13 02:17:47 +12:00
|
|
|
"""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)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I have just unloaded the {} module".format(module))
|
2016-07-13 02:17:47 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2017-02-13 12:45:19 +13:00
|
|
|
@commands.check(utils.is_owner)
|
2017-03-08 11:35:30 +13:00
|
|
|
async def reload(self, ctx, *, module: str):
|
2016-07-13 02:17:47 +12:00
|
|
|
"""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)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("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```'
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(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))
|