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.

139 lines
5.5KB

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