wastebot.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from collections import namedtuple
  2. from datetime import datetime
  3. from os import environ
  4. import asyncio
  5. import discord
  6. import re
  7. client = discord.Client()
  8. trailhead = discord.Game("%help", start=datetime(1970, 1, 1, 0, 0, 0))
  9. Command = namedtuple("Command", ["name", "desc", "usage", "regex", "action"])
  10. commands = {}
  11. # HELP
  12. help_commands = ("help", "hello", "roles", "enrole", "derole", "color", "bleach")
  13. async def help_action(msg, invoc):
  14. name = invoc[1]
  15. if name:
  16. if name in commands:
  17. await msg.channel.send("`%{0.name}`: {0.desc} Usage: `{0.usage}`.".format(commands[name]))
  18. else:
  19. await msg.channel.send("`uhh, %{}` isn’t even a command. for more information, type `%help`.".format(name))
  20. else:
  21. await msg.channel.send("Commands: {}".format(", ".join(map("`{}`".format, help_commands))))
  22. commands["help"] = Command("help", "prompts the bot to list commands or describe the specified command.", "%help [%<command name>]", re.compile("\s*%help(?:\s+%([a-z-]+))?\s*$"), help_action)
  23. # HELLO
  24. async def hello_action(msg, invoc):
  25. await msg.channel.send("Hello, {0.display_name}.".format(msg.author))
  26. commands["hello"] = Command("hello", "prompts the bot to greet the commander.", "%hello", re.compile("\s*%hello\s*$"), hello_action)
  27. # TODO: ROLES
  28. # TODO: COLOR
  29. # MAIN SHIT
  30. @client.event
  31. async def on_ready():
  32. await client.change_presence(activity=trailhead)
  33. @client.event
  34. async def on_message(msg):
  35. if msg.author == client.user:
  36. return
  37. match = re.match("\s*%([a-z-]+)(?:\s+|$)", msg.content)
  38. if match:
  39. comm = commands.get(match[1])
  40. if comm:
  41. invoc = comm.regex.match(msg.content)
  42. if invoc:
  43. await comm.action(msg, invoc)
  44. else:
  45. await msg.channel.send("uhh, that’s not how you use `%{0.name}`. Usage: `{0.usage}`. for more information, type `%help %{0.name}`.".format(comm))
  46. else:
  47. await msg.channel.send("uhh, `%{}` isn’t even a command. for more information, type `%help`.".format(match[1]))
  48. if ("TOKEN" in environ):
  49. client.run(environ["TOKEN"])