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.

128 lines
5.4KB

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