You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.7KB

  1. import discord
  2. from discord.ext.commands import group
  3. from config.server_config import ServerConfig
  4. import load_config
  5. # TODO: Sort out admin commands, mod commands, the settings before ever pushing this to general use. It needs to be a mod only thing.
  6. class CustomCommands():
  7. def __init__(self, Bot):
  8. self.bot = Bot
  9. self.con = ServerConfig()
  10. self.servers = self.con.servers
  11. async def on_message(self, message):
  12. msg = message.content.lower()
  13. channel = message.channel
  14. server = message.server.id
  15. if message.author == self.bot.user:
  16. return
  17. if msg.startswith(self.bot.command_prefix):
  18. if msg.split(self.bot.command_prefix)[1] in self.servers[server]["custom_commands"]["1"]:
  19. return await self.bot.send_message(channel, self.servers[server]["custom_commands"]["1"][msg.split(self.bot.command_prefix)[1]])
  20. elif len(msg.split(" ")) < 2:
  21. if msg.split(" ")[0] in self.servers[server]["custom_commands"]["0"]:
  22. return await self.bot.send_message(channel, self.servers[server]["custom_commands"]["0"][msg.split(" ")[0]])
  23. @group(pass_context=True, aliases=["cc"])
  24. async def custom(self, ctx):
  25. if ctx.invoked_subcommand is None:
  26. return await self.bot.say('Missing Argument')
  27. @custom.command(pass_context=True)
  28. async def add(self, ctx, command, output, prefix_required = "0"):
  29. command = command.lower()
  30. output = output.lower()
  31. zero = self.servers[ctx.message.server.id]["custom_commands"]["0"]
  32. one = self.servers[ctx.message.server.id]["custom_commands"]["1"]
  33. if ctx.message.mentions:
  34. return await self.bot.say("Custom Commands cannot mention people.")
  35. elif len(output) > 1999: # This probably wont happen atm since the command itself would make the whole message over len 2000 which would be impossible to send. But this is here incase we need to adjust this number.
  36. return await self.bot.say("The output is too long")
  37. elif command in self.bot.commands and prefix_required == "1":
  38. return await self.bot.say("This is already the name of a built in command.")
  39. elif command in zero or command in one:
  40. return await self.bot.say("Custom Command already exists.")
  41. elif prefix_required != "1" and prefix_required != "0":
  42. return await self.bot.say("No prefix setting set.")
  43. self.servers[ctx.message.server.id]["custom_commands"][prefix_required][command] = output
  44. self.con.update_config(self.servers)
  45. return await self.bot.say("{} has been added with the output: '{}'".format(command, output))
  46. @custom.command(pass_context=True)
  47. async def edit(self, ctx, command, edit):
  48. zero = self.servers[ctx.message.server.id]["custom_commands"]["0"]
  49. one = self.servers[ctx.message.server.id]["custom_commands"]["1"]
  50. if ctx.message.mentions:
  51. return await self.bot.say("Custom Commands cannot mention people.")
  52. if command in zero:
  53. self.servers[ctx.message.server.id]["custom_commands"]["0"][command] = edit
  54. self.con.update_config(self.servers)
  55. return await self.bot.say("Edit made. {} now outputs {}".format(command, edit))
  56. elif command in one:
  57. self.servers[ctx.message.server.id]["custom_commands"]["1"][command] = edit
  58. self.con.update_config(self.servers)
  59. return await self.bot.say("Edit made. {} now outputs {}".format(command, edit))
  60. else:
  61. return await self.bot.say("That Custom Command doesn't exist.")
  62. @custom.command(pass_context=True)
  63. async def remove(self, ctx, command):
  64. command = command.lower()
  65. if command in self.servers[ctx.message.server.id]["custom_commands"]["1"]:
  66. self.servers[ctx.message.server.id]["custom_commands"]["1"].pop(command)
  67. self.con.update_config(self.servers)
  68. return await self.bot.say("Removed {} custom command".format(command))
  69. elif command in self.servers[ctx.message.server.id]["custom_commands"]["0"]:
  70. self.servers[ctx.message.server.id]["custom_commands"]["0"].pop(command)
  71. self.con.update_config(self.servers)
  72. return await self.bot.say("Removed {} custom command".format(command))
  73. else:
  74. return await self.bot.say("Custom Command doesn't exist.")
  75. @custom.command(pass_context=True)
  76. async def list(self, ctx):
  77. l = self.servers[ctx.message.server.id]["custom_commands"]
  78. listzero = ""
  79. listone = ""
  80. for command in l["0"]:
  81. listzero = listzero + "- " + command + "\n"
  82. for command in l["1"]:
  83. listone = listone + "- " + command + "\n"
  84. if not listone:
  85. listone = "There are no commands setup.\n"
  86. if not listzero:
  87. listzero = "There are no commands setup.\n"
  88. em = discord.Embed(title="Here is the list of Custom Commands", color=load_config.embedcolour)
  89. em.add_field(name="Commands that require Prefix:", value=listone, inline=False)
  90. em.add_field(name="Commands that don't:", value=listzero, inline=False)
  91. return await self.bot.say(embed=em)
  92. def setup(Bot):
  93. Bot.add_cog(CustomCommands(Bot))