[CC] Moved commands to [p]customcom command group

This commit is contained in:
Twentysix 2017-04-05 22:07:43 +02:00
parent f68667b628
commit 7a38da9152

View File

@ -7,27 +7,38 @@ import re
class CustomCommands: class CustomCommands:
"""Custom commands.""" """Custom commands
Creates commands used to display text"""
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.file_path = "data/customcom/commands.json" self.file_path = "data/customcom/commands.json"
self.c_commands = dataIO.load_json(self.file_path) self.c_commands = dataIO.load_json(self.file_path)
@commands.command(pass_context=True, no_pm=True) @commands.group(aliases=["cc"], pass_context=True, no_pm=True)
@checks.mod_or_permissions(administrator=True) @checks.mod_or_permissions(administrator=True)
async def addcom(self, ctx, command : str, *, text): async def customcom(self, ctx):
"""Custom commands management"""
if ctx.invoked_subcommand is None:
await self.bot.send_cmd_help(ctx)
@customcom.command(name="add", pass_context=True)
async def cc_add(self, ctx, command : str, *, text):
"""Adds a custom command """Adds a custom command
Example: Example:
!addcom yourcommand Text you want [p]customcom add yourcommand Text you want
CCs can be enhanced with arguments:
https://twentysix26.github.io/Red-Docs/red_guide_command_args/
""" """
server = ctx.message.server server = ctx.message.server
command = command.lower() command = command.lower()
if command in self.bot.commands.keys(): if command in self.bot.commands:
await self.bot.say("That command is already a standard command.") await self.bot.say("That command is already a standard command.")
return return
if not server.id in self.c_commands: if server.id not in self.c_commands:
self.c_commands[server.id] = {} self.c_commands[server.id] = {}
cmdlist = self.c_commands[server.id] cmdlist = self.c_commands[server.id]
if command not in cmdlist: if command not in cmdlist:
@ -36,15 +47,16 @@ class CustomCommands:
dataIO.save_json(self.file_path, self.c_commands) dataIO.save_json(self.file_path, self.c_commands)
await self.bot.say("Custom command successfully added.") await self.bot.say("Custom command successfully added.")
else: else:
await self.bot.say("This command already exists. Use editcom to edit it.") await self.bot.say("This command already exists. Use "
"`{}customcom edit` to edit it."
"".format(ctx.prefix))
@commands.command(pass_context=True, no_pm=True) @customcom.command(name="edit", pass_context=True)
@checks.mod_or_permissions(administrator=True) async def cc_edit(self, ctx, command : str, *, text):
async def editcom(self, ctx, command : str, *, text):
"""Edits a custom command """Edits a custom command
Example: Example:
!editcom yourcommand Text you want [p]customcom edit yourcommand Text you want
""" """
server = ctx.message.server server = ctx.message.server
command = command.lower() command = command.lower()
@ -56,17 +68,20 @@ class CustomCommands:
dataIO.save_json(self.file_path, self.c_commands) dataIO.save_json(self.file_path, self.c_commands)
await self.bot.say("Custom command successfully edited.") await self.bot.say("Custom command successfully edited.")
else: else:
await self.bot.say("That command doesn't exist. Use addcom [command] [text]") await self.bot.say("That command doesn't exist. Use "
"`{}customcom add` to add it."
"".format(ctx.prefix))
else: else:
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]") await self.bot.say("There are no custom commands in this server."
" Use `{}customcom add` to start adding some."
"".format(ctx.prefix))
@commands.command(pass_context=True, no_pm=True) @customcom.command(name="delete", pass_context=True)
@checks.mod_or_permissions(administrator=True) async def cc_delete(self, ctx, command : str):
async def delcom(self, ctx, command : str):
"""Deletes a custom command """Deletes a custom command
Example: Example:
!delcom yourcommand""" [p]customcom delete yourcommand"""
server = ctx.message.server server = ctx.message.server
command = command.lower() command = command.lower()
if server.id in self.c_commands: if server.id in self.c_commands:
@ -79,10 +94,12 @@ class CustomCommands:
else: else:
await self.bot.say("That command doesn't exist.") await self.bot.say("That command doesn't exist.")
else: else:
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]") await self.bot.say("There are no custom commands in this server."
" Use `{}customcom add` to start adding some."
"".format(ctx.prefix))
@commands.command(pass_context=True, no_pm=True) @customcom.command(name="list", pass_context=True)
async def customcommands(self, ctx): async def cc_list(self, ctx):
"""Shows custom commands list""" """Shows custom commands list"""
server = ctx.message.server server = ctx.message.server
if server.id in self.c_commands: if server.id in self.c_commands:
@ -90,7 +107,7 @@ class CustomCommands:
if cmdlist: if cmdlist:
i = 0 i = 0
msg = ["```Custom commands:\n"] msg = ["```Custom commands:\n"]
for cmd in sorted([cmd for cmd in cmdlist.keys()]): for cmd in sorted([cmd for cmd in cmdlist]):
if len(msg[i]) + len(ctx.prefix) + len(cmd) + 5 > 2000: if len(msg[i]) + len(ctx.prefix) + len(cmd) + 5 > 2000:
msg[i] += "```" msg[i] += "```"
i += 1 i += 1
@ -101,11 +118,15 @@ class CustomCommands:
for cmds in msg: for cmds in msg:
await self.bot.whisper(cmds) await self.bot.whisper(cmds)
else: else:
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]") await self.bot.say("There are no custom commands in this server."
" Use `{}customcom add` to start adding some."
"".format(ctx.prefix))
else: else:
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]") await self.bot.say("There are no custom commands in this server."
" Use `{}customcom add` to start adding some."
"".format(ctx.prefix))
async def checkCC(self, message): async def on_message(self, message):
if len(message.content) < 2 or message.channel.is_private: if len(message.content) < 2 or message.channel.is_private:
return return
@ -118,11 +139,11 @@ class CustomCommands:
if server.id in self.c_commands and user_allowed(message): if server.id in self.c_commands and user_allowed(message):
cmdlist = self.c_commands[server.id] cmdlist = self.c_commands[server.id]
cmd = message.content[len(prefix):] cmd = message.content[len(prefix):]
if cmd in cmdlist.keys(): if cmd in cmdlist:
cmd = cmdlist[cmd] cmd = cmdlist[cmd]
cmd = self.format_cc(cmd, message) cmd = self.format_cc(cmd, message)
await self.bot.send_message(message.channel, cmd) await self.bot.send_message(message.channel, cmd)
elif cmd.lower() in cmdlist.keys(): elif cmd.lower() in cmdlist:
cmd = cmdlist[cmd.lower()] cmd = cmdlist[cmd.lower()]
cmd = self.format_cc(cmd, message) cmd = self.format_cc(cmd, message)
await self.bot.send_message(message.channel, cmd) await self.bot.send_message(message.channel, cmd)
@ -170,15 +191,15 @@ def check_folders():
print("Creating data/customcom folder...") print("Creating data/customcom folder...")
os.makedirs("data/customcom") os.makedirs("data/customcom")
def check_files(): def check_files():
f = "data/customcom/commands.json" f = "data/customcom/commands.json"
if not dataIO.is_valid_json(f): if not dataIO.is_valid_json(f):
print("Creating empty commands.json...") print("Creating empty commands.json...")
dataIO.save_json(f, {}) dataIO.save_json(f, {})
def setup(bot): def setup(bot):
check_folders() check_folders()
check_files() check_files()
n = CustomCommands(bot) bot.add_cog(CustomCommands(bot))
bot.add_listener(n.checkCC, "on_message")
bot.add_cog(n)