__init__.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import environment_vars
  2. import discord
  3. from os import environ
  4. from img_urls import good_face_url
  5. from decorators import (
  6. author_is_not_bot,
  7. notify_if_wrong_command
  8. )
  9. from utils import (
  10. on_ready_print,
  11. receive_message_then_send,
  12. get_commands_from_file,
  13. message_is_song_name,
  14. get_video_url_by_song_name,
  15. get_embed,
  16. get_commands_list_to_send,
  17. is_embed,
  18. in_bot_channel
  19. )
  20. class MyClient(discord.Client):
  21. _help_commands = get_commands_from_file("commands_list_divided_with_newline")
  22. _help_commands_for_output = ('~ ' + command for command in _help_commands)
  23. _help_commands_for_output = '\n'.join(_help_commands_for_output)
  24. async def on_ready(self):
  25. on_ready_print(self)
  26. @author_is_not_bot
  27. @notify_if_wrong_command
  28. async def on_message(self, message):
  29. if not in_bot_channel(message=message):
  30. return
  31. if await receive_message_then_send(message, "ping", "pong"):
  32. return
  33. if await receive_message_then_send(message, "avatar"):
  34. image_to_send = get_embed(self.user.avatar_url)
  35. await message.channel.send(embed=image_to_send)
  36. return
  37. if await receive_message_then_send(message, "!help"):
  38. await message.channel.send(get_commands_list_to_send(self))
  39. return
  40. if message.attachments:
  41. await message.delete()
  42. await message.channel.send('No attachments! :)')
  43. return
  44. if await receive_message_then_send(message, "face"):
  45. image_to_send = get_embed(url=good_face_url)
  46. await message.channel.send(embed=image_to_send)
  47. return
  48. if message_is_song_name(message):
  49. try:
  50. url = get_video_url_by_song_name(message)
  51. except NameError as error_info:
  52. await message.channel.send(error_info)
  53. return
  54. await message.channel.send(url)
  55. return
  56. return True
  57. async def on_typing(self, channel, user, when):
  58. if not in_bot_channel(channel=channel):
  59. return
  60. await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
  61. async def on_message_delete(self, message):
  62. if not in_bot_channel(message=message):
  63. return
  64. message_content = f'"{message.content}"' \
  65. if not is_embed(message) \
  66. else 'just an embed or an image.'
  67. await message.channel.send(
  68. f"{message.author.mention}'s message has just been deleted which was {message_content}"
  69. )
  70. bot_token = environ.get('bot_token')
  71. client = MyClient()
  72. client.run(bot_token)