Skip to content

Commit

Permalink
Upgrading play func in Player
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelSolVargas committed Jan 7, 2022
1 parent 27c2ce3 commit 688d82e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 76 deletions.
7 changes: 6 additions & 1 deletion config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@
HELP_FRASE = "Send a randomly phrase, perhaps you get the braba"
HELP_HELP = 'This command :)'

SONGS_ADDED = 'You added {} songs to the queue'
SONG_ADDED = 'You added the song {} to the queue'
SONG_QUEUE_TITLE = 'Songs Queue'

ERROR_TITLE = 'Error :/'
NO_CHANNEL = 'To play some music, connect to any voice channel first.'
NO_GUILD = 'This guild are not connected to Vulkan'
INVALID_INPUT = 'This type of input was too strange, try something better'
DOWNLOADING_ERROR = 'An error occurred while downloading'
SONG_ADDED = 'Song added to the Queue'
EXTRACTING_ERROR = 'An error ocurred while searching for the songs'

COLOURS = {
'red': 0xDC143C,
Expand Down
144 changes: 69 additions & 75 deletions vulkan/music/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Player(commands.Cog):
def __init__(self, bot, guild):
self.__searcher: Searcher = Searcher()
self.__downloader: Downloader = Downloader()
self.__down: Downloader = Downloader()
self.__playlist: Playlist = Playlist()
self.__bot: discord.Client = bot
self.__guild: discord.Guild = guild
Expand Down Expand Up @@ -54,93 +54,87 @@ async def __play_music(self, ctx, song):
await ctx.invoke(self.__bot.get_command('np'))

songs = self.__playlist.songs_to_preload
await self.__downloader.preload(songs)

async def play(self, ctx, *args) -> str:
user_input = " ".join(args)
await self.__down.preload(songs)

async def play(self, ctx, track, requester) -> str:
try:
if self.__guild.voice_client == None:
voice_channel = ctx.author.voice.channel
await voice_channel.connect()
except:
embed = discord.Embed(
description=config.NO_CHANNEL, colour=config.COLOURS['red'])
await ctx.send(embed=embed)
else:
songs_quant = 0
try:
musics_identifiers, provider = self.__searcher.search(
user_input)
except:
return config.INVALID_INPUT

songs_names, provider = self.__searcher.search(track)
if provider == Provider.Unknown:
return config.INVALID_INPUT
embed = discord.Embed(
title=config.ERROR_TITLE,
description=config.INVALID_INPUT,
colours=config.COLOURS['blue'])
await ctx.send(embed=embed)
return

if provider == Provider.YouTube:
try:
musics_identifiers = self.__downloader.extract_youtube_link(
musics_identifiers[0])
except:
await ctx.send('Problema com o download do Youtube')
elif provider == Provider.YouTube:
songs_names = self.__down.extract_youtube_link(songs_names[0])

for identifier in musics_identifiers: # Creating songs
last_song = self.__playlist.add_song(identifier)
songs_quant = 0
for name in songs_names:
song = self.__playlist.add_song(name, requester)
songs_quant += 1

songs_preload = self.__playlist.songs_to_preload
await self.__downloader.preload(songs_preload)

if songs_quant == 1: # If only one music downloaded
song = self.__downloader.download_one(
last_song) # Download the new music
await self.__down.preload(songs_preload)

if song == None: # If song not downloaded
embed = discord.Embed(
description=config.DOWNLOADING_ERROR, colour=config.COLOURS['blue'])
await ctx.send(embed=embed)
except:
embed = discord.Embed(
title=config.ERROR_TITLE,
description=config.DOWNLOADING_ERROR,
colours=config.COLOURS['blue'])
await ctx.send(embed=embed)
return

elif not self.__playing: # If not playing
text = f'You added the song **{song.title}** to the queue'
embed = discord.Embed(
description=text, colour=config.COLOURS['blue'])
await ctx.send(embed=embed)
if songs_quant == 1:
song = self.__down.download_one(song)

else: # If playing
title = config.SONG_ADDED
embed = self.__format_embed(song.info, title=title)
await ctx.send(embed=embed)
else:
text = f'You added {songs_quant} songs to the queue'
if song == None:
embed = discord.Embed(
description=text, colour=config.COLOURS['blue'])
title=config.ERROR_TITLE,
description=config.DOWNLOADING_ERROR,
colours=config.COLOURS['blue'])
await ctx.send(embed=embed)
return
elif not self.__playing:
embed = discord.Embed(
title=config.SONG_QUEUE_TITLE,
description=config.SONG_ADDED.format(song.title),
colour=config.COLOURS['blue'])
await ctx.send(embed=embed)
else:
embed = self.__format_embed(song.info, config.SONG_ADDED)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title=config.SONG_QUEUE_TITLE,
description=config.SONGS_ADDED.format(songs_quant),
colour=config.COLOURS['blue'])
await ctx.send(embed=embed)

if not self.__playing:
try_another = True

while try_another:
first = self.__playlist.next_song()
if first == None:
embed = discord.Embed(
description=config.DOWNLOADING_ERROR, colour=config.COLOURS['blue'])
await ctx.send(embed=embed)
break
if not self.__playing:
try_another = True

while True:
if first.source != None: # If song got downloaded
try_another = False
break
while try_another: # This will ensure the first song source to be ready
first_song = self.__playlist.next_song()
if first_song == None:
embed = discord.Embed(
title=config.ERROR_TITLE,
description=config.DOWNLOADING_ERROR,
colour=config.COLOURS['blue'])
await ctx.send(embed=embed)
break

if first.problematic: # If song got any error, try another one
break
while True:
if first_song.source != None: # If song got downloaded
try_another = False
break

else: # The song is downloading, check again
continue
if first_song.problematic: # If song got any error, try another one
break

if first != None:
await self.__play_music(ctx, first)
if first_song != None:
await self.__play_music(ctx, first_song)

async def queue(self) -> discord.Embed:
if self.__playlist.looping_one:
Expand All @@ -149,7 +143,7 @@ async def queue(self) -> discord.Embed:
return self.__format_embed(info, title)

songs_preload = self.__playlist.songs_to_preload
await self.__downloader.preload(songs_preload)
await self.__down.preload(songs_preload)
total_time = format_time(sum([int(song.duration if song.duration else 0)
for song in songs_preload])) # Sum the duration
total_songs = len(self.__playlist)
Expand Down Expand Up @@ -240,7 +234,7 @@ async def shuffle(self) -> str:
self.__playlist.shuffle()
songs = self.__playlist.songs_to_preload

await self.__downloader.preload(songs)
await self.__down.preload(songs)
return 'Musics shuffled successfully'
except:
return 'An error ocurred :/'
Expand All @@ -256,7 +250,7 @@ async def move(self, pos1, pos2='1') -> str:
result = self.__playlist.move_songs(pos1, pos2)

songs = self.__playlist.songs_to_preload
await self.__downloader.preload(songs)
await self.__down.preload(songs)
return result

async def remove(self, position) -> str:
Expand Down Expand Up @@ -293,11 +287,11 @@ def __format_embed(self, info, title='') -> discord.Embed:
duration = str(datetime.timedelta(seconds=info['duration']))
embedvc.add_field(name=config.SONGINFO_DURATION,
value=f"{duration}",
inline=False)
inline=True)
else:
embedvc.add_field(name=config.SONGINFO_DURATION,
value=config.SONGINFO_UNKNOWN_DURATION,
inline=False)
inline=True)

return embedvc

Expand Down

0 comments on commit 688d82e

Please sign in to comment.