Basic Admin, Utils and Git

This commit is contained in:
Dusty.P 2018-05-05 19:04:41 -08:00
parent 4eddf2aabd
commit e52ebf2a0a
3 changed files with 237 additions and 2 deletions

View File

@ -1,3 +1,141 @@
import discord
from discord.ext import commands
import os
from .imports import checks, utils
import json
import logging
admin_log = logging.getLogger('admin')
config_dir = 'config/'
bot_config_file = 'bot_config.json'
class Admin:
def __init__(self, bot):
self.bot = bot
@commands.group(case_insensitive=True)
async def set(self, ctx):
"""Run help set for more info"""
pass
@commands.group(case_insensitive=True)
async def add(self, ctx):
"""Run help set for more info"""
pass
@commands.group(case_insensitive=True)
async def remove(self, ctx):
"""Run help set for more info"""
pass
@commands.command(hidden=True)
@commands.check(checks.is_guild_owner)
async def get_guild_config(self, ctx):
config = self.bot.con.one('select * from guild_config where guild_id = %(id)s', {'id': ctx.guild.id})
configs = [str(config)[i:i+1990] for i in range(0, len(config), 1990)]
await ctx.message.author.send(f'The current config for the {ctx.guild.name} guild is:\n')
admin_log.info(configs)
for config in configs:
await ctx.message.author.send(f'```{config}```')
await ctx.send(f'{ctx.message.author.mention} check your DMs.')
@commands.command(hidden=True)
@commands.is_owner()
async def get_bot_config(self, ctx):
n = 2000
config = [str(self.bot.bot_config)[i:i+n] for i in range(0, len(str(self.bot.bot_config)), n)]
for conf in config:
await ctx.message.author.send(conf)
await ctx.send(f'{ctx.message.author.mention} check your DMs.')
@commands.command(hidden=True)
@commands.is_owner()
async def reload_bot_config(self, ctx):
with open(f'{config_dir}{bot_config_file}') as file:
self.bot.bot_config = json.load(file)
del self.bot.bot_config['token']
del self.bot.bot_config['db_con']
await ctx.send('Config reloaded.')
@set.command(name='channel_lockdown', aliases=['lockdown', 'restrict_access', 'cl'])
async def _channel_lockdown(self, ctx, config='true'):
if ctx.guild:
if checks.is_admin(self.bot, ctx):
if str(config).lower() == 'true':
if self.bot.con.one('select allowed_channels from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}) is []:
await ctx.send('Please set at least one allowed channel before running this command.')
else:
self.bot.con.run('update guild_config set channel_lockdown = True where guild_id = %(id)s',
{'id': ctx.guild.id})
await ctx.send('Channel Lockdown is now active.')
elif str(config).lower() == 'false':
if self.bot.con.one('select channel_lockdown from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}):
self.bot.con.run('update guild_config set channel_lockdown = False where guild_id = %(id)s',
{'id': ctx.guild.id})
await ctx.send('Channel Lockdown has been deactivated.')
else:
await ctx.send('Channel Lockdown is already deactivated.')
else:
await ctx.send(f'You are not authorized to run this command.')
else:
await ctx.send('This command must be run from inside a guild.')
@add.command(name='allowed_channels', aliases=['channel', 'ac'])
async def _allowed_channels(self, ctx, *, channels):
if ctx.guild:
if checks.is_admin(self.bot, ctx):
channels = channels.lower().replace(' ', '').split(',')
added = ''
for channel in channels:
chnl = discord.utils.get(ctx.guild.channels, name=channel)
if chnl is None:
await ctx.send(f'{channel} is not a valid text channel in this guild.')
else:
admin_log.info('Chan found')
if self.bot.con.one('select allowed_channels from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}):
if chnl.id in json.loads(self.bot.con.one('select allowed_channels from guild_config '
'where guild_id = %(id)s',
{'id': ctx.guild.id})):
admin_log.info('Chan found in config')
await ctx.send(f'{channel} is already in the list of allowed channels. Skipping...')
else:
admin_log.info('Chan not found in config')
allowed_channels = json.loads(self.bot.con.one('select allowed_channels from '
'guild_config where guild_id = %(id)s',
{'id': ctx.guild.id})).append(chnl.id)
self.bot.con.run('update guild_config set allowed_channels = %(channels)s '
'where guild_id = %(id)s',
{'id': ctx.guild.id, 'channels': allowed_channels})
added = f'{added}\n{channel}'
else:
admin_log.info('Chan not found in config')
allowed_channels = [chnl.id]
self.bot.con.run('update guild_config set allowed_channels = %(channels)s '
'where guild_id = %(id)s',
{'id': ctx.guild.id, 'channels': allowed_channels})
added = f'{added}\n{channel}'
if added != '':
await ctx.send(f'The following channels have been added to the allowed channel list: {added}')
await ctx.message.add_reaction('')
else:
await ctx.send(f'You are not authorized to run this command.')
else:
await ctx.send('This command must be run from inside a guild.')
@commands.command()
@commands.is_owner()
async def view_code(self, ctx, code_name):
pages = utils.paginate(inspect.getsource(self.bot.get_command(code_name).callback))
for page in pages:
await ctx.send(page)
def setup(bot):
bot.add_cog(Admin(bot))

53
exts/git.py Normal file
View File

@ -0,0 +1,53 @@
import discord
from discord.ext import commands
import logging
from .imports.utils import paginate, run_command
import asyncio
git_log = logging.getLogger('git')
class Git:
def __init__(self, bot):
self.bot = bot
@commands.group(case_insensitive=True)
async def git(self, ctx):
"""Run help git for more info"""
pass
@git.command()
@commands.is_owner()
async def pull(self, ctx):
em = discord.Embed(style='rich',
title=f'Git Pull',
color=embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
result = await asyncio.wait_for(self.bot.loop.create_task(run_command('git fetch --all')), 120) + '\n'
result += await asyncio.wait_for(self.bot.loop.create_task(run_command('git reset --hard '
'origin/$(git '
'rev-parse --symbolic-full-name'
' --abbrev-ref HEAD)')), 120) + '\n\n'
result += await asyncio.wait_for(self.bot.loop.create_task(run_command('git show --stat | '
'sed "s/.*@.*[.].*/ /g"')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='', value=f'{page}')
await ctx.send(embed=em)
@git.command()
@commands.is_owner()
async def status(self, ctx):
em = discord.Embed(style='rich',
title=f'Git Pull',
color=embed_color)
em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}')
result = await asyncio.wait_for(self.bot.loop.create_task(run_command('git status')), 10)
results = paginate(result, maxlen=1014)
for page in results[:5]:
em.add_field(name='', value=f'{page}')
await ctx.send(embed=em)
def setup(bot):
bot.add_cog(Git(bot))

View File

@ -1,3 +1,47 @@
import discord
from discord.ext import commands
import os
import math
import psutil
class Utils:
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.cooldown(1, 5, type=commands.BucketType.user)
async def ping(self, ctx):
"""Check the Bot\'s connection to Discord"""
em = discord.Embed(style='rich',
title=f'Pong 🏓',
color=discord.Colour.green()
)
msg = await ctx.send(embed=em)
time1 = ctx.message.created_at
time = (msg.created_at - time1).total_seconds() * 1000
em.description = f'''Response Time: **{math.ceil(time)}ms**
Discord Latency: **{math.ceil(self.bot.latency*1000)}ms**'''
await msg.edit(embed=em)
@commands.command(aliases=['oauth', 'link'])
@commands.cooldown(1, 5, type=commands.BucketType.user)
async def invite(self, ctx, guy: discord.User=None):
"""Shows you the bot's invite link.
If you pass in an ID of another bot, it gives you the invite link to that bot.
"""
guy = guy or self.bot.user
url = discord.utils.oauth_url(guy.id)
await ctx.send(f'**{url}**')
@commands.command()
@commands.is_owner()
async def sysinfo(self, ctx):
await ctx.send(f'```ml\n'
f'CPU Percentages: {psutil.cpu_percent(percpu=True)}\n'
f'Memory Usage: {psutil.virtual_memory().percent}%\n'
f'Disc Usage: {psutil.disk_usage("/").percent}%\n'
f'```')
def setup(bot):
bot.add_cog(Utils(bot))