better handling of AttributeError

This commit is contained in:
Arthur Lu 2022-08-23 01:53:56 +00:00
parent 0fc83134a9
commit bc18614027

View File

@ -82,6 +82,7 @@ async def leave(ctx):
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
bot.queue = None bot.queue = None
shutil.rmtree('session/') # temporary cleanup procedure, will add caching later
@bot.command() @bot.command()
async def skip(ctx): async def skip(ctx):
@ -141,33 +142,40 @@ async def start_playing(ctx):
event = asyncio.Event() event = asyncio.Event()
event.set() event.set()
while bot.queue.has_next(): try:
event.clear() while bot.queue.has_next():
yt = bot.queue.dequeue() event.clear()
name = yt.title
duration = yt.length
filepath = 'session/' yt = bot.queue.dequeue()
fileprefix = '' name = yt.title
filename = name duration = yt.length
if duration < bot.config['max-length']: filepath = 'session/'
fileprefix = ''
filename = name
await ctx.send('playing {0} | {1} tracks remaining in queue'.format(name, len(bot.queue.elem))) if duration < bot.config['max-length']:
yt.streams.filter(only_audio=True, file_extension='mp4').last().download(output_path=filepath, filename=filename, filename_prefix=fileprefix) await ctx.send('playing {0} | {1} tracks remaining in queue'.format(name, len(bot.queue.elem)))
path = filepath + fileprefix + filename
ctx.voice_client.play(discord.FFmpegPCMAudio(path), after=lambda e:event.set())
else: yt.streams.filter(only_audio=True, file_extension='mp4').last().download(output_path=filepath, filename=filename, filename_prefix=fileprefix)
await ctx.send('{0} is too long: {1} > {2}'.format(name, duration, bot.config['max-length'])) path = filepath + fileprefix + filename
ctx.voice_client.play(discord.FFmpegPCMAudio(path), after=lambda e:event.set())
await event.wait() else:
await ctx.send('{0} is too long: {1} > {2}'.format(name, duration, bot.config['max-length']))
await ctx.voice_client.disconnect() await event.wait()
shutil.rmtree('session/') # temporary cleanup procedure, will add caching later
except AttributeError:
pass
except Exception as e:
print(e)
bot.queue = None
bot.start_playing = start_playing bot.start_playing = start_playing