command.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import servers
  2. import asyncio
  3. #dict stores pointers to command objects
  4. # keys are the names of the commands
  5. commands = {}
  6. #contains help about all commands
  7. helpstring = "**Commands**: \n"
  8. #gets initialized on ready, updates player counts
  9. updater = None
  10. async def init_updater():
  11. global updater
  12. updater = servers.Updater()
  13. loop = asyncio.get_event_loop()
  14. loop.create_task(updater.start())
  15. class CommandNameError(Exception):
  16. def __init__(self, message):
  17. self.message = message
  18. class Command:
  19. def __init__(self, names, help, fn):
  20. self.names = names #list of names the command responds to
  21. self.help = help #string added to help
  22. self.function = fn #args:(client, message), returns command response
  23. global helpstring
  24. for name in names:
  25. try:
  26. commands[name]
  27. raise CommandNameError("Duplicate: \'" + name + "\'")
  28. except KeyError:
  29. commands[name] = self
  30. helpstring = helpstring + "`" + name + "` "
  31. helpstring = helpstring + ": " + help + "\n"
  32. async def respond(self, client, message):
  33. msg = await self.function(client, message)
  34. await message.channel.send(msg)
  35. #helper functions
  36. def parse_arguments(msg):
  37. command_content = msg.content
  38. words = command_content.split()
  39. return words
  40. def author_is_admin(message):
  41. isEran = message.author.id == 372460576485539840
  42. try:
  43. has_perms = message.author.permissions_in(message.channel).manage_channels
  44. return has_perms or isEran
  45. except AttributeError:
  46. return isEran
  47. #Command definitions
  48. #help
  49. async def get_help(client, msg):
  50. args = parse_arguments(msg)
  51. try:
  52. return commands[args[1]].help
  53. except:
  54. return helpstring
  55. Command(["help"], "Displays help about commands", get_help)
  56. #source
  57. async def show_source(client, msg):
  58. return "Can I see your guts too?\n" + \
  59. "https://notabug.org/NetherEran/Post_DvZ_Bot"
  60. Command(["sauce", "source", "sourcecode", "code"],\
  61. "Shows a link to the source code", show_source)
  62. #add mc server
  63. async def addmc(client, msg):
  64. if author_is_admin(msg):
  65. return servers.add_mc_server(msg)
  66. else:
  67. return "You have insufficient permissions for this command."
  68. Command(["addmcserver"],\
  69. "Admin only, adds player count updates to the channel it's used in. Arguments: ip, name, game threshold",\
  70. addmc)
  71. async def force_count(client, msg):
  72. try:
  73. return await updater.force_update(msg.guild.id)
  74. except KeyError:
  75. return "This guild does not have a player counter"
  76. Command(["count", "update"],\
  77. "Forces a player counter update and posts it in chat", \
  78. force_count)
  79. #react to commands
  80. async def handle_command(client, message):
  81. try:
  82. await commands[(message.content.split())[0][1:]].respond(client, message)
  83. except KeyError:
  84. await message.channel.send("Unknown Command")