utils.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import discord
  2. from youtubesearchpython import VideosSearch
  3. 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. def message_is_song_name(message):
  19. return message.content.split(' ')[0] == '!song'
  20. 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. def _get_movie_id(videos_search):
  27. return videos_search.result()['result'][0]['id']
  28. def get_video_url_by_song_name(message):
  29. url = 'https://www.youtube.com/watch?v='
  30. song_name = _get_song_name_from_message(message)
  31. videos_search = VideosSearch(song_name, limit=1)
  32. video_id = _get_movie_id(videos_search)
  33. return url + video_id
  34. def get_embed(url):
  35. return discord.Embed().set_image(url=url)
  36. def get_commands_list_to_send(self):
  37. return "Available commands: \n" + self._help_commands_for_output
  38. def is_embed(message):
  39. return message.content == ""
  40. def in_bot_channel(channel=None, message=None):
  41. if message:
  42. channel = message.channel
  43. return channel.name == 'bot'