__init__.py 4.1 KB

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