fix an issue with restricted media

This commit is contained in:
Arthur Lu 2024-07-09 20:42:11 +00:00
parent 12988ef6e4
commit ccc13700d9

View File

@ -4,7 +4,7 @@ from config import *
import discord import discord
from discord.ext import commands from discord.ext import commands
from embed import * from embed import *
from pytube import YouTube, Playlist, Search from pytube import YouTube, Playlist, Search, exceptions
import shutil import shutil
from embed import get_search_results from embed import get_search_results
@ -70,7 +70,10 @@ async def leave(ctx):
bot.queue.purge() bot.queue.purge()
bot.previous_search = None bot.previous_search = None
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
try:
shutil.rmtree('session/') # temporary cleanup procedure, will add caching later shutil.rmtree('session/') # temporary cleanup procedure, will add caching later
except FileNotFoundError: # if there is no session folder that's probably ok, just continue
pass
@bot.command() @bot.command()
async def skip(ctx, *args): async def skip(ctx, *args):
@ -190,10 +193,13 @@ async def start_playing(ctx): # should guarantee ctx.voice_client.is_playing() i
if duration < bot.config['max-length']: if duration < bot.config['max-length']:
await ctx.send(embed=get_status(ctx.voice_client.channel, bot.queue, bot.currently_playing)) await ctx.send(embed=get_status(ctx.voice_client.channel, bot.queue, bot.currently_playing))
try: # try to get the music and then start playing
yt.streams.filter(only_audio=True, file_extension='mp4').last().download(output_path=filepath, filename=filename, filename_prefix=fileprefix) yt.streams.filter(only_audio=True, file_extension='mp4').last().download(output_path=filepath, filename=filename, filename_prefix=fileprefix)
path = filepath + fileprefix + filename path = filepath + fileprefix + filename
ctx.voice_client.play(discord.FFmpegPCMAudio(path), after=lambda e:event.set()) ctx.voice_client.play(discord.FFmpegPCMAudio(path), after=lambda e:event.set())
except exceptions.AgeRestrictedError: # if it is age restricted, just skip
await ctx.send(embed=get_error('{0} is age restricted'.format(name, duration, bot.config['max-length'])))
event.set()
else: else:
await ctx.send(embed=get_error('{0} is too long: {1} > {2}'.format(name, duration, bot.config['max-length']))) await ctx.send(embed=get_error('{0} is too long: {1} > {2}'.format(name, duration, bot.config['max-length'])))