From 5cb5bcad23e9d6d9474686421fb69233cb4373ef Mon Sep 17 00:00:00 2001 From: insert Date: Sun, 4 Aug 2024 17:10:12 -0400 Subject: [PATCH] add voteskip --- bot.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index b93b570..2fa513e 100644 --- a/bot.py +++ b/bot.py @@ -25,12 +25,13 @@ logger.addHandler(handler) queue = [] user_queue = [] +skip_list = [] shuffle = False load_dotenv() options = Options() options.profile = webdriver.FirefoxProfile(os.getenv("PROFILE_PATH")) driver = webdriver.Firefox(options=options) -intents = disnake.Intents.default() +intents = disnake.Intents.all() intents.message_content = False bot = commands.Bot(intents=intents, command_prefix=".", test_guilds=[int(os.getenv("GUILD_ID"))]) @@ -42,10 +43,11 @@ async def on_ready(): def play_video(videourl): driver.get(videourl) try: - elem = WebDriverWait(driver, 5, 0.2, None).until(lambda x: x.find_element(By.CLASS_NAME, "ytp-fullscreen-button")) + elem = WebDriverWait(driver, 8, 0.2, None).until(lambda x: x.find_element(By.CLASS_NAME, "ytp-fullscreen-button")) sleep(1) elem.send_keys(Keys.RETURN) - except Exception: + except Exception as e: + print(e) return #if this errors there is no fullscreen options, such as playlists, so skip the link try: elem = driver.find_element(By.XPATH, '//button[@data-tooltip-target-id="ytp-autonav-toggle-button"][@aria-label="Autoplay is on"]') @@ -59,7 +61,8 @@ def play_video(videourl): print(e) try: elem = WebDriverWait(driver, (float(os.getenv("MAX_MIN")) * 60), 1, None).until(lambda x: x.find_element(By.CLASS_NAME, "html5-endscreen").is_displayed()) - except Exception: + except Exception as e: + print(e) return #same as above sleep(1) return @@ -189,6 +192,7 @@ async def toggleplayback(inter: disnake.AppCmdInter): @bot.slash_command( name="skip", description="skips the current video", + default_member_permissions=disnake.Permissions(8192), ) async def skip(inter: disnake.AppCmdInter): await inter.response.defer(ephemeral=False) @@ -199,6 +203,7 @@ async def skip(inter: disnake.AppCmdInter): except asyncio.CancelledError: queue.pop(0) user_queue.pop(0) + skip_list.clear() if len(queue) < 1: driver.get(f"file://{os.getcwd()}/waitingforvideo.png") driver.fullscreen_window() @@ -206,6 +211,51 @@ async def skip(inter: disnake.AppCmdInter): queuetask = asyncio.create_task(queuehandler()) await inter.edit_original_response("skipped") +@bot.slash_command( + name="voteskip", + description="vote to skip the current video", +) +async def voteskip(inter: disnake.AppCmdInter): + await inter.response.defer(ephemeral=False) + if not inter.user.voice: + await inter.edit_original_response("You are not in the voice channel") + return + if inter.user.id in skip_list: + await inter.edit_original_response("You have already voted to skip this video") + return + vc = inter.user.voice.channel.members #this could be better due to potential for abuse, but it's fine for now + print(inter.user.voice.channel.name) + broadcaster = False + for m in vc: + if m.voice.self_video or m.voice.self_stream: + broadcaster = True + break + if not broadcaster: + await inter.edit_original_response("No one is playing video so how can this be the correct vc?") + return + skip_list.append(inter.user.id) + print(len(skip_list)) + print(math.floor(len(vc)/2)) + print(vc) + if len(skip_list) >= (math.floor(len(vc)/2)): + global queuetask + queuetask.cancel() + try: + await queuetask + except asyncio.CancelledError: + queue.pop(0) + user_queue.pop(0) + skip_list.clear() + if len(queue) < 1: + driver.get(f"file://{os.getcwd()}/waitingforvideo.png") + driver.fullscreen_window() + else: + queuetask = asyncio.create_task(queuehandler()) + await inter.edit_original_response("skipped as sufficient votes were reached") + else: + await inter.edit_original_response(f"**{inter.user.display_name}** has voted to skip the video, {len(skip_list)}/{math.floor(len(vc)/2)}") + + @bot.slash_command( name="remove", description="removes a video from the queue", @@ -235,6 +285,7 @@ async def queuehandler(): await loop.run_in_executor(None, play_video, queue[0]) queue.pop(0) user_queue.pop(0) + skip_list.clear() driver.get(f"file://{os.getcwd()}/waitingforvideo.png") driver.fullscreen_window()