Disable/enable commands, uptime fix

This commit is contained in:
Twentysix 2016-05-06 20:56:41 +02:00
parent 111ed83d89
commit 399ddeb886
2 changed files with 76 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import discord
from discord.ext import commands
from cogs.utils import checks
from __main__ import set_cog, send_cmd_help, settings
from .utils.dataIO import fileIO
import importlib
import traceback
@ -43,6 +44,7 @@ class Owner:
def __init__(self, bot):
self.bot = bot
self.setowner_lock = False
self.disabled_commands = fileIO("data/red/disabled_commands.json", "load")
@commands.command()
@checks.is_owner()
@ -282,6 +284,68 @@ class Owner:
"""Shuts down Red"""
await self.bot.logout()
@commands.group(name="command", pass_context=True)
@checks.is_owner()
async def command_disabler(self, ctx):
if ctx.invoked_subcommand is None:
await send_cmd_help(ctx)
if self.disabled_commands:
msg = "Disabled commands:\n```xl\n"
for cmd in self.disabled_commands:
msg += "{}, ".format(cmd)
msg = msg.strip(", ")
await self.bot.whisper("{}```".format(msg))
@command_disabler.command()
async def disable(self, *, command):
comm_obj = await self.get_command(command)
if comm_obj is KeyError:
await self.bot.say("That command doesn't seem to exist.")
elif comm_obj is False:
await self.bot.say("You cannot disable the commands of the owner cog.")
else:
comm_obj.enabled = False
comm_obj.hidden = True
self.disabled_commands.append(command)
fileIO("data/red/disabled_commands.json", "save", self.disabled_commands)
await self.bot.say("Command has been disabled.")
@command_disabler.command()
async def enable(self, *, command):
if command in self.disabled_commands:
self.disabled_commands.remove(command)
fileIO("data/red/disabled_commands.json", "save", self.disabled_commands)
await self.bot.say("Command enabled.")
else:
await self.bot.say("That command is not disabled.")
return
try:
comm_obj = await self.get_command(command)
comm_obj.enabled = True
comm_obj.hidden = False
except: # In case it was in the disabled list but not currently loaded
pass # No point in even checking what returns
async def get_command(self, command):
command = command.split()
try:
comm_obj = self.bot.commands[command[0]]
if len(command) > 1:
command.pop(0)
for cmd in command:
comm_obj = comm_obj.commands[cmd]
except KeyError:
return KeyError
if comm_obj.cog_name == "Owner":
return False
return comm_obj
async def disable_commands(self): # runs at boot
for cmd in self.disabled_commands:
cmd_obj = await self.get_command(cmd)
cmd_obj.enabled = False
cmd_obj.hidden = True
@commands.command()
@checks.is_owner()
async def join(self, invite_url: discord.Invite=None):
@ -448,7 +512,12 @@ class Owner:
return 'Last updated: ``{}``\nCommit: ``{}``\nHash: ``{}``'.format(
*version)
def check_files():
if not os.path.isfile("data/red/disabled_commands.json"):
print("Creating empty disabled_commands.json...")
fileIO("data/red/disabled_commands.json", "save", [])
def setup(bot):
check_files()
n = Owner(bot)
bot.add_cog(n)
bot.add_cog(n)

8
red.py
View File

@ -38,7 +38,8 @@ async def on_ready():
users = str(len(set(bot.get_all_members())))
servers = str(len(bot.servers))
channels = str(len([c for c in bot.get_all_channels()]))
bot.uptime = int(time.perf_counter())
if not "uptime" in dir(bot): #prevents reset in case of reconnection
bot.uptime = int(time.perf_counter())
print('------')
print(bot.user.name + " is now online.")
print('------')
@ -55,6 +56,7 @@ async def on_ready():
bot.oauth_url = url
print(url)
print("------")
await bot.get_cog('Owner').disable_commands()
@bot.event
@ -74,6 +76,8 @@ async def on_command_error(error, ctx):
await send_cmd_help(ctx)
elif isinstance(error, commands.BadArgument):
await send_cmd_help(ctx)
elif isinstance(error, commands.DisabledCommand):
await bot.send_message(ctx.message.channel, "That command is disabled.")
async def send_cmd_help(ctx):
if ctx.invoked_subcommand:
@ -379,4 +383,4 @@ if __name__ == '__main__':
logger.error(traceback.format_exc())
loop.run_until_complete(bot.logout())
finally:
loop.close()
loop.close()