From 8b2fb4ef4cf4df13a892d8cd4e8e538cef1c7adf Mon Sep 17 00:00:00 2001 From: insert Date: Thu, 18 Jul 2024 11:26:14 -0400 Subject: [PATCH] add queue pages --- bot.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 616c5f3..24f44df 100644 --- a/bot.py +++ b/bot.py @@ -12,6 +12,7 @@ import asyncio from dotenv import load_dotenv import os import logging +import math logger = logging.getLogger('disnake') logger.setLevel(logging.DEBUG) @@ -19,7 +20,8 @@ handler = logging.FileHandler(filename='disnake.log', encoding='utf-8', mode='w' handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) logger.addHandler(handler) -queue = [] +queue = ['https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D', 'https://www.youtube.com/watch?v=f5ACkTmHoSI&pp=ygUEcXVtdQ%3D%3D'] +#queue = [] shuffle = False load_dotenv() options = Options() @@ -107,13 +109,63 @@ async def getqueue(inter: disnake.AppCmdInter): await inter.response.defer(ephemeral=True) if not queue: await inter.edit_original_response("There are no items in queue") - message = "" - for idx, item in enumerate(queue): - if idx == 0: - message = message + f"Now playing: <{item}>\n" + message = f"Now Playing: <{queue[0]}>\n" + for i in range(11): + if i == 0: continue - message = message + f"{idx}. <{item}>\n" - await inter.edit_original_response(message[:2000]) #its possible for the queue to be longer then discords max allowed + try: + message = message + f"{i}. <{queue[i]}>\n" + except IndexError: + break + message = message + f"1 of {math.ceil((len(queue)-1)/10) if (len(queue)-1)/10 > 1 else 1}" + if math.ceil((len(queue)-1)/10) > 1: + await inter.edit_original_response(message, components=[disnake.ui.Button(label=">>", style=disnake.ButtonStyle.primary, custom_id="Forward"),]) + else: + await inter.edit_original_response(message) + +@bot.listen("on_button_click") +async def button_listener(inter: disnake.MessageInteraction): + ogmsg = inter.message.content + page = ogmsg.split("\n") + page = page[-1].split(" of ") + if inter.component.custom_id == "Forward": + message = f"Now Playing: <{queue[0]}>\n" + offset = int(int(page[0]) * 10) + for i in range(11): + if i == 0: + continue + try: + message = message + f"{int(i+offset)}. <{queue[int(i+offset)]}>\n" + except IndexError: + break + message = message + f"{int(page[0])+1} of {math.ceil((len(queue)-1)/10) if (len(queue)-1)/10 > 1 else 1}" + if (int(page[0])+1) >= int(page[1]): + await inter.response.edit_message(message, components=[disnake.ui.Button(label="<<", style=disnake.ButtonStyle.primary, custom_id="Backward"),]) + else: + await inter.response.edit_message(message, components=[disnake.ui.Button(label="<<", style=disnake.ButtonStyle.primary, custom_id="Backward"), + disnake.ui.Button(label=">>", style=disnake.ButtonStyle.primary, custom_id="Forward"),]) + return + if inter.component.custom_id == "Backward": + message = f"Now Playing: <{queue[0]}>\n" + offset = int((int(page[0]) - 2) * 10) + for i in range(11): + if i == 0: + continue + try: + message = message + f"{int(i+offset)}. <{queue[int(i+offset)]}>\n" + except IndexError: + break + message = message + f"{int(page[0])-1} of {math.ceil((len(queue)-1)/10) if (len(queue)-1)/10 > 1 else 1}" + if (int(page[0])-1) <= 1 and int(page[1] == 1): + await inter.response.edit_message(message) + elif (int(page[0])-1) <= 1 and int(page[1] > 1): + await inter.response.edit_message(message, components=[disnake.ui.Button(label=">>", style=disnake.ButtonStyle.primary, custom_id="Forward"),]) + else: + await inter.response.edit_message(message, components=[disnake.ui.Button(label="<<", style=disnake.ButtonStyle.primary, custom_id="Backward"), + disnake.ui.Button(label=">>", style=disnake.ButtonStyle.primary, custom_id="Forward"),]) + await inter.response.edit_message(message, components=[]) + return + return @bot.slash_command( name="toggleplayback",