KiTTY, un bot Discord qui est un petit chat :) Il est basé sur une ancienne version du bot Red, sous Python 3.6 et qui a des fonctionnalités bien sympatiques !
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
Bu depo arşivlendi. Dosyaları görüntüleyebilir ve klonlayabilirsiniz ama işlem gönderemez ve konu/değişiklik isteği açamazsınız.

192 satır
7.2KB

  1. from discord.ext import commands
  2. from .utils.chat_formatting import box
  3. from .utils.dataIO import dataIO
  4. from .utils import checks
  5. from __main__ import user_allowed, send_cmd_help
  6. from copy import copy
  7. import os
  8. import discord
  9. class Alias:
  10. def __init__(self, bot):
  11. self.bot = bot
  12. self.file_path = "data/alias/aliases.json"
  13. self.aliases = dataIO.load_json(self.file_path)
  14. self.remove_old()
  15. @commands.group(pass_context=True, no_pm=True)
  16. async def alias(self, ctx):
  17. """Manage per-server aliases for commands"""
  18. if ctx.invoked_subcommand is None:
  19. await send_cmd_help(ctx)
  20. @alias.command(name="add", pass_context=True, no_pm=True)
  21. @checks.mod_or_permissions(manage_server=True)
  22. async def _add_alias(self, ctx, command, *, to_execute):
  23. """Add an alias for a command
  24. Example: !alias add test flip @Twentysix"""
  25. server = ctx.message.server
  26. command = command.lower()
  27. if len(command.split(" ")) != 1:
  28. await self.bot.say("I can't safely do multi-word aliases because"
  29. " of the fact that I allow arguments to"
  30. " aliases. It sucks, I know, deal with it.")
  31. return
  32. if self.part_of_existing_command(command, server.id):
  33. await self.bot.say('I can\'t safely add an alias that starts with '
  34. 'an existing command or alias. Sry <3')
  35. return
  36. prefix = self.get_prefix(server, to_execute)
  37. if prefix is not None:
  38. to_execute = to_execute[len(prefix):]
  39. if server.id not in self.aliases:
  40. self.aliases[server.id] = {}
  41. if command not in self.bot.commands:
  42. self.aliases[server.id][command] = to_execute
  43. dataIO.save_json(self.file_path, self.aliases)
  44. await self.bot.say("Alias '{}' added.".format(command))
  45. else:
  46. await self.bot.say("Cannot add '{}' because it's a real bot "
  47. "command.".format(command))
  48. @alias.command(name="help", pass_context=True, no_pm=True)
  49. async def _help_alias(self, ctx, command):
  50. """Tries to execute help for the base command of the alias"""
  51. server = ctx.message.server
  52. if server.id in self.aliases:
  53. server_aliases = self.aliases[server.id]
  54. if command in server_aliases:
  55. help_cmd = server_aliases[command].split(" ")[0]
  56. new_content = self.bot.settings.get_prefixes(server)[0]
  57. new_content += "help "
  58. new_content += help_cmd[len(self.get_prefix(server,
  59. help_cmd)):]
  60. message = ctx.message
  61. message.content = new_content
  62. await self.bot.process_commands(message)
  63. else:
  64. await self.bot.say("That alias doesn't exist.")
  65. @alias.command(name="show", pass_context=True, no_pm=True)
  66. async def _show_alias(self, ctx, command):
  67. """Shows what command the alias executes."""
  68. server = ctx.message.server
  69. if server.id in self.aliases:
  70. server_aliases = self.aliases[server.id]
  71. if command in server_aliases:
  72. await self.bot.say(box(server_aliases[command]))
  73. else:
  74. await self.bot.say("That alias doesn't exist.")
  75. @alias.command(name="del", pass_context=True, no_pm=True)
  76. @checks.mod_or_permissions(manage_server=True)
  77. async def _del_alias(self, ctx, command):
  78. """Deletes an alias"""
  79. command = command.lower()
  80. server = ctx.message.server
  81. if server.id in self.aliases:
  82. self.aliases[server.id].pop(command, None)
  83. dataIO.save_json(self.file_path, self.aliases)
  84. await self.bot.say("Alias '{}' deleted.".format(command))
  85. @alias.command(name="list", pass_context=True, no_pm=True)
  86. async def _alias_list(self, ctx):
  87. """Lists aliases available on this server
  88. Responds in DM"""
  89. server = ctx.message.server
  90. if server.id in self.aliases:
  91. message = "```Alias list:\n"
  92. for alias in sorted(self.aliases[server.id]):
  93. if len(message) + len(alias) + 3 > 2000:
  94. await self.bot.whisper(message)
  95. message = "```\n"
  96. message += "\t{}\n".format(alias)
  97. if message != "```Alias list:\n":
  98. message += "```"
  99. await self.bot.whisper(message)
  100. else:
  101. await self.bot.say("There are no aliases on this server.")
  102. async def on_message(self, message):
  103. if len(message.content) < 2 or message.channel.is_private:
  104. return
  105. msg = message.content
  106. server = message.server
  107. prefix = self.get_prefix(server, msg)
  108. if not prefix:
  109. return
  110. if server.id in self.aliases and user_allowed(message):
  111. alias = self.first_word(msg[len(prefix):]).lower()
  112. if alias in self.aliases[server.id]:
  113. new_command = self.aliases[server.id][alias]
  114. args = message.content[len(prefix + alias):]
  115. new_message = copy(message)
  116. new_message.content = prefix + new_command + args
  117. await self.bot.process_commands(new_message)
  118. def part_of_existing_command(self, alias, server):
  119. '''Command or alias'''
  120. for command in self.bot.commands:
  121. if alias.lower() == command.lower():
  122. return True
  123. return False
  124. def remove_old(self):
  125. for sid in self.aliases:
  126. to_delete = []
  127. to_add = []
  128. for aliasname, alias in self.aliases[sid].items():
  129. lower = aliasname.lower()
  130. if aliasname != lower:
  131. to_delete.append(aliasname)
  132. to_add.append((lower, alias))
  133. if aliasname != self.first_word(aliasname):
  134. to_delete.append(aliasname)
  135. continue
  136. server = discord.Object(id=sid)
  137. prefix = self.get_prefix(server, alias)
  138. if prefix is not None:
  139. self.aliases[sid][aliasname] = alias[len(prefix):]
  140. for alias in to_delete: # Fixes caps and bad prefixes
  141. del self.aliases[sid][alias]
  142. for alias, command in to_add: # For fixing caps
  143. self.aliases[sid][alias] = command
  144. dataIO.save_json(self.file_path, self.aliases)
  145. def first_word(self, msg):
  146. return msg.split(" ")[0]
  147. def get_prefix(self, server, msg):
  148. prefixes = self.bot.settings.get_prefixes(server)
  149. for p in prefixes:
  150. if msg.startswith(p):
  151. return p
  152. return None
  153. def check_folder():
  154. if not os.path.exists("data/alias"):
  155. print("Creating data/alias folder...")
  156. os.makedirs("data/alias")
  157. def check_file():
  158. aliases = {}
  159. f = "data/alias/aliases.json"
  160. if not dataIO.is_valid_json(f):
  161. print("Creating default alias's aliases.json...")
  162. dataIO.save_json(f, aliases)
  163. def setup(bot):
  164. check_folder()
  165. check_file()
  166. bot.add_cog(Alias(bot))