From 2352c101bc4ca0cba95ac2f6a4647fc23fad1cdd Mon Sep 17 00:00:00 2001 From: a Date: Mon, 27 Mar 2023 00:43:27 +0000 Subject: [PATCH] Update 'cogs/music.py' --- cogs/music.py | 53 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/cogs/music.py b/cogs/music.py index 7dc809f..f8526bf 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -3,8 +3,7 @@ from discord.ext.commands import Cog, command, Context from discord.ext.pages import Paginator import asyncio # used to run async functions within regular functions import subprocess # for running ffprobe and getting duration of files -from os import path, makedirs -from decouple import config +from os import getenv, path, makedirs from time import time # performance tracking import random # for shuffling the queue import math # for ceiling function in queue pages @@ -61,7 +60,7 @@ class Track: self.data = data def __repr__(self): - return f"') @@ -459,6 +460,7 @@ class Music(Cog): player = await Player.prepare_file(self.track, loop = self.bot.loop) logger.info("playing Player on the voice client") + self.h += [self.track] ctx.voice_client.play( player, after=lambda e: self.after(ctx) @@ -628,7 +630,8 @@ class Music(Cog): # check that there is a queue and a current track if not self.q and not self.track: msg = await ctx.send("The queue is currently empty.") - logger.info("Message sent: The queue is currently empty.") + if msg: + logger.info("Message sent: The queue is currently empty.") return # paginate the queue to just one page full_queue = [self.track] + self.q @@ -652,6 +655,25 @@ class Music(Cog): msg = await ctx.send(formatted_results) if msg: logger.info("Message sent: Sent queue page to channel") + + @command(aliases=['h']) + async def history(self, ctx: Context, limit: int = 10): + """Show recent actions""" + logger.info(f".history {limit}" if limit else ".history") + if not self.h: + msg = await ctx.send("No available history in this session.") + if msg: + logger.info("Message sent: No available history in this session.") + return + page = self.h[-limit:] + formatted_results = f"Last {len(page)} tracks played:\n" + for i, entry in enumerate(page): + formatted_results += ( + f"{i - len(page)}: {entry}\n" + ) + msg = await ctx.send(formatted_results) + if msg: + logger.info("Message sent: Sent history page to channel") @command(aliases=['np']) async def nowplaying(self, ctx: Context): @@ -709,7 +731,9 @@ class Music(Cog): async def remove(self, ctx: Context, i: int): """Remove track at given position""" logger.info(f".remove {i}") - i -= 1 # convert to zero-indexing + i -= 2 # convert to zero-indexing and also the np track is popped + logger.warning(f"trying to pop index {i}") + logger.warning(f"{self.q[i]=}") track = self.q.pop(i) msg = await ctx.send(f"Removed: {track.title}") if msg: @@ -781,6 +805,7 @@ class Music(Cog): logger.info(".refresh") self.q = [] self.track = [] + self.h = [] msg = await ctx.send("Music bot has been refreshed") if msg: logger.info("Message sent: Bot has been refreshed") @@ -837,6 +862,6 @@ ytdl_format_options = { # "source_address": "0.0.0.0", # Bind to ipv4 since ipv6 addresses cause issues "extract_flat": True, # massive speedup for fetching metadata, at the cost of no upload date } -# username = config("YOUTUBE_USERNAME") -# password = config("YOUTUBE_PASSWORD") +username = getenv("YOUTUBE_USERNAME") +password = getenv("YOUTUBE_PASSWORD") ytdl = youtube_dl.YoutubeDL(ytdl_format_options) \ No newline at end of file