1
0
Fork 0
mirror of synced 2024-05-16 02:22:32 +12:00
Bonfire/cogs/owner.py

281 lines
9 KiB
Python
Raw Permalink Normal View History

2016-07-09 13:27:19 +12:00
from discord.ext import commands
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
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
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__
)
2019-02-24 09:13:10 +13:00
class Owner(commands.Cog):
"""Commands that can only be used by the owner of the bot, bot management commands"""
2019-02-24 09:13:10 +13:00
_last_result = None
sessions = set()
2019-02-24 09:13:10 +13:00
async def cog_check(self, ctx):
return await ctx.bot.is_owner(ctx.author)
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```
if content.startswith("```") and content.endswith("```"):
return "\n".join(content.split("\n")[1:-1])
2017-03-26 12:37:12 +13:00
# remove `foo`
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 = {
"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:
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)
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):
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
code = None
2017-03-26 12:37:12 +13:00
while True:
try:
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:
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)
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
if cleaned.count("\n") == 0:
2017-03-26 12:37:12 +13:00
# single statement, potentially 'eval'
try:
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:
code = compile(cleaned, "<repl session>", "exec")
2017-03-26 12:37:12 +13:00
except SyntaxError as e:
await ctx.send(get_syntax_error(e))
2017-03-26 12:37:12 +13:00
continue
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
except Exception:
2017-03-26 12:37:12 +13:00
value = stdout.getvalue()
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:
fmt = "```py\n{}{}\n```".format(value, result)
variables["_"] = result
2017-03-26 12:37:12 +13:00
elif value:
fmt = "```py\n{}\n```".format(value)
2017-03-26 12:37:12 +13:00
try:
if fmt is not None:
if len(fmt) > 2000:
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:
await ctx.send("Unexpected error: `{}`".format(e))
2016-08-31 10:33:46 +12:00
@commands.command()
2017-05-06 08:58:17 +12:00
async def sendtochannel(self, ctx, cid: int, *, message):
"""Sends a message to a provided channel, by ID"""
2019-02-24 09:13:10 +13:00
channel = ctx.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-05-10 11:21:05 +12:00
async def debug(self, ctx, *, body: str):
env = {
"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,
}
2016-08-17 03:22:32 +12: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-05-10 11:21:05 +12:00
try:
2017-05-10 11:21:05 +12:00
exec(to_compile, env)
except SyntaxError as e:
return await ctx.send(get_syntax_error(e))
2017-05-10 11:21:05 +12:00
func = env["func"]
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()
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:
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
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(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT
2020-04-14 07:15:37 +12: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:
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"""
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()
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)
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))
await ctx.send("Just changed my status to '{}'!".format(status))
2016-07-09 13:27:19 +12:00
@commands.command()
2017-03-08 11:35:30 +13:00
async def load(self, ctx, *, 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:
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))
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
@commands.command()
2017-03-08 11:35:30 +13:00
async def unload(self, ctx, *, 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
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))
@commands.command()
2017-03-08 11:35:30 +13:00
async def reload(self, ctx, *, 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)
2019-02-24 09:13:10 +13:00
ctx.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:
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))
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-09 13:27:19 +12:00
def setup(bot):
bot.add_cog(Owner(bot))