Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

86 řádky
2.0KB

  1. # Maki
  2. # ----
  3. # Discord bot by MrDetonia
  4. #
  5. # Copyright 2018 Zac Herd
  6. # Licensed under BSD 3-clause License, see LICENSE.md for more info
  7. # IMPORTS
  8. import asyncio
  9. import discord
  10. import logging
  11. import datetime
  12. import re
  13. # LOCAL IMPORTS
  14. from common import *
  15. # clamps an integer
  16. def clamp(n, small, large): return max(small, min(n, large))
  17. # converts a datetime to a string
  18. def strfromdt(dt):
  19. return dt.strftime('%Y-%m-%d %H:%M:%S')
  20. # converts a timestamp to a datetime
  21. def dtfromts(ts):
  22. return datetime.datetime.fromtimestamp(ts)
  23. # logging setup
  24. def logger():
  25. logger = logging.getLogger('discord')
  26. logger.setLevel(logging.DEBUG)
  27. handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
  28. handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
  29. logger.addHandler(handler)
  30. # send_message wrapper (deals with Discord's shit API)
  31. @asyncio.coroutine
  32. def discord_send(client, message, response):
  33. if response is not '' and message.server.id not in quiet:
  34. for attempt in range(5):
  35. try:
  36. yield from client.send_message(message.channel, response)
  37. except discord.errors.HTTPException:
  38. continue
  39. else:
  40. break
  41. else:
  42. print('ERROR: Failed to send message to discord after 5 attempts')
  43. # send typing signal to Discord
  44. @asyncio.coroutine
  45. def discord_typing(client, message):
  46. for attempt in range(5):
  47. try:
  48. yield from client.send_typing(message.channel)
  49. except discord.errors.HTTPException:
  50. continue
  51. else:
  52. break
  53. else:
  54. print('ERROR: Failed to send typing signal to discord after 5 attempts')
  55. # Maki Reacts to...
  56. @asyncio.coroutine
  57. def makireacts(client, msg):
  58. # TODO: track down the person(s) responsible for naming emoji
  59. reactions = {
  60. r"\bmaki\b": "\N{BLACK HEART SUIT}",
  61. r"\bbutter\b": "\N{PERSON WITH FOLDED HANDS}",
  62. r"\begg[s]?\b": "\N{AUBERGINE}",
  63. r"\bproblematic\b": "\N{EYEGLASSES}",
  64. }
  65. for i in reactions:
  66. if bool(re.search(i, msg.content, re.IGNORECASE)):
  67. yield from client.add_reaction(msg, reactions[i])