__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import unicodedata
  2. import environment_vars
  3. import discord
  4. from os import environ
  5. from img_urls import good_face_url
  6. from decorators import (
  7. author_is_not_bot,
  8. notify_if_wrong_command
  9. )
  10. from utils import (
  11. on_ready_print,
  12. receive_message_then_send,
  13. get_commands_from_file,
  14. message_is_song_name,
  15. get_video_url_by_song_name,
  16. get_embed,
  17. get_commands_list_to_send,
  18. is_embed,
  19. in_bot_channel,
  20. get_message_by_id,
  21. get_reaction_info,
  22. create_message_and_add_reactions,
  23. create_and_get_roles_dict,
  24. get_roles_for_send, get_role_from_payload, reacted_user_is_bot, get_on_delete_content
  25. )
  26. class MyClient(discord.Client):
  27. _help_commands = get_commands_from_file("commands_list_divided_with_newline")
  28. _help_commands_for_output = ('~ ' + command for command in _help_commands)
  29. _help_commands_for_output = '\n'.join(_help_commands_for_output)
  30. _emojis = {
  31. ":smiling_face_with_3_hearts:": u"\U0001F970",
  32. ":train:": u"\U0001F68B",
  33. ":kimono:": u"\U0001F458"
  34. }
  35. _roles = create_and_get_roles_dict(_emojis)
  36. _roles_for_send = get_roles_for_send(_roles)
  37. async def on_ready(self):
  38. await on_ready_print(self)
  39. _channel = None
  40. for channel in self.get_all_channels():
  41. if channel.name != 'bot':
  42. _channel = channel
  43. await create_message_and_add_reactions(self, _channel)
  44. @author_is_not_bot
  45. @notify_if_wrong_command
  46. async def on_message(self, message):
  47. if not await in_bot_channel(message=message):
  48. return
  49. if await receive_message_then_send(message, "ping", "pong"):
  50. return
  51. if await receive_message_then_send(message, "avatar"):
  52. image_to_send = await get_embed(self.user.avatar_url)
  53. await message.channel.send(embed=image_to_send)
  54. return
  55. if await receive_message_then_send(message, "!help"):
  56. await message.channel.send(await get_commands_list_to_send(self))
  57. return
  58. if message.attachments:
  59. await message.delete()
  60. await message.channel.send('No attachments! :)')
  61. return
  62. if await receive_message_then_send(message, "face"):
  63. image_to_send = await get_embed(url=good_face_url)
  64. await message.channel.send(embed=image_to_send)
  65. return
  66. if await message_is_song_name(message):
  67. try:
  68. url = await get_video_url_by_song_name(message)
  69. except (NameError, discord.errors.HTTPException):
  70. await message.channel.send('Bad song name!')
  71. return
  72. await message.channel.send(url)
  73. return
  74. return True
  75. async def on_typing(self, channel, user, when):
  76. if not await in_bot_channel(channel=channel):
  77. return
  78. await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
  79. async def on_message_delete(self, message):
  80. if not await in_bot_channel(message=message):
  81. return
  82. message_content = await get_on_delete_content(message)
  83. await message.channel.send(
  84. f"{message.author.mention}'s message has just been deleted which was {message_content}"
  85. )
  86. async def on_raw_reaction_add(self, payload):
  87. if await reacted_user_is_bot(self, payload):
  88. return
  89. channel = self.get_channel(payload.channel_id)
  90. if await in_bot_channel(channel=channel):
  91. return
  92. role = await get_role_from_payload(self, payload, channel)
  93. await payload.member.add_roles(role)
  94. bot_token = environ.get('bot_token')
  95. client = MyClient()
  96. client.run(bot_token)