Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

89 lignes
2.2KB

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