1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 21:02:38 +00:00

Moved owner commands to Bot class, undid unpythonic formatting

This commit is contained in:
Myned 2017-10-12 22:26:57 -04:00
parent 2bd2808bfc
commit 25afa93aa3

View file

@ -1,37 +1,93 @@
import asyncio import asyncio
import code import code
import io import io
import pyrasite as pyr import os
import re import re
import sys import sys
import traceback as tb import traceback as tb
import discord as d import discord as d
import pyrasite as pyr
from discord.ext import commands from discord.ext import commands
from misc import checks
from misc import exceptions as exc from misc import exceptions as exc
from misc import checks
from utils import utils as u
nl = re.compile('\n') nl = re.compile('\n')
class Bot:
def __init__(self, bot, config):
self.bot = bot
self.config = config
# Close connection to Discord - immediate offline
@commands.command(name=',die', aliases=[',d'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True)
@commands.is_owner()
@checks.del_ctx()
async def die(self, ctx):
if isinstance(self.bot.get_channel(self.config['startup_channel']), d.TextChannel):
await self.bot.get_channel(self.config['shutdown_channel']).send('**Shutting down...** 🌙')
# loop = self.bot.loop.all_tasks()
# for task in loop:
# task.cancel()
await u.session.close()
await self.bot.logout()
await self.bot.close()
print('-------')
print('CLOSED')
@commands.command(name=',restart', aliases=[',res', ',r'], hidden=True)
@commands.is_owner()
@checks.del_ctx()
async def restart(self, ctx):
print('RESTARTING')
print('-------')
if isinstance(self.bot.get_channel(self.config['startup_channel']), d.TextChannel):
await self.bot.get_channel(self.config['shutdown_channel']).send('**Restarting...** 💤')
# loop = self.bot.loop.all_tasks()
# for task in loop:
# task.cancel()
await u.session.close()
await self.bot.logout()
await self.bot.close()
os.execl(sys.executable, 'python3', 'run.py')
# Invite bot to bot owner's server
@commands.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True)
@commands.is_owner()
@checks.del_ctx()
async def invite(self, ctx):
await ctx.send('🔗 https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}'.format(self.config['client_id'], self.config['permissions']), delete_after=10)
class Tools: class Tools:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
def format(self, i='', o=''): def format(self, i='', o=''):
if len(o) > 1: return '>>> {}\n{}'.format(i, o) if len(o) > 1:
else: return '>>> {}'.format(i) return '>>> {}\n{}'.format(i, o)
else:
return '>>> {}'.format(i)
async def generate(self, d, i='', o=''): async def generate(self, d, i='', o=''):
return await d.send('```python\n{}```'.format(self.format(i, o))) return await d.send('```python\n{}```'.format(self.format(i, o)))
async def refresh(self, m, i='', o=''): async def refresh(self, m, i='', o=''):
global nl global nl
output = m.content[10:-3] output = m.content[10:-3]
if len(nl.findall(output)) <= 20: await m.edit(content='```python\n{}\n{}\n>>>```'.format(output, self.format(i, o))) if len(nl.findall(output)) <= 20:
else: await m.edit(content='```python\n{}```'.format(self.format(i, o))) await m.edit(content='```python\n{}\n{}\n>>>```'.format(output, self.format(i, o)))
else:
await m.edit(content='```python\n{}```'.format(self.format(i, o)))
async def generate_err(self, d, o=''): async def generate_err(self, d, o=''):
return await d.send('```\n{}```'.format(o)) return await d.send('```\n{}```'.format(o))
async def refresh_err(self, m, o=''): async def refresh_err(self, m, o=''):
await m.edit(content='```\n{}```'.format(o)) await m.edit(content='```\n{}```'.format(o))
@ -42,8 +98,10 @@ class Tools:
def execute(msg): def execute(msg):
if msg.content == ',exit' and msg.author is ctx.message.author: if msg.content == ',exit' and msg.author is ctx.message.author:
raise exc.CheckFail raise exc.CheckFail
elif msg.author is ctx.message.author and msg.channel is ctx.message.channel: return True elif msg.author is ctx.message.author and msg.channel is ctx.message.channel:
else: return False return True
else:
return False
try: try:
console = await self.generate(ctx) console = await self.generate(ctx)
@ -53,8 +111,10 @@ class Tools:
await exe.delete() await exe.delete()
sys.stdout = io.StringIO() sys.stdout = io.StringIO()
sys.stderr = io.StringIO() sys.stderr = io.StringIO()
try: exec(exe.content) try:
except Exception: tb.print_exc(limit=1) exec(exe.content)
except Exception:
tb.print_exc(limit=1)
await self.refresh(console, exe.content, sys.stdout.getvalue()) await self.refresh(console, exe.content, sys.stdout.getvalue())
await self.refresh_err(exception, sys.stderr.getvalue()) await self.refresh_err(exception, sys.stderr.getvalue())
await ctx.send(console.content[10:-3]) await ctx.send(console.content[10:-3])
@ -62,9 +122,6 @@ class Tools:
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
except exc.CheckFail: except exc.CheckFail:
await ctx.send('↩️ **Exited console.**') await ctx.send('↩️ **Exited console.**')
except Exception:
await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1)))
tb.print_exc(limit=1, file=sys.__stderr__)
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
@ -79,7 +136,7 @@ class Tools:
exec(exe) exec(exe)
await self.generate(ctx, exe, sys.stdout.getvalue()) await self.generate(ctx, exe, sys.stdout.getvalue())
except Exception: except Exception:
await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) await ctx.send('```\n{}```'.format(tb.format_exc(limit=1)))
tb.print_exc(limit=1) tb.print_exc(limit=1)
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
@ -90,9 +147,11 @@ class Tools:
@checks.del_ctx() @checks.del_ctx()
async def debug(self, ctx): async def debug(self, ctx):
console = await self.generate(ctx) console = await self.generate(ctx)
@debug.command(name='inject', aliases=['inj']) @debug.command(name='inject', aliases=['inj'])
async def _inject(self, ctx, *, input_): async def _inject(self, ctx, *, input_):
pass pass
@debug.command(name='inspect', aliases=['ins']) @debug.command(name='inspect', aliases=['ins'])
async def _inspect(self, ctx, *, input_): async def _inspect(self, ctx, *, input_):
pass pass