shrekbot.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import discord
  3. import random
  4. import re
  5. # TODO: don't keep this token in the code itself!
  6. TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  7. NONLETTER = re.compile('[^A-Za-z0-9]')
  8. client = discord.Client()
  9. def occasionally() -> bool:
  10. '''
  11. returns True one in every twenty times
  12. '''
  13. return random.randint(0, 20) == 0
  14. class Msg:
  15. '''
  16. Just a nice wrapper class to wrap messages in, which makes it easier
  17. to write high-level response functions
  18. '''
  19. def __init__(self, msg: str):
  20. self.msg = msg
  21. self.raw = NONLETTER.split(msg.content.lower())
  22. self.words = set(self.raw)
  23. def __contains__(self, substr: str):
  24. '''
  25. Check to see if the message contains this substring or sequence of
  26. substrings (if it's a list)
  27. '''
  28. if type(substr) is str:
  29. return substr in self.words
  30. elif type(substr) is list:
  31. find_len = len(substr)
  32. for x in range(len(self.raw)):
  33. if self.raw[x:x+find_len] == substr:
  34. return True
  35. return False
  36. async def respond_with_image(self, image_path: str, target=None):
  37. '''
  38. Respond to a message with an image present in our image path
  39. '''
  40. with open('./imgs/{}'.format(image_path), mode='rb') as f:
  41. f = discord.File(f)
  42. if target is None:
  43. target = self.msg.author.mention
  44. await self.msg.channel.send(target, files=[f])
  45. async def respond(self, text):
  46. '''
  47. Respond to a message with a simple piece of text
  48. '''
  49. await self.msg.channel.send(text)
  50. @client.event
  51. async def on_message(message):
  52. # we do not want the bot to reply to itself
  53. if message.author == client.user:
  54. return
  55. # wrap the message in our Msg wrapper
  56. msg = Msg(message)
  57. # TODO: turn this into a data-driven table (and maybe put a web
  58. # frontend here?) instead of just having a bunch of conditions
  59. if 'oof' in msg and occasionally():
  60. await msg.respond_with_image('cringe-compilation.jpg')
  61. if 'sandwich' in msg and occasionally():
  62. await msg.respond_with_image('snadwich.jpg')
  63. if 'anime' in msg and occasionally():
  64. response = random.choice([
  65. '"anime" lmao',
  66. 'anime? you mean pervert cartoons?',
  67. ]);
  68. await msg.respond(response)
  69. # song lyric section
  70. if ['almost','heaven'] in msg:
  71. await msg.respond('west virginia')
  72. if ['blue','ridge','mountains'] in msg:
  73. await msg.respond('shenandoah river')
  74. if ['my','life','into','pieces'] in msg:
  75. await msg.respond('THIS IS MY LAST RESORT')
  76. @client.event
  77. async def on_reaction_add(reaction, user):
  78. if reaction.emoji == '🐱':
  79. msg = reaction.message
  80. modified_message = msg.content.lower().replace('l', 'w').replace('r', 'w')
  81. face = random.choice(['OwO', 'UwU', '^w^', 'owo', ':3c', ':3cccc'])
  82. await msg.channel.send('{}: {} {}'.format(msg.author.mention, modified_message, face))
  83. else:
  84. print(reaction.emoji)
  85. @client.event
  86. async def on_ready():
  87. c_name, c_id = client.user.name, client.user.id
  88. print('Logged in as {} ({})'.format(c_name, c_id))
  89. if __name__ == '__main__':
  90. client.run(TOKEN)