utils.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import discord
  2. from youtubesearchpython import VideosSearch
  3. async def on_ready_print(self):
  4. print("---------")
  5. print("Logged as")
  6. print(self.user.name)
  7. print(self.user.id)
  8. print("---------")
  9. async def receive_message_then_send(message, received: str, to_send=""):
  10. if message.content == received:
  11. if to_send:
  12. await message.channel.send(to_send)
  13. return True
  14. def get_commands_from_file(filename: str) -> tuple:
  15. with open(filename, 'r') as file:
  16. _help_commands = tuple((command.split('\n')[0] for command in file))
  17. return _help_commands
  18. async def message_is_song_name(message):
  19. return message.content.split(' ')[0] == '!song'
  20. async def _get_song_name_from_message(message):
  21. parsed_msg_list = message.content.split(' ')
  22. song_name = ' '.join(parsed_msg_list[1:])
  23. if not song_name:
  24. raise NameError("Song name can't be empty!")
  25. return song_name
  26. async def _get_movie_id(videos_search):
  27. return videos_search.result()['result'][0]['id']
  28. async def get_video_url_by_song_name(message):
  29. url = 'https://www.youtube.com/watch?v='
  30. song_name = await _get_song_name_from_message(message)
  31. if song_name:
  32. videos_search = VideosSearch(song_name, limit=1)
  33. else:
  34. raise NameError
  35. try:
  36. video_id = await _get_movie_id(videos_search)
  37. except IndexError:
  38. raise NameError
  39. return url + video_id
  40. async def get_embed(url):
  41. return discord.Embed().set_image(url=url)
  42. async def get_commands_list_to_send(self):
  43. return "Available commands: \n" + self._help_commands_for_output
  44. async def is_embed(message):
  45. return message.content == ""
  46. async def in_bot_channel(channel=None, message=None):
  47. if message:
  48. channel = message.channel
  49. return channel.name == 'bot'
  50. async def get_message_by_id(channel, message_id):
  51. return await discord.GroupChannel.fetch_message(self=channel, id=message_id)
  52. async def get_reaction_info(payload):
  53. return payload.emoji, payload.member, payload.message_id
  54. async def create_message_and_add_reactions(self, _channel):
  55. message = await _channel.send("Loaded")
  56. await message.add_reaction(emoji=self._role_1)
  57. await message.add_reaction(emoji=self._role_2)
  58. await message.add_reaction(emoji=self._role_3)