1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-24 14:27:27 +00:00

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Myned 2018-03-31 16:37:32 -04:00
commit 8da2dd37ae
2 changed files with 1906 additions and 1906 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,245 +1,245 @@
import asyncio import asyncio
import code import code
import io import io
import os import os
import re import re
import sys import sys
import traceback as tb import traceback as tb
from contextlib import redirect_stdout, suppress from contextlib import redirect_stdout, suppress
import discord as d import discord as d
import pyrasite as pyr import pyrasite as pyr
from discord.ext import commands as cmds from discord.ext import commands as cmds
from misc import exceptions as exc from misc import exceptions as exc
from misc import checks from misc import checks
from utils import utils as u from utils import utils as u
class Bot: class Bot:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
# Close connection to Discord - immediate offline # Close connection to Discord - immediate offline
@cmds.command(name=',die', aliases=[',d'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True) @cmds.command(name=',die', aliases=[',d'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def die(self, ctx): async def die(self, ctx):
await ctx.message.add_reaction('\N{CRESCENT MOON}') await ctx.message.add_reaction('\N{CRESCENT MOON}')
await self.bot.get_channel(u.config['info_channel']).send('**Shutting down** \N{CRESCENT MOON} . . .') await self.bot.get_channel(u.config['info_channel']).send('**Shutting down** \N{CRESCENT MOON} . . .')
chantype = 'guild' if isinstance(ctx.channel, d.TextChannel) else 'private' chantype = 'guild' if isinstance(ctx.channel, d.TextChannel) else 'private'
u.temp['startup'] = (chantype, ctx.channel.id if chantype == 'guild' else ctx.author.id, ctx.message.id) u.temp['startup'] = (chantype, ctx.channel.id if chantype == 'guild' else ctx.author.id, ctx.message.id)
u.dump(u.temp, 'temp/temp.pkl') u.dump(u.temp, 'temp/temp.pkl')
# loop = self.bot.loop.all_tasks() # loop = self.bot.loop.all_tasks()
# for task in loop: # for task in loop:
# task.cancel() # task.cancel()
await self.bot.logout() await self.bot.logout()
u.close(self.bot.loop) u.close(self.bot.loop)
print('\n< < < < < < < < < < < <\nD I S C O N N E C T E D\n< < < < < < < < < < < <\n') print('\n< < < < < < < < < < < <\nD I S C O N N E C T E D\n< < < < < < < < < < < <\n')
# u.notify('D I S C O N N E C T E D') # u.notify('D I S C O N N E C T E D')
@cmds.command(name=',restart', aliases=[',res', ',r'], hidden=True) @cmds.command(name=',restart', aliases=[',res', ',r'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def restart(self, ctx): async def restart(self, ctx):
await ctx.message.add_reaction('\N{SLEEPING SYMBOL}') await ctx.message.add_reaction('\N{SLEEPING SYMBOL}')
print('\n^ ^ ^ ^ ^ ^ ^ ^ ^ ^\nR E S T A R T I N G\n^ ^ ^ ^ ^ ^ ^ ^ ^ ^\n') print('\n^ ^ ^ ^ ^ ^ ^ ^ ^ ^\nR E S T A R T I N G\n^ ^ ^ ^ ^ ^ ^ ^ ^ ^\n')
await self.bot.get_channel(u.config['info_channel']).send('**Restarting** \N{SLEEPING SYMBOL} . . .') await self.bot.get_channel(u.config['info_channel']).send('**Restarting** \N{SLEEPING SYMBOL} . . .')
# u.notify('R E S T A R T I N G') # u.notify('R E S T A R T I N G')
chantype = 'guild' if isinstance(ctx.channel, d.TextChannel) else 'private' chantype = 'guild' if isinstance(ctx.channel, d.TextChannel) else 'private'
u.temp['startup'] = (chantype, ctx.channel.id if chantype == 'guild' else ctx.author.id, ctx.message.id) u.temp['startup'] = (chantype, ctx.channel.id if chantype == 'guild' else ctx.author.id, ctx.message.id)
u.dump(u.temp, 'temp/temp.pkl') u.dump(u.temp, 'temp/temp.pkl')
# loop = self.bot.loop.all_tasks() # loop = self.bot.loop.all_tasks()
# for task in loop: # for task in loop:
# task.cancel() # task.cancel()
await self.bot.logout() await self.bot.logout()
u.close(self.bot.loop) u.close(self.bot.loop)
os.execl(sys.executable, 'python3', 'run.py') os.execl(sys.executable, 'python3', 'run.py')
# Invite bot to bot owner's server # Invite bot to bot owner's server
@cmds.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True) @cmds.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def invite(self, ctx): async def invite(self, ctx):
await ctx.message.add_reaction('\N{ENVELOPE}') await ctx.message.add_reaction('\N{ENVELOPE}')
await ctx.send('https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}'.format(u.config['client_id'], u.config['permissions']), delete_after=5) await ctx.send('https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}'.format(u.config['client_id'], u.config['permissions']), delete_after=5)
@cmds.command(name=',guilds', aliases=[',glds', ',servers', ',servs']) @cmds.command(name=',guilds', aliases=[',glds', ',servers', ',svrs'])
@cmds.is_owner() @cmds.is_owner()
async def guilds(self, ctx): async def guilds(self, ctx):
paginator = cmds.Paginator() paginator = cmds.Paginator()
for guild in self.bot.guilds: for guild in self.bot.guilds:
paginator.add_line(guild.name) paginator.add_line(guild.name)
for page in paginator.pages: for page in paginator.pages:
await ctx.send(f'**Guilds:**\n{page}') await ctx.send(f'**Guilds:**\n{page}')
@cmds.command(name=',status', aliases=[',presence', ',game'], hidden=True) @cmds.command(name=',status', aliases=[',presence', ',game'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def change_status(self, ctx, *, game=None): async def change_status(self, ctx, *, game=None):
if game: if game:
await self.bot.change_presence(game=d.Game(name=game)) await self.bot.change_presence(game=d.Game(name=game))
u.config['playing'] = game u.config['playing'] = game
u.dump(u.config, 'config.json', json=True) u.dump(u.config, 'config.json', json=True)
await ctx.send(f'**Game changed to** `{game}`') await ctx.send(f'**Game changed to** `{game}`')
else: else:
await self.bot.change_presence(game=None) await self.bot.change_presence(game=None)
u.config['playing'] = '' u.config['playing'] = ''
u.dump(u.config, 'config.json', json=True) u.dump(u.config, 'config.json', json=True)
await ctx.send('**Game changed to** ` `') await ctx.send('**Game changed to** ` `')
@cmds.command(name=',username', aliases=[',user'], hidden=True) @cmds.command(name=',username', aliases=[',user'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def change_username(self, ctx, *, username=None): async def change_username(self, ctx, *, username=None):
if username: if username:
await self.bot.user.edit(username=username) await self.bot.user.edit(username=username)
await ctx.send(f'**Username changed to** `{username}`') await ctx.send(f'**Username changed to** `{username}`')
else: else:
await ctx.send('**Invalid string**', delete_after=7) await ctx.send('**Invalid string**', delete_after=7)
await ctx.message.add_reaction('\N{CROSS MARK}') await ctx.message.add_reaction('\N{CROSS MARK}')
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: if len(o) > 1:
return '>>> {}\n{}'.format(i, o) return '>>> {}\n{}'.format(i, o)
else: else:
return '>>> {}'.format(i) 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=''):
output = m.content[9:-3] output = m.content[9:-3]
if len(re.findall('\n', output)) <= 20: if len(re.findall('\n', output)) <= 20:
await m.edit(content='```python\n{}\n{}\n>>>```'.format(output, self.format(i, o))) await m.edit(content='```python\n{}\n{}\n>>>```'.format(output, self.format(i, o)))
else: else:
await m.edit(content='```python\n{}```'.format(self.format(i, o))) 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))
@cmds.command(name=',console', aliases=[',con', ',c'], hidden=True) @cmds.command(name=',console', aliases=[',con', ',c'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def console(self, ctx): async def console(self, ctx):
def execute(msg): def execute(msg):
if msg.content.lower().startswith('exec ') and msg.author is ctx.author and msg.channel is ctx.channel: if msg.content.lower().startswith('exec ') and msg.author is ctx.author and msg.channel is ctx.channel:
msg.content = msg.content[5:] msg.content = msg.content[5:]
return True return True
return False return False
def evaluate(msg): def evaluate(msg):
if msg.content.lower().startswith('eval ') and msg.author is ctx.author and msg.channel is ctx.channel: if msg.content.lower().startswith('eval ') and msg.author is ctx.author and msg.channel is ctx.channel:
msg.content = msg.content[5:] msg.content = msg.content[5:]
return True return True
return False return False
def exit(reaction, user): def exit(reaction, user):
if reaction.emoji == '\N{OCTAGONAL SIGN}' and user is ctx.author and reaction.message.id == ctx.message.id: if reaction.emoji == '\N{OCTAGONAL SIGN}' and user is ctx.author and reaction.message.id == ctx.message.id:
raise exc.Abort raise exc.Abort
return False return False
try: try:
console = await self.generate(ctx) console = await self.generate(ctx)
exception = await self.generate_err(ctx) exception = await self.generate_err(ctx)
await ctx.message.add_reaction('\N{OCTAGONAL SIGN}') await ctx.message.add_reaction('\N{OCTAGONAL SIGN}')
while not self.bot.is_closed(): while not self.bot.is_closed():
try: try:
done, pending = await asyncio.wait([self.bot.wait_for('message', check=execute), self.bot.wait_for('message', check=evaluate), self.bot.wait_for('reaction_add', check=exit)], return_when=asyncio.FIRST_COMPLETED) done, pending = await asyncio.wait([self.bot.wait_for('message', check=execute), self.bot.wait_for('message', check=evaluate), self.bot.wait_for('reaction_add', check=exit)], return_when=asyncio.FIRST_COMPLETED)
message = done.pop().result() message = done.pop().result()
print(message.content) print(message.content)
except exc.Execute: except exc.Execute:
try: try:
sys.stdout = io.StringIO() sys.stdout = io.StringIO()
sys.stderr = io.StringIO() sys.stderr = io.StringIO()
exec(message.content) exec(message.content)
except Exception: except Exception:
await self.refresh_err(exception, tb.format_exc(limit=1)) await self.refresh_err(exception, tb.format_exc(limit=1))
finally: finally:
await self.refresh(console, message.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None) await self.refresh(console, message.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None)
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
with suppress(d.NotFound): with suppress(d.NotFound):
await message.delete() await message.delete()
except exc.Evaluate: except exc.Evaluate:
try: try:
sys.stdout = io.StringIO() sys.stdout = io.StringIO()
sys.stderr = io.StringIO() sys.stderr = io.StringIO()
eval(message.content) eval(message.content)
except Exception: except Exception:
await self.refresh_err(exception, tb.format_exc(limit=1)) await self.refresh_err(exception, tb.format_exc(limit=1))
finally: finally:
await self.refresh(console, message.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None) await self.refresh(console, message.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None)
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
with suppress(d.NotFound): with suppress(d.NotFound):
await message.delete() await message.delete()
except exc.Abort: except exc.Abort:
pass pass
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
print('RESET : sys.std output/error') print('RESET : sys.std output/error')
@cmds.command(name=',execute', aliases=[',exec'], hidden=True) @cmds.command(name=',execute', aliases=[',exec'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def execute(self, ctx, *, exe): async def execute(self, ctx, *, exe):
try: try:
with io.StringIO() as buff, redirect_stdout(buff): with io.StringIO() as buff, redirect_stdout(buff):
exec(exe) exec(exe)
await self.generate(ctx, exe, f'\n{buff.getvalue()}') await self.generate(ctx, exe, f'\n{buff.getvalue()}')
except Exception: except Exception:
await self.generate(ctx, exe, f'\n{tb.format_exc()}') await self.generate(ctx, exe, f'\n{tb.format_exc()}')
@cmds.command(name=',evaluate', aliases=[',eval'], hidden=True) @cmds.command(name=',evaluate', aliases=[',eval'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
async def evaluate(self, ctx, *, evl): async def evaluate(self, ctx, *, evl):
try: try:
with io.StringIO() as buff, redirect_stdout(buff): with io.StringIO() as buff, redirect_stdout(buff):
eval(evl) eval(evl)
await self.generate(ctx, evl, f'\n{buff.getvalue()}') await self.generate(ctx, evl, f'\n{buff.getvalue()}')
except Exception: except Exception:
await self.generate(ctx, evl, f'\n{tb.format_exc()}') await self.generate(ctx, evl, f'\n{tb.format_exc()}')
@cmds.group(aliases=[',db'], hidden=True) @cmds.group(aliases=[',db'], hidden=True)
@cmds.is_owner() @cmds.is_owner()
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
# @cmds.command(name='endpoint', aliases=['end']) # @cmds.command(name='endpoint', aliases=['end'])
# async def get_endpoint(self, ctx, *args): # async def get_endpoint(self, ctx, *args):
# await ctx.send(f'```\n{await u.fetch(f"https://{args[0]}/{args[1]}/{args[2]}", params={args[3]: args[4], "limit": 1}, json=True)}```') # await ctx.send(f'```\n{await u.fetch(f"https://{args[0]}/{args[1]}/{args[2]}", params={args[3]: args[4], "limit": 1}, json=True)}```')