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.

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