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.2KB

  1. import discord
  2. from Roxbot import checks, load_config
  3. from discord.ext.commands import group
  4. from Roxbot.settings import guild_settings
  5. def blacklisted(user):
  6. with open("Roxbot/blacklist.txt", "r") as fp:
  7. for line in fp.readlines():
  8. if user.id+"\n" == line:
  9. return True
  10. return False
  11. class CustomCommands():
  12. def __init__(self, bot_client):
  13. self.bot = bot_client
  14. async def on_message(self, message):
  15. settings = guild_settings.get(message.guild)
  16. msg = message.content.lower()
  17. channel = message.channel
  18. if blacklisted(message.author) or type(message.channel) != discord.TextChannel:
  19. return
  20. if message.author == self.bot.user:
  21. return
  22. if msg.startswith(self.bot.command_prefix):
  23. if msg.split(self.bot.command_prefix)[1] in settings.custom_commands["1"]:
  24. return await channel.send(settings.custom_commands["1"][msg.split(self.bot.command_prefix)[1]])
  25. else:
  26. for command in settings.custom_commands["0"]:
  27. if msg == command:
  28. return await channel.send(settings.custom_commands["0"][command])
  29. @group(pass_context=True, aliases=["cc"])
  30. @checks.is_owner_or_admin()
  31. async def custom(self, ctx):
  32. "A group of commands to manage custom commands for your server."
  33. if ctx.invoked_subcommand is None:
  34. return await ctx.send('Missing Argument')
  35. @custom.command(pass_context=True)
  36. async def add(self, ctx, command, output, prefix_required = "0"):
  37. "Adds a custom command to the list of custom commands."
  38. settings = guild_settings.get(ctx.guild)
  39. command = command.lower()
  40. output = output
  41. zero = settings.custom_commands["0"]
  42. one = settings.custom_commands["1"]
  43. if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions:
  44. return await ctx.send("Custom Commands cannot mention people/roles/everyone.")
  45. elif len(output) > 1800:
  46. return await ctx.send("The output is too long")
  47. elif command in self.bot.commands and prefix_required == "1":
  48. return await ctx.send("This is already the name of a built in command.")
  49. elif command in zero or command in one:
  50. return await ctx.send("Custom Command already exists.")
  51. elif prefix_required != "1" and prefix_required != "0":
  52. return await ctx.send("No prefix setting set.")
  53. elif len(command.split(" ")) > 1 and prefix_required == "1":
  54. return await ctx.send("Custom commands with a prefix can only be one word with no spaces.")
  55. settings.custom_commands[prefix_required][command] = output
  56. settings.update(settings.custom_commands, "custom_commands")
  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. settings = guild_settings.get(ctx.guild)
  62. zero = settings.custom_commands["0"]
  63. one = settings.custom_commands["1"]
  64. if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions:
  65. return await ctx.send("Custom Commands cannot mention people/roles/everyone.")
  66. if command in zero:
  67. settings.custom_commands["0"][command] = edit
  68. settings.update(settings.custom_commands, "custom_commands")
  69. return await ctx.send("Edit made. {} now outputs {}".format(command, edit))
  70. elif command in one:
  71. settings.custom_commands["1"][command] = edit
  72. settings.update(settings.custom_commands, "custom_commands")
  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. settings = guild_settings.get(ctx.guild)
  80. command = command.lower()
  81. if command in settings.custom_commands["1"]:
  82. settings.custom_commands["1"].pop(command)
  83. settings.update(settings.custom_commands, "custom_commands")
  84. return await ctx.send("Removed {} custom command".format(command))
  85. elif command in settings.custom_commands["0"]:
  86. settings.custom_commands["0"].pop(command)
  87. settings.update(settings.custom_commands, "custom_commands")
  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. settings = guild_settings.get(ctx.guild)
  97. l = settings.custom_commands
  98. listzero = ""
  99. listone = ""
  100. for command in l["0"]:
  101. if debug == "1":
  102. command += " - {}".format(l["0"][command])
  103. listzero = listzero + "- " + command + "\n"
  104. for command in l["1"]:
  105. if debug == "1":
  106. 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))