2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2016-10-06 07:44:37 +13:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
import asyncio
|
2021-04-01 21:14:14 +13:00
|
|
|
from contextlib import redirect_stdout
|
2016-07-09 13:27:19 +12:00
|
|
|
import discord
|
2016-07-13 16:30:22 +12:00
|
|
|
import inspect
|
2021-04-01 21:14:14 +13:00
|
|
|
import io
|
|
|
|
import re
|
2017-03-26 12:37:12 +13:00
|
|
|
import textwrap
|
|
|
|
import traceback
|
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:
|
2020-10-11 16:36:11 +13:00
|
|
|
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__
|
|
|
|
)
|
2017-06-28 12:26:32 +12:00
|
|
|
|
|
|
|
|
2019-02-24 09:13:10 +13:00
|
|
|
class Owner(commands.Cog):
|
2017-06-30 07:49:52 +12:00
|
|
|
"""Commands that can only be used by the owner of the bot, bot management commands"""
|
2020-10-11 16:36:11 +13:00
|
|
|
|
2019-02-24 09:13:10 +13:00
|
|
|
_last_result = None
|
|
|
|
sessions = set()
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2019-02-24 09:13:10 +13:00
|
|
|
async def cog_check(self, ctx):
|
|
|
|
return await ctx.bot.is_owner(ctx.author)
|
2018-10-30 14:00:37 +13:00
|
|
|
|
2018-09-24 07:23:27 +12:00
|
|
|
@staticmethod
|
|
|
|
def cleanup_code(content):
|
2017-03-26 12:37:12 +13:00
|
|
|
"""Automatically removes code blocks from the code."""
|
|
|
|
# remove ```py\n```
|
2020-10-11 16:36:11 +13:00
|
|
|
if content.startswith("```") and content.endswith("```"):
|
|
|
|
return "\n".join(content.split("\n")[1:-1])
|
2017-03-26 12:37:12 +13:00
|
|
|
|
|
|
|
# remove `foo`
|
2020-10-11 16:36:11 +13:00
|
|
|
return content.strip("` \n")
|
2017-03-26 12:37:12 +13:00
|
|
|
|
|
|
|
@commands.command(hidden=True)
|
|
|
|
async def repl(self, ctx):
|
|
|
|
msg = ctx.message
|
|
|
|
|
|
|
|
variables = {
|
2020-10-11 16:36:11 +13:00
|
|
|
"ctx": ctx,
|
|
|
|
"bot": ctx.bot,
|
|
|
|
"message": msg,
|
|
|
|
"guild": msg.guild,
|
|
|
|
"server": msg.guild,
|
|
|
|
"channel": msg.channel,
|
|
|
|
"author": msg.author,
|
|
|
|
"self": self,
|
|
|
|
"_": None,
|
2017-03-26 12:37:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.channel.id in self.sessions:
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send(
|
|
|
|
"Already running a REPL session in this channel. Exit it with `quit`."
|
|
|
|
)
|
2017-03-26 12:37:12 +13:00
|
|
|
return
|
|
|
|
|
|
|
|
self.sessions.add(msg.channel.id)
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send("Enter code to execute or evaluate. `exit()` or `quit` to exit.")
|
2017-03-26 12:37:12 +13:00
|
|
|
|
|
|
|
def check(m):
|
2020-10-11 16:36:11 +13:00
|
|
|
return (
|
|
|
|
m.author.id == msg.author.id
|
|
|
|
and m.channel.id == msg.channel.id
|
|
|
|
and m.content.startswith("`")
|
|
|
|
)
|
2017-03-26 12:37:12 +13:00
|
|
|
|
2018-10-30 14:00:37 +13:00
|
|
|
code = None
|
|
|
|
|
2017-03-26 12:37:12 +13:00
|
|
|
while True:
|
|
|
|
try:
|
2020-10-11 16:36:11 +13:00
|
|
|
response = await ctx.bot.wait_for(
|
|
|
|
"message", check=check, timeout=10.0 * 60.0
|
|
|
|
)
|
2017-03-26 12:37:12 +13:00
|
|
|
except asyncio.TimeoutError:
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send("Exiting REPL session.")
|
2017-03-26 12:37:12 +13:00
|
|
|
self.sessions.remove(msg.channel.id)
|
|
|
|
break
|
|
|
|
|
|
|
|
cleaned = self.cleanup_code(response.content)
|
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
if cleaned in ("quit", "exit", "exit()"):
|
|
|
|
await ctx.send("Exiting.")
|
2017-03-26 12:37:12 +13:00
|
|
|
self.sessions.remove(msg.channel.id)
|
|
|
|
return
|
|
|
|
|
|
|
|
executor = exec
|
2020-10-11 16:36:11 +13:00
|
|
|
if cleaned.count("\n") == 0:
|
2017-03-26 12:37:12 +13:00
|
|
|
# single statement, potentially 'eval'
|
|
|
|
try:
|
2020-10-11 16:36:11 +13:00
|
|
|
code = compile(cleaned, "<repl session>", "eval")
|
2017-03-26 12:37:12 +13:00
|
|
|
except SyntaxError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
executor = eval
|
|
|
|
|
|
|
|
if executor is exec:
|
|
|
|
try:
|
2020-10-11 16:36:11 +13:00
|
|
|
code = compile(cleaned, "<repl session>", "exec")
|
2017-03-26 12:37:12 +13:00
|
|
|
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
|
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
variables["message"] = response
|
2017-03-26 12:37:12 +13:00
|
|
|
|
|
|
|
fmt = None
|
|
|
|
stdout = io.StringIO()
|
|
|
|
|
|
|
|
try:
|
|
|
|
with redirect_stdout(stdout):
|
|
|
|
result = executor(code, variables)
|
|
|
|
if inspect.isawaitable(result):
|
|
|
|
result = await result
|
2018-10-30 14:00:37 +13:00
|
|
|
except Exception:
|
2017-03-26 12:37:12 +13:00
|
|
|
value = stdout.getvalue()
|
2020-10-11 16:36:11 +13:00
|
|
|
fmt = "```py\n{}{}\n```".format(value, traceback.format_exc())
|
2017-03-26 12:37:12 +13:00
|
|
|
else:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
if result is not None:
|
2020-10-11 16:36:11 +13:00
|
|
|
fmt = "```py\n{}{}\n```".format(value, result)
|
|
|
|
variables["_"] = result
|
2017-03-26 12:37:12 +13:00
|
|
|
elif value:
|
2020-10-11 16:36:11 +13:00
|
|
|
fmt = "```py\n{}\n```".format(value)
|
2017-03-26 12:37:12 +13:00
|
|
|
|
|
|
|
try:
|
|
|
|
if fmt is not None:
|
|
|
|
if len(fmt) > 2000:
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send("Content too big to be printed.")
|
2017-03-26 12:37:12 +13:00
|
|
|
else:
|
|
|
|
await ctx.send(fmt)
|
|
|
|
except discord.Forbidden:
|
|
|
|
pass
|
|
|
|
except discord.HTTPException as e:
|
2020-10-11 16:36:11 +13:00
|
|
|
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()
|
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"""
|
2019-02-24 09:13:10 +13:00
|
|
|
channel = ctx.bot.get_channel(cid)
|
2017-05-06 08:53:12 +12:00
|
|
|
await channel.send(message)
|
|
|
|
try:
|
|
|
|
await ctx.message.delete()
|
|
|
|
except discord.Forbidden:
|
|
|
|
pass
|
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
@commands.command()
|
2017-05-10 11:21:05 +12:00
|
|
|
async def debug(self, ctx, *, body: str):
|
2017-02-18 08:52:25 +13:00
|
|
|
env = {
|
2020-10-11 16:36:11 +13:00
|
|
|
"bot": ctx.bot,
|
|
|
|
"ctx": ctx,
|
|
|
|
"channel": ctx.message.channel,
|
|
|
|
"author": ctx.message.author,
|
|
|
|
"server": ctx.message.guild,
|
|
|
|
"guild": ctx.message.guild,
|
|
|
|
"message": ctx.message,
|
|
|
|
"self": self,
|
|
|
|
"_": 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()
|
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
to_compile = "async def func():\n%s" % textwrap.indent(body, " ")
|
2017-05-10 11:21:05 +12:00
|
|
|
|
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
|
|
|
|
2020-10-11 16:36:11 +13: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()
|
2018-09-24 07:23:27 +12:00
|
|
|
except Exception:
|
2017-05-10 11:21:05 +12:00
|
|
|
value = stdout.getvalue()
|
2019-02-15 13:35:53 +13:00
|
|
|
await ctx.send(f"```py\n{value}{traceback.format_exc()}\n```"[:2000])
|
2017-05-10 11:21:05 +12:00
|
|
|
else:
|
|
|
|
value = stdout.getvalue()
|
|
|
|
try:
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.message.add_reaction("\u2705")
|
2018-09-24 07:23:27 +12:00
|
|
|
except Exception:
|
2017-05-10 11:21:05 +12:00
|
|
|
pass
|
|
|
|
|
2019-02-15 13:35:53 +13:00
|
|
|
if ret is None:
|
|
|
|
if value:
|
|
|
|
await ctx.send(f"```py\n{value}\n```"[:2000])
|
|
|
|
else:
|
|
|
|
self._last_result = ret
|
|
|
|
await ctx.send(f"```py\n{value}{ret}\n```"[:2000])
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2018-01-12 05:56:11 +13:00
|
|
|
@commands.command()
|
|
|
|
async def bash(self, ctx, *, cmd: str):
|
|
|
|
"""Runs a bash command"""
|
2020-04-14 07:15:37 +12:00
|
|
|
proc = await asyncio.create_subprocess_shell(
|
2020-10-11 16:36:11 +13:00
|
|
|
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT
|
2020-04-14 07:15:37 +12:00
|
|
|
)
|
2021-04-01 21:19:24 +13:00
|
|
|
m = r"(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&//=]*)"
|
2020-04-14 07:15:37 +12:00
|
|
|
stdout = (await proc.communicate())[0]
|
|
|
|
if stdout:
|
2021-04-01 21:19:24 +13:00
|
|
|
output = re.sub(m, r"<\1>", stdout.decode())
|
2021-04-01 21:14:14 +13:00
|
|
|
await ctx.send(f"[stdout]\n{output}")
|
2018-01-13 10:41:12 +13:00
|
|
|
else:
|
2020-04-14 07:15:37 +12:00
|
|
|
await ctx.send("Process finished, no output")
|
2018-01-12 05:56:11 +13:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
@commands.command()
|
2016-07-09 13:27:19 +12:00
|
|
|
async def shutdown(self, ctx):
|
|
|
|
"""Shuts the bot down"""
|
2020-10-11 16:36:11 +13: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))
|
2019-02-24 09:13:10 +13:00
|
|
|
await ctx.bot.logout()
|
|
|
|
await ctx.bot.close()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
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"""
|
2019-02-24 09:13:10 +13:00
|
|
|
await ctx.bot.user.edit(username=new_nick)
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send("Changed username to " + new_nick)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
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"""
|
2019-02-24 09:13:10 +13:00
|
|
|
await ctx.bot.change_presence(activity=discord.Game(name=status, type=0))
|
2017-03-09 08:25:52 +13:00
|
|
|
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-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:
|
2019-02-24 09:13:10 +13:00
|
|
|
ctx.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:
|
2020-10-11 16:36:11 +13:00
|
|
|
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-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
|
|
|
|
2019-02-24 09:13:10 +13:00
|
|
|
ctx.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-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)
|
2019-02-24 09:13:10 +13:00
|
|
|
ctx.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:
|
2019-02-24 09:13:10 +13:00
|
|
|
ctx.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:
|
2020-10-11 16:36:11 +13:00
|
|
|
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))
|