From 41cf551f4bdf1c0eb235c07be0187aa7470bdfa6 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Tue, 21 Jun 2022 13:53:20 +0530 Subject: [PATCH 01/28] Update music.py --- src/cogs/music.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index cd7850c6..91158975 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -63,6 +63,7 @@ class Music(commands.Cog): # queue format: # query/url(str) | download(bool) | ctx(ctx) music_queue = [] + popped = 0 currently_playing_music = () currently_playing_player = None is_playing = False @@ -198,8 +199,6 @@ async def play_music_from_player(self, ctx, *, player): f'Player error: {e}') if e else None) await ctx.send(embed=embed) - _random_int = 0 - async def keep_playing(self, ctx): while len(self.music_queue) > 0: if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): @@ -308,7 +307,7 @@ async def dplay_command(self, ctx, *, url): await ctx.send(embed=embed) await self.keep_playing(ctx) - # @commands.command(name="loop", help="loops the currently playing song") + # @commands.command(name="repeat", help="loops the currently playing song") # async def loop(self, ctx): # if ctx.voice_client is None: # async with ctx.typing(): From 751185ba2b69db1604360864a88ce260c09c1660 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Thu, 23 Jun 2022 08:20:23 +0530 Subject: [PATCH 02/28] bugs-to-be-fixed --- music2.py | 605 ++++++++++++++++++++++++++++++++++++++++++++++ src/bot.py | 6 +- src/cogs/music.py | 286 +++++++++++++--------- 3 files changed, 778 insertions(+), 119 deletions(-) create mode 100644 music2.py diff --git a/music2.py b/music2.py new file mode 100644 index 00000000..3a4a4e2e --- /dev/null +++ b/music2.py @@ -0,0 +1,605 @@ +import discord +import aiohttp +import json +import youtube_dl +import asyncio +import typing +import datetime +from discord.ext import commands + +# Suppress noise about console usage from errors +youtube_dl.utils.bug_reports_message = lambda: '' + + +YTDL_FORMAT_OPTIONS = { + 'format': 'bestaudio/best', + 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', + 'restrictfilenames': True, + 'noplaylist': True, + 'nocheckcertificate': True, + 'ignoreerrors': False, + 'logtostderr': False, + 'quiet': True, + 'no_warnings': True, + 'default_search': 'auto', + # binds to ipv4 since ipv6 addresses cause issues sometimes + 'source_address': '0.0.0.0' +} + +FFMPEG_OPTIONS = { + 'options': '-vn' +} + +ytdl = youtube_dl.YoutubeDL(YTDL_FORMAT_OPTIONS) + + +class YTDLSource(discord.PCMVolumeTransformer): + + def __init__(self, source, *, data, volume=0.5): + super().__init__(source, volume) + + self.data = data + self.title = data.get('title') + self.url = data.get('url') + + @classmethod + async def from_url(cls, url, *, loop=None, stream=False): + loop = loop or asyncio.get_event_loop() + data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) + + if data is not None: + if 'entries' in data: + # take first item from a playlist + data = data['entries'][0] + else: + return None + + filename = data['url'] if stream else ytdl.prepare_filename(data) + return cls(discord.FFmpegPCMAudio(filename, **FFMPEG_OPTIONS), data=data) + + +class Music2(commands.Cog): + + # queue format: + # query/url(str) | download(bool) | ctx(ctx) + music_queue = [] + popped = 0 + currently_playing_music = () + currently_playing_player = None + is_playing = False + + bad_request_error_message = '' + bad_request_error_message += ( + ''.join("Bad response while searching for the music\n\n")) + bad_request_error_message += (''.join("**Possible causes include:**\n")) + bad_request_error_message += ( + ''.join("*1. bad network on the bot's end;\n")) + bad_request_error_message += ( + ''.join("2. the given search query couldn't find matching results;*\n")) + bad_request_error_message += (''.join( + "***3. too many queuing requests made, without letting the bot to respond to them;***\n")) + bad_request_error_message += (''.join( + "\n**To avoid any further unexpected errors, make the bot rejoin the voice channel using ` leave` and then ` join`**\n")) + bad_request_error_message += (''.join("**SORRY FOR THE INCONVENIENCE!**")) + + embed_error_no_vc_dex = discord.Embed( + title="Error", + description=''.join( + "Dex is not in any voice channel\n**Use ` join` to make it connect to one and then use music commands**"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + + MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" + + def __init__(self, bot): + self.bot = bot + self.is_playing = False + self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" + self.currently_playing_music = () + self.currently_playing_player = None + self.music_queue = [] + + @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") + async def join_command(self, ctx): + if ctx.voice_client is None: + if ctx.author.voice is not None: + await ctx.author.voice.channel.connect() + return + else: + embed = discord.Embed( + title="Error", + description=ctx.author.mention + ", you are not connected to a voice channel", + colour=0xFF0000, + timestamp=datetime.datetime.utcnow() + ) + embed.set_footer(text="join request from " + ctx.author.name) + await ctx.send(embed=embed) + return + else: + if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): + pass + else: + if ctx.author.voice is not None: + await ctx.voice_client.move_to(ctx.author.voice.channel) + return + else: + embed = discord.Embed( + title="Error", + description=ctx.author.mention + ", you are not connected to a voice channel", + colour=0xFF0000, + timestamp=datetime.datetime.utcnow() + ) + embed.set_footer( + text="join request from " + ctx.author.name) + await ctx.send(embed=embed) + return + embed = discord.Embed( + title="Error", + description=''.join( + "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + embed.set_footer(text="join request from " + ctx.author.name) + await ctx.send(embed=embed) + + async def make_join(self, ctx): + if ctx.author.voice: + if ctx.voice_client is None: + await ctx.author.voice.channel.connect() + return True + else: + if (ctx.voice_client.is_playing() or ctx.voice_client.is_paused()) and (ctx.voice_client.channel != ctx.author.voice.channel): + async with ctx.typing(): + mssg_ = "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!" + embed = discord.Embed( + title="Error", + description=mssg_, + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + embed.set_footer( + text="play request from " + ctx.author.name) + await ctx.send(embed=embed) + return True + else: + await ctx.voice_client.move_to(ctx.author.voice.channel) + return True + else: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=ctx.author.mention + ", you are not connected to a voice channel", + colour=0xFF0000, + timestamp=datetime.datetime.utcnow() + ) + embed.set_footer(text="play request from " + ctx.author.name) + await ctx.send(embed=embed) + return False + + async def play_music_from_player(self, ctx, *, player): + if player is None: + return + self.currently_playing_player = player + embed = discord.Embed( + title="Now Playing", + description="- requested by " + + self.music_queue[0][1].author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Remaining in queue", + value=len(self.music_queue)-1, inline=False) + ctx.voice_client.play(player, after=lambda e: print( + f'Player error: {e}') if e else None) + await ctx.send(embed=embed) + + async def keep_playing(self, ctx): + while len(self.music_queue) > 0: + if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): + self.is_playing = True + await self.play_music_from_player(self.music_queue[0][1], player=self.music_queue[0][0]) + self.music_queue.pop(0) + await asyncio.sleep(0.5) + + @commands.command(name="play", aliases=["stream", "p", "add"], help="streams a song directly from youtube") + async def play_command(self, ctx, *, url: typing.Optional[str]): + if (url is None) and (ctx.message.content[(len(ctx.message.content)-3):(len(ctx.message.content))] != "add"): + if ctx.voice_client is None: + await self.make_join(ctx) + if ctx.voice_client is None: + return + if ctx.voice_client.is_playing(): + return + if ctx.voice_client.is_paused(): + ctx.voice_client.resume() + elif len(self.music_queue) > 0: + if not ctx.voice_client.is_playing(): + await self.keep_playing(ctx) + else: + embed = discord.Embed( + title="Error", + description=''.join( + "Queue is empty, nothing to play\nUse ` play ` to add to queue"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + elif url is None: + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + n = "Error" + v = "Missing required arguements" + embed.add_field(name=n, value=v, inline=False) + await ctx.send(embed=embed) + + joined = await self.make_join(ctx) + if joined == False: + return + player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) + if player is None: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join(self.bad_request_error_message), + colour=0xff0000, + timestamp=datetime.datetime.utcnow(), + ) + await ctx.send(embed=embed) + return + self.music_queue.append([player, ctx]) + async with ctx.typing(): + embed = discord.Embed( + title="Added to queue", + description="\"" + url + "\" requested by " + ctx.author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow(), + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Queue Position", value=len( + self.music_queue), inline=True) + await ctx.send(embed=embed) + await self.keep_playing(ctx) + + @commands.command(name='dplay', help="downloads a song and then plays it to reduce any possible lags") + async def dplay_command(self, ctx, *, url): + await self.make_join(ctx) + if ctx.voice_client is None: + return + player = await YTDLSource.from_url(url, loop=self.bot.loop) + if player is None: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join(self.bad_request_error_message), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + self.music_queue.append([player, ctx]) + async with ctx.typing(): + embed = discord.Embed( + title="Downloaded & Added to queue", + description="\"" + url + "\" requested by " + ctx.author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow(), + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Queue Position", value=len( + self.music_queue), inline=True) + await ctx.send(embed=embed) + await self.keep_playing(ctx) + + # @commands.command(name="repeat", help="loops the currently playing song") + # async def loop(self, ctx): + # if ctx.voice_client is None: + # async with ctx.typing(): + # embed=self.embed_error_no_vc_dex + # await ctx.send(embed=embed) + # return + # if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): + # async with ctx.typing(): + # embed=discord.Embed( + # title="Error", + # description=''.join("Queue is empty, nothing to loop through\nUse ` play ` to add to queue"), + # colour=0xff0000, + # timestamp=datetime.datetime.utcnow() + # ) + # await ctx.send(embed=embed) + # return + # while True: + + @commands.command(name="queue", aliases=["view"], help="displays the current queue") + async def queue_command(self, ctx, *args): + if (ctx.message.content[(len(ctx.message.content)-5):(len(ctx.message.content))] == "queue"): + url = "".join(args) + if url != "": + await self.play_command(ctx, url) + return + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + if len(self.music_queue) == 0: + async with ctx.typing(): + embed = discord.Embed( + title="Queue", + description=''.join( + "Queue is empty, nothing to play\nUse ` play ` to add to queue"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + embed = discord.Embed( + title="Queue", + colour=0x0000ff, + timestamp=datetime.datetime.utcnow() + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) + size = len(self.music_queue) + for i in range(0, size, 25): + if i + 25 > size: + for j in range(i, size): + embed.add_field( + name=str(j + 1), value=self.music_queue[j][0].title, inline=False) + else: + for j in range(i, i + 25): + embed.add_field( + name=str(j + 1), value=self.music_queue[j][0].title, inline=False) + async with ctx.typing(): + embed.set_footer( + text="Page " + str(int(i / 25) + 1) + " of " + str(int(size / 25) + 1)) + await ctx.send(embed=embed) + + @commands.command(name="remove", help="removes a song from the queue, takes song position as argument") + async def remove_command(self, ctx, pos): + pos = int(pos) + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + if len(self.music_queue) < int(pos): + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join("There are only " + str(len(self.music_queue)) + + " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + async with ctx.typing(): + embed = discord.Embed( + title="Removed from queue", + description="track requested by " + + self.music_queue[int(pos)-1][1].author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + player = self.music_queue[int(pos)-1][0] + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Remove request by", + value=ctx.author.mention, inline=True) + self.music_queue.pop(int(pos)-1) + await ctx.send(embed=embed) + + @commands.command(name="jump", help="jumps to a song in the queue, takes song position as argument") + async def jump_command(self, ctx, pos): + pos = int(pos) + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + if len(self.music_queue) < int(pos): + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join("There are only " + str(len(self.music_queue)) + + " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + async with ctx.typing(): + embed = discord.Embed( + title="Jumping to " + str(pos), + description="- requested by " + ctx.author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + player = self.music_queue[int(pos)-1][0] + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Remaining in queue", value=str( + len(self.music_queue)-pos), inline=True) + await ctx.send(embed=embed) + counter = 0 + while counter < int(pos) - 1: + self.music_queue.pop(0) + counter += 1 + await self.skip_command(ctx) + + @commands.command(name="volume", aliases=["vol"], help="changes the volume of the music player") + async def volume_command(self, ctx, volume: int): + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + ctx.voice_client.source.volume = volume / 100 + async with ctx.typing(): + embed = discord.Embed( + title=str(volume) + "%", + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + embed.set_author(name="Volume set to", + icon_url=ctx.author.avatar_url) + await ctx.send(embed=embed) + + @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") + async def stop_command(self, ctx): + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): + self.music_queue.clear() + ctx.voice_client.stop() + return + + @commands.command(name="pause", help="pauses the music player") + async def pause_command(self, ctx): + if ctx.voice_client is None: + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + elif ctx.voice_client.is_playing(): + ctx.voice_client.pause() + + @commands.command(name="resume", help="resumes the music player") + async def resume_command(self, ctx): + if ctx.voice_client is None: + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + elif ctx.voice_client.is_paused(): + ctx.voice_client.resume() + elif len(self.music_queue) > 0: + if not ctx.voice_client.is_playing(): + await self.keep_playing(ctx) + + @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") + async def leave_command(self, ctx): + self.music_queue.clear() + self.currently_playing_music = None + self.currently_playing_player = None + if ctx.voice_client is None: + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + else: + await ctx.voice_client.disconnect() + + @commands.command(name="skip", aliases=["next"], help="skips the currently playing song") + async def skip_command(self, ctx): + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + else: + if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): + async with ctx.typing(): + embed = discord.Embed( + title="Skipping", + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + player = self.currently_playing_player + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author( + name=player.title, url=player.url, icon_url=ctx.author.avatar_url) + embed.add_field( + name="Title", value=player.title, inline=False) + embed.add_field(name="Requested by", + value=ctx.author.mention, inline=False) + embed.set_footer(text="skip requested by "+ctx.author.name) + await ctx.send(embed=embed) + ctx.voice_client.stop() + + async def get_lyrics(self, song_title): + API_URL = "https://some-random-api.ml/lyrics?title=" + song_title + async with aiohttp.ClientSession() as session: + async with session.get(API_URL) as response: + data_json = await response.json() + return data_json + + @commands.command(name='lyrics', help='sends the lyrics of the song') + async def lyrics_command(self, ctx, *args) -> None: + song_title = '' + for arg in args: + song_title += arg+'%20' + if len(song_title) > 0: + song_title = song_title[:-3] + else: + if self.currently_playing_player is None: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description="No song is currently playing", + color=0xff0000, + timestamp=datetime.datetime.utcnow(), + ) + await ctx.send(embed=embed) + return + args = self.currently_playing_player.title.split() + for arg in args: + song_title += arg+'%20' + song_title = song_title[:-3] + + data = await self.get_lyrics(song_title) + if not 'lyrics' in data.keys(): + err_mssg = data['error'] + embed = discord.Embed( + title="Error", + description=err_mssg + + ('\n'+'[see results from GoogleSearch](https://www.google.com/search?q='+song_title+'+lyrics)'), + colour=0xff0000, + timestamp=datetime.datetime.utcnow(), + ) + await ctx.send(embed=embed) + else: + async with ctx.typing(): + lyrics = data['lyrics'] + extend_text = '\n[see results from GoogleSearch](https://www.google.com/search?q=' + \ + song_title+'+lyrics)' + if len(lyrics) > 3500: + lyrics = lyrics[:3500]+'... ' + extend_text = '[read more](https://www.google.com/search?q=' + \ + song_title+'+lyrics)' + + embed = discord.Embed( + title=data['title'], + description=lyrics+extend_text, + color=0x00ff00, + timestamp=datetime.datetime.utcnow(), + ) + embed.set_author( + name=data['author'], + ) + embed.set_thumbnail(url=data['thumbnail']['genius']) + embed.set_footer( + icon_url=ctx.author.avatar_url, + ) + await ctx.send(embed=embed) + + +# def setup(bot): +# bot.add_cog(Music(bot)) diff --git a/src/bot.py b/src/bot.py index 186cb6a3..5d0aec1a 100644 --- a/src/bot.py +++ b/src/bot.py @@ -88,11 +88,13 @@ async def on_message(self, message) -> None: str(message.guild.id) + '\';') tag_switch = cur.fetchone() cur.close() - if tag_switch[0] == 'off': - return target = message.author if target == self.user: return + print("\n\n-----------------------\n-----------------------\n\n" + + str(message.content) + "\n-----------------------\n") + if tag_switch[0] == 'off': + return embed = discord.Embed( title='Message Tagged', colour=target.colour, diff --git a/src/cogs/music.py b/src/cogs/music.py index 91158975..86b7abce 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -1,3 +1,4 @@ +from turtle import title import discord import aiohttp import json @@ -49,13 +50,16 @@ async def from_url(cls, url, *, loop=None, stream=False): if data is not None: if 'entries' in data: - # take first item from a playlist data = data['entries'][0] else: return None filename = data['url'] if stream else ytdl.prepare_filename(data) return cls(discord.FFmpegPCMAudio(filename, **FFMPEG_OPTIONS), data=data) + # ---------------------------------------------------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------------------------------------------------- +# ---------------------------------------------------------------------------------------------------------------------- class Music(commands.Cog): @@ -64,6 +68,10 @@ class Music(commands.Cog): # query/url(str) | download(bool) | ctx(ctx) music_queue = [] popped = 0 + current = -1 + queued = 0 + loop_queue = True + repeat_song = False currently_playing_music = () currently_playing_player = None is_playing = False @@ -91,6 +99,7 @@ class Music(commands.Cog): ) MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" + # ---------------------------------------------------------------------------------------------------------------------- def __init__(self, bot): self.bot = bot @@ -99,14 +108,12 @@ def __init__(self, bot): self.currently_playing_music = () self.currently_playing_player = None self.music_queue = [] + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") async def join_command(self, ctx): - if ctx.voice_client is None: - if ctx.author.voice is not None: - await ctx.author.voice.channel.connect() - return - else: + if ctx.author.voice is None: + async with ctx.typing(): embed = discord.Embed( title="Error", description=ctx.author.mention + ", you are not connected to a voice channel", @@ -114,69 +121,46 @@ async def join_command(self, ctx): timestamp=datetime.datetime.utcnow() ) embed.set_footer(text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - return + await ctx.send(embed=embed) + return False + + if ctx.voice_client is None: + await ctx.author.voice.channel.connect() + return True else: - if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - pass - else: - if ctx.author.voice is not None: - await ctx.voice_client.move_to(ctx.author.voice.channel) - return - else: + if (ctx.voice_client.is_playing() or ctx.voice_client.is_paused()) and (ctx.voice_client.channel != ctx.author.voice.channel): + async with ctx.typing(): embed = discord.Embed( title="Error", - description=ctx.author.mention + ", you are not connected to a voice channel", - colour=0xFF0000, + description=''.join( + "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!"), + colour=0xff0000, timestamp=datetime.datetime.utcnow() ) embed.set_footer( text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - return - embed = discord.Embed( - title="Error", - description=''.join( - "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer(text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - - async def make_join(self, ctx): - if ctx.author.voice: - if ctx.voice_client is None: - await ctx.author.voice.channel.connect() + await ctx.send(embed=embed) return True else: - if (ctx.voice_client.is_playing() or ctx.voice_client.is_paused()) and (ctx.voice_client.channel != ctx.author.voice.channel): - async with ctx.typing(): - mssg_ = "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!" - embed = discord.Embed( - title="Error", - description=mssg_, - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer( - text="play request from " + ctx.author.name) - await ctx.send(embed=embed) - return True - else: - await ctx.voice_client.move_to(ctx.author.voice.channel) - return True - else: - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=ctx.author.mention + ", you are not connected to a voice channel", - colour=0xFF0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer(text="play request from " + ctx.author.name) + await ctx.voice_client.move_to(ctx.author.voice.channel) + return True + + # ---------------------------------------------------------------------------------------------------------------------- + + @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") + async def leave_command(self, ctx): + self.music_queue.clear() + self.currently_playing_music = None + self.current = -1 + self.popped = 0 + self.queued = 0 + self.currently_playing_player = None + if ctx.voice_client is None: + embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) - return False + else: + await ctx.voice_client.disconnect() + # ---------------------------------------------------------------------------------------------------------------------- async def play_music_from_player(self, ctx, *, player): if player is None: @@ -185,7 +169,7 @@ async def play_music_from_player(self, ctx, *, player): embed = discord.Embed( title="Now Playing", description="- requested by " + - self.music_queue[0][1].author.mention, + self.music_queue[self.current][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) @@ -193,25 +177,37 @@ async def play_music_from_player(self, ctx, *, player): embed.set_author(name=player.title, url=player.url, icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Remaining in queue", - value=len(self.music_queue)-1, inline=False) + embed.add_field(name="Position in queue", + value=self.current+1, inline=False) ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) await ctx.send(embed=embed) + # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): - while len(self.music_queue) > 0: - if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): + while ((len(self.music_queue) - self.popped > 0) or (self.loop_queue is True)) and (len(self.music_queue) > 0): + if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): self.is_playing = True - await self.play_music_from_player(self.music_queue[0][1], player=self.music_queue[0][0]) - self.music_queue.pop(0) + if (not self.repeat_song) or (self.current == -1): + self.current += 1 + if self.popped == len(self.music_queue): + self.popped = 0 + if self.current == len(self.music_queue): + self.current = 0 + player = await YTDLSource.from_url(self.music_queue[self.current][2], loop=self.bot.loop, stream=self.music_queue[self.current][3]) + self.music_queue[self.current][0] = player + await self.play_music_from_player(self.music_queue[self.current][1], player=player) + if not self.repeat_song: + self.popped += 1 await asyncio.sleep(0.5) + return + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="play", aliases=["stream", "p", "add"], help="streams a song directly from youtube") async def play_command(self, ctx, *, url: typing.Optional[str]): if (url is None) and (ctx.message.content[(len(ctx.message.content)-3):(len(ctx.message.content))] != "add"): if ctx.voice_client is None: - await self.make_join(ctx) + await self.join_command(ctx) if ctx.voice_client is None: return if ctx.voice_client.is_playing(): @@ -220,7 +216,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): ctx.voice_client.resume() elif len(self.music_queue) > 0: if not ctx.voice_client.is_playing(): - await self.keep_playing(ctx) + self.keep_playing(ctx) else: embed = discord.Embed( title="Error", @@ -243,7 +239,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): embed.add_field(name=n, value=v, inline=False) await ctx.send(embed=embed) - joined = await self.make_join(ctx) + joined = await self.join_command(ctx) if joined == False: return player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) @@ -257,7 +253,8 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): ) await ctx.send(embed=embed) return - self.music_queue.append([player, ctx]) + self.music_queue.append([player, ctx, url, True]) + self.queued += 1 async with ctx.typing(): embed = discord.Embed( title="Added to queue", @@ -272,11 +269,23 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + self.keep_playing(ctx) + return + # ---------------------------------------------------------------------------------------------------------------------- + + @commands.command(name="playm", aliases=["streamm", "pm", "addm"], help="plays multiple songs (seperated by semicolons ';')") + async def playm_command(self, ctx, *, args): + urls = args.split(';') + for url in urls: + print("\n*****calling play_command with url: " + url) + await self.play_command(ctx, url=url) + print("\n*****returning from playm_command") + return + # ---------------------------------------------------------------------------------------------------------------------- - @commands.command(name='dplay', help="downloads a song and then plays it to reduce any possible lags") + @commands.command(name='dplay', help="downloads a song and then queues it to reduce any possible lags") async def dplay_command(self, ctx, *, url): - await self.make_join(ctx) + await self.join_command(ctx) if ctx.voice_client is None: return player = await YTDLSource.from_url(url, loop=self.bot.loop) @@ -290,7 +299,8 @@ async def dplay_command(self, ctx, *, url): ) await ctx.send(embed=embed) return - self.music_queue.append([player, ctx]) + self.music_queue.append([player, ctx, url, False]) + self.queued += 1 async with ctx.typing(): embed = discord.Embed( title="Downloaded & Added to queue", @@ -305,7 +315,18 @@ async def dplay_command(self, ctx, *, url): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + self.keep_playing(ctx) + # ---------------------------------------------------------------------------------------------------------------------- + + @commands.command(name='dplaym', help="dplays multiple songs (seperated by semicolons ';')") + async def dplaym_command(self, ctx, *, args): + urls = args.split(';') + for url in urls: + print("\n*****calling dplay_command with url: " + url) + await self.dplay_command(ctx, url=url) + print("\n*****returning from dplaym_command") + return + # ---------------------------------------------------------------------------------------------------------------------- # @commands.command(name="repeat", help="loops the currently playing song") # async def loop(self, ctx): @@ -325,13 +346,13 @@ async def dplay_command(self, ctx, *, url): # await ctx.send(embed=embed) # return # while True: + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="queue", aliases=["view"], help="displays the current queue") - async def queue_command(self, ctx, *args): - if (ctx.message.content[(len(ctx.message.content)-5):(len(ctx.message.content))] == "queue"): - url = "".join(args) + async def queue_command(self, ctx, *, url: typing.Optional[str]): + if url is not None: if url != "": - await self.play_command(ctx, url) + await self.play_command(ctx, url=url) return if ctx.voice_client is None: async with ctx.typing(): @@ -358,55 +379,70 @@ async def queue_command(self, ctx, *args): embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) size = len(self.music_queue) for i in range(0, size, 25): - if i + 25 > size: - for j in range(i, size): - embed.add_field( - name=str(j + 1), value=self.music_queue[j][0].title, inline=False) - else: - for j in range(i, i + 25): - embed.add_field( - name=str(j + 1), value=self.music_queue[j][0].title, inline=False) + for j in range(i, min(size, i + 25)): + k = "**" if j == self.current else "" + embed.add_field( + name=str(j + 1) + (" ***(Currently Playing)***" if j == self.current else ""), value=k+str(self.music_queue[j][0].title)+k, inline=False) async with ctx.typing(): embed.set_footer( text="Page " + str(int(i / 25) + 1) + " of " + str(int(size / 25) + 1)) await ctx.send(embed=embed) + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="remove", help="removes a song from the queue, takes song position as argument") async def remove_command(self, ctx, pos): - pos = int(pos) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) return - if len(self.music_queue) < int(pos): + if (pos is None): async with ctx.typing(): embed = discord.Embed( title="Error", - description=''.join("There are only " + str(len(self.music_queue)) + - " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), + description=''.join( + "Missing required argument: ``"), colour=0xff0000, timestamp=datetime.datetime.utcnow() ) await ctx.send(embed=embed) return + if (1 > int(pos)) or (len(self.music_queue) < int(pos)): + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=str( + "Queue Position must be between (1 & "+str(len(self.music_queue))+")"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + pos = int(pos) - 1 async with ctx.typing(): embed = discord.Embed( title="Removed from queue", description="track requested by " + - self.music_queue[int(pos)-1][1].author.mention, + self.music_queue[int(pos)][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) - player = self.music_queue[int(pos)-1][0] + player = self.music_queue[int(pos)][0] embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name=player.title, url=player.url, icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Remove request by", value=ctx.author.mention, inline=True) - self.music_queue.pop(int(pos)-1) + self.music_queue.pop(int(pos)) + if self.current > pos: + self.current -= 1 + self.popped -= 1 + elif self.current == pos: + self.current -= 1 + ctx.voice_client.stop() await ctx.send(embed=embed) + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="jump", help="jumps to a song in the queue, takes song position as argument") async def jump_command(self, ctx, pos): @@ -416,37 +452,48 @@ async def jump_command(self, ctx, pos): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) return - if len(self.music_queue) < int(pos): + if (pos is None): + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join( + "Missing required argument: ``"), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + return + if (1 > int(pos)) or (len(self.music_queue) < int(pos)): async with ctx.typing(): embed = discord.Embed( title="Error", - description=''.join("There are only " + str(len(self.music_queue)) + - " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), + description=str( + "Queue Position must be between (1 & "+str(len(self.music_queue))+")"), colour=0xff0000, timestamp=datetime.datetime.utcnow() ) await ctx.send(embed=embed) return + pos = int(pos) - 1 async with ctx.typing(): embed = discord.Embed( - title="Jumping to " + str(pos), + title="Jumping to " + str(pos + 1), description="- requested by " + ctx.author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) - player = self.music_queue[int(pos)-1][0] + player = self.music_queue[int(pos)][0] embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name=player.title, url=player.url, icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Remaining in queue", value=str( - len(self.music_queue)-pos), inline=True) + embed.add_field( + name="Queue Looping", value="On" if self.loop_queue else "Off", inline=True) await ctx.send(embed=embed) - counter = 0 - while counter < int(pos) - 1: - self.music_queue.pop(0) - counter += 1 - await self.skip_command(ctx) + self.popped = pos + self.current = pos - 1 + ctx.voice_client.stop() + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="volume", aliases=["vol"], help="changes the volume of the music player") async def volume_command(self, ctx, volume: int): @@ -465,9 +512,15 @@ async def volume_command(self, ctx, volume: int): embed.set_author(name="Volume set to", icon_url=ctx.author.avatar_url) await ctx.send(embed=embed) + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") async def stop_command(self, ctx): + self.current = -1 + self.popped = 0 + self.queued = 0 + self.currently_playing_music = None + self.currently_playing_player = None if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -477,6 +530,7 @@ async def stop_command(self, ctx): self.music_queue.clear() ctx.voice_client.stop() return + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="pause", help="pauses the music player") async def pause_command(self, ctx): @@ -485,6 +539,8 @@ async def pause_command(self, ctx): await ctx.send(embed=embed) elif ctx.voice_client.is_playing(): ctx.voice_client.pause() + return + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="resume", help="resumes the music player") async def resume_command(self, ctx): @@ -496,17 +552,8 @@ async def resume_command(self, ctx): elif len(self.music_queue) > 0: if not ctx.voice_client.is_playing(): await self.keep_playing(ctx) - - @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") - async def leave_command(self, ctx): - self.music_queue.clear() - self.currently_playing_music = None - self.currently_playing_player = None - if ctx.voice_client is None: - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - else: - await ctx.voice_client.disconnect() + return + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="skip", aliases=["next"], help="skips the currently playing song") async def skip_command(self, ctx): @@ -532,7 +579,10 @@ async def skip_command(self, ctx): value=ctx.author.mention, inline=False) embed.set_footer(text="skip requested by "+ctx.author.name) await ctx.send(embed=embed) + self.popped += 1 ctx.voice_client.stop() + return + # ---------------------------------------------------------------------------------------------------------------------- async def get_lyrics(self, song_title): API_URL = "https://some-random-api.ml/lyrics?title=" + song_title @@ -540,6 +590,7 @@ async def get_lyrics(self, song_title): async with session.get(API_URL) as response: data_json = await response.json() return data_json + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name='lyrics', help='sends the lyrics of the song') async def lyrics_command(self, ctx, *args) -> None: @@ -599,6 +650,7 @@ async def lyrics_command(self, ctx, *args) -> None: icon_url=ctx.author.avatar_url, ) await ctx.send(embed=embed) + # ---------------------------------------------------------------------------------------------------------------------- def setup(bot): From d8ccd01b1a8e092044d16ab8050262332e36c856 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Thu, 23 Jun 2022 21:16:07 +0530 Subject: [PATCH 03/28] env_var changed --- src/bot.py | 18 +++++++++--------- src/cogs/fun.py | 2 +- src/cogs/modset.py | 2 +- src/cogs/music.py | 5 ++--- src/cogs/report.py | 6 +++--- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/bot.py b/src/bot.py index 5d0aec1a..511169a3 100644 --- a/src/bot.py +++ b/src/bot.py @@ -35,14 +35,14 @@ def __init__(self, *args, **kwargs): self.load_extension(f'src.cogs.{file[:-3]}') def connect_to_db(self) -> None: - - self.DB_CONNECTION = psycopg2.connect( - host=os.getenv('DEX_DB_HOST'), - database=os.getenv('DEX_DB_NAME'), - user=os.getenv('DEX_DB_USER'), - port=os.getenv('DEX_DB_PORT'), - password=os.getenv('DEX_DB_PASSWORD'), - ) + print(os.environ['DEX_USAGE_HISTORY_CHANNEL_ID']) + # self.DB_CONNECTION = psycopg2.connect( + # host=os.getenv('DEX_DB_HOST'), + # database=os.getenv('DEX_DB_NAME'), + # user=os.getenv('DEX_DB_USER'), + # port=os.getenv('DEX_DB_PORT'), + # password=os.getenv('DEX_DB_PASSWORD'), + # ) async def get_prefix(self, message): cur = self.DB_CONNECTION.cursor() @@ -52,7 +52,7 @@ async def get_prefix(self, message): return prefix def run(self) -> None: - super().run(os.getenv('BOT_TOKEN')) + super().run(os.getenv('DEX_BOT_TOKEN')) async def on_ready(self): print('Logged in as {0.user}'.format(self)) diff --git a/src/cogs/fun.py b/src/cogs/fun.py index 0c0463fe..7837de5b 100644 --- a/src/cogs/fun.py +++ b/src/cogs/fun.py @@ -32,7 +32,7 @@ async def send_iquote(self, ctx): async def get_nasa(self): API_URL = "https://api.nasa.gov/planetary/apod?api_key=" + \ - str(os.getenv('NASA_API_KEY')) + str(os.getenv('DEX_NASA_API_KEY')) async with aiohttp.ClientSession() as session: async with session.get(API_URL) as resp: data_json = await resp.json() diff --git a/src/cogs/modset.py b/src/cogs/modset.py index 873343f9..d193bc72 100644 --- a/src/cogs/modset.py +++ b/src/cogs/modset.py @@ -57,7 +57,7 @@ async def message_tags(self, ctx, switch: Optional[str]): @command(name="changepref", aliases=["changeprefix"], help="changes the prefix to the appended string") async def change_prefix(self, ctx, *args): prefix = "".join(args) - if ctx.guild.id == int(os.environ['PUBLIC_BOT_SERVER']): + if ctx.guild.id == int(os.environ['DEX_PUBLIC_BOT_SERVER']): embed = Embed(title="Status", colour=0xff0000, timestamp=datetime.utcnow()) diff --git a/src/cogs/music.py b/src/cogs/music.py index 86b7abce..432e21de 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -144,7 +144,6 @@ async def join_command(self, ctx): else: await ctx.voice_client.move_to(ctx.author.voice.channel) return True - # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") @@ -272,7 +271,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- - + @commands.command(name="playm", aliases=["streamm", "pm", "addm"], help="plays multiple songs (seperated by semicolons ';')") async def playm_command(self, ctx, *, args): urls = args.split(';') @@ -317,7 +316,7 @@ async def dplay_command(self, ctx, *, url): await ctx.send(embed=embed) self.keep_playing(ctx) # ---------------------------------------------------------------------------------------------------------------------- - + @commands.command(name='dplaym', help="dplays multiple songs (seperated by semicolons ';')") async def dplaym_command(self, ctx, *, args): urls = args.split(';') diff --git a/src/cogs/report.py b/src/cogs/report.py index f018d4c9..d04bb057 100644 --- a/src/cogs/report.py +++ b/src/cogs/report.py @@ -20,7 +20,7 @@ async def on_ready(self): if m.bot: bot_count += 1 - channel = self.bot.get_channel(int(os.getenv('CONSOLE_CHANNEL_ID'))) + channel = self.bot.get_channel(int(os.getenv('DEX_CONSOLE_CHANNEL_ID'))) embed = discord.Embed(title='Status', colour=0x0023dd, timestamp=datetime.utcnow()) fields = [ @@ -68,7 +68,7 @@ async def on_ready(self): for n, v, i in fields: embed.add_field(name=n, value=v, inline=i) channel = self.bot.get_channel( - int(os.getenv('CONSOLE_CHANNEL_ID')) + int(os.getenv('DEX_CONSOLE_CHANNEL_ID')) ) await channel.send(embed=embed) @@ -84,7 +84,7 @@ async def on_message(self, message): embed.set_author(name=str(target), icon_url=target.avatar_url) embed.add_field(name='Message', value=message.content, inline=False) channel = self.bot.get_channel( - int(os.getenv('USAGE_HISTORY_CHANNEL_ID')) + int(os.getenv('DEX_USAGE_HISTORY_CHANNEL_ID')) ) await channel.send(embed=embed) From ba4ce455d278da8322026969b600ccb8dd229c9c Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 00:20:15 +0530 Subject: [PATCH 04/28] working weird ffmpeg errors on local system; rest seems fine; --- src/bot.py | 15 ++-- src/cogs/modset.py | 65 +++++++++------ src/cogs/music.py | 200 ++++++++++++++++++++++++++++++++++++++------- src/cogs/report.py | 3 +- 4 files changed, 220 insertions(+), 63 deletions(-) diff --git a/src/bot.py b/src/bot.py index 511169a3..d779dc0f 100644 --- a/src/bot.py +++ b/src/bot.py @@ -35,14 +35,13 @@ def __init__(self, *args, **kwargs): self.load_extension(f'src.cogs.{file[:-3]}') def connect_to_db(self) -> None: - print(os.environ['DEX_USAGE_HISTORY_CHANNEL_ID']) - # self.DB_CONNECTION = psycopg2.connect( - # host=os.getenv('DEX_DB_HOST'), - # database=os.getenv('DEX_DB_NAME'), - # user=os.getenv('DEX_DB_USER'), - # port=os.getenv('DEX_DB_PORT'), - # password=os.getenv('DEX_DB_PASSWORD'), - # ) + self.DB_CONNECTION = psycopg2.connect( + host=os.getenv('DEX_DB_HOST'), + database=os.getenv('DEX_DB_NAME'), + user=os.getenv('DEX_DB_USER'), + port=os.getenv('DEX_DB_PORT'), + password=os.getenv('DEX_DB_PASSWORD'), + ) async def get_prefix(self, message): cur = self.DB_CONNECTION.cursor() diff --git a/src/cogs/modset.py b/src/cogs/modset.py index d193bc72..6c1bfb46 100644 --- a/src/cogs/modset.py +++ b/src/cogs/modset.py @@ -2,7 +2,6 @@ import psycopg2 from typing import Optional from datetime import datetime -from discord import Embed, Member from discord.ext.commands import Cog from discord.ext.commands import command import os @@ -14,7 +13,7 @@ def __init__(self, bot): self.bot = bot @command(name="modset", aliases=["mset", "modsettings"]) - async def modset(self, ctx, target: Optional[Member]): + async def modset(self, ctx, target: Optional[discord.Member]): pass @command(name="tags", aliases=["tagging", "msgtag"], help="toggles message tags") @@ -23,6 +22,7 @@ async def message_tags(self, ctx, switch: Optional[str]): cur.execute( 'SELECT tag_messages FROM guilds WHERE guild_id = \'' + str(ctx.guild.id) + '\';') tag_messages = cur.fetchone() + cur.close() tag_switch = tag_messages[0] if switch is None: if tag_switch == "off": @@ -35,10 +35,17 @@ async def message_tags(self, ctx, switch: Optional[str]): tag_switch = "on" else: - embed = Embed(title="Status", colour=0xff0000, - timestamp=datetime.utcnow()) - embed.add_field( - name="Error", value="Invalid value provided", inline=True) + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0xff0000, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Error", + value="Invalid value provided", + inline=True + ) await ctx.send(embed=embed) return @@ -47,28 +54,34 @@ async def message_tags(self, ctx, switch: Optional[str]): '\' WHERE guild_id = \'' + str(ctx.guild.id) + '\';') self.bot.DB_CONNECTION.commit() cur.close() - embed = Embed(title="Status", - colour=0x00ff00, - timestamp=datetime.utcnow()) - embed.add_field(name="Done", value="Message Tags are now " + - tag_switch, inline=True) + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0x00ff00, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Done", + value="Message Tags are now " + tag_switch, + inline=True + ) await ctx.send(embed=embed) @command(name="changepref", aliases=["changeprefix"], help="changes the prefix to the appended string") async def change_prefix(self, ctx, *args): prefix = "".join(args) if ctx.guild.id == int(os.environ['DEX_PUBLIC_BOT_SERVER']): - embed = Embed(title="Status", - colour=0xff0000, - timestamp=datetime.utcnow()) + embed = discord.Embed(title="Status", + colour=0xff0000, + timestamp=datetime.utcnow()) embed.add_field( name="Error", value="Prefix changes are not allowed on this server!", inline=True) await ctx.send(embed=embed) else: if ctx.author != ctx.guild.owner: - embed = Embed(title="Status", - colour=0xff0000, - timestamp=datetime.utcnow()) + embed = discord.Embed(title="Status", + colour=0xff0000, + timestamp=datetime.utcnow()) embed.add_field( name="Error", value="Only server owner can change the prefix!", inline=True) await ctx.send(embed=embed) @@ -80,16 +93,16 @@ async def change_prefix(self, ctx, *args): "\' WHERE guild_id = \'"+str(ctx.guild.id)+"\';") self.bot.DB_CONNECTION.commit() cur.close() - embed = Embed(title="Status", - colour=0x00ff00, - timestamp=datetime.utcnow()) + embed = discord.Embed(title="Status", + colour=0x00ff00, + timestamp=datetime.utcnow()) embed.add_field( name="Done", value="New Prefix is " + prefix, inline=True) await ctx.send(embed=embed) else: - embed = Embed(title="Status", - colour=0xff0000, - timestamp=datetime.utcnow()) + embed = discord.Embed(title="Status", + colour=0xff0000, + timestamp=datetime.utcnow()) embed.add_field( name="Error", value="prefix length must be between (1 - 27)", inline=True) await ctx.send(embed=embed) @@ -97,9 +110,9 @@ async def change_prefix(self, ctx, *args): @command(name="goodbye!", aliases=["leaveThisServer"], help="makes the bot to leave the server (only for server owner)") async def leave_this_server(self, ctx): if ctx.author != ctx.guild.owner: - embed = Embed(title="Status", - colour=0xff0000, - timestamp=datetime.utcnow()) + embed = discord.Embed(title="Status", + colour=0xff0000, + timestamp=datetime.utcnow()) embed.add_field( name="Error", value="Only server owner can use this command!", inline=True) await ctx.send(embed=embed) diff --git a/src/cogs/music.py b/src/cogs/music.py index 432e21de..c2814725 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -1,3 +1,4 @@ +from ast import alias from turtle import title import discord import aiohttp @@ -70,7 +71,7 @@ class Music(commands.Cog): popped = 0 current = -1 queued = 0 - loop_queue = True + loop_queue = False repeat_song = False currently_playing_music = () currently_playing_player = None @@ -153,6 +154,8 @@ async def leave_command(self, ctx): self.current = -1 self.popped = 0 self.queued = 0 + self.loop_queue = False + self.repeat_song = False self.currently_playing_player = None if ctx.voice_client is None: embed = self.embed_error_no_vc_dex @@ -237,6 +240,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): v = "Missing required arguements" embed.add_field(name=n, value=v, inline=False) await ctx.send(embed=embed) + return joined = await self.join_command(ctx) if joined == False: @@ -275,17 +279,45 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): @commands.command(name="playm", aliases=["streamm", "pm", "addm"], help="plays multiple songs (seperated by semicolons ';')") async def playm_command(self, ctx, *, args): urls = args.split(';') + joined = await self.join_command(ctx) + if joined == False: + return for url in urls: - print("\n*****calling play_command with url: " + url) - await self.play_command(ctx, url=url) - print("\n*****returning from playm_command") + player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) + if player is None: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join(self.bad_request_error_message), + colour=0xff0000, + timestamp=datetime.datetime.utcnow(), + ) + await ctx.send(embed=embed) + continue + self.music_queue.append([player, ctx, url, True]) + self.queued += 1 + async with ctx.typing(): + embed = discord.Embed( + title="Added to queue", + description="\"" + url + "\" requested by " + ctx.author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow(), + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Queue Position", value=len( + self.music_queue), inline=True) + await ctx.send(embed=embed) + self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name='dplay', help="downloads a song and then queues it to reduce any possible lags") async def dplay_command(self, ctx, *, url): - await self.join_command(ctx) - if ctx.voice_client is None: + joined = await self.join_command(ctx) + if joined == False: return player = await YTDLSource.from_url(url, loop=self.bot.loop) if player is None: @@ -315,36 +347,141 @@ async def dplay_command(self, ctx, *, url): self.music_queue), inline=True) await ctx.send(embed=embed) self.keep_playing(ctx) + return # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name='dplaym', help="dplays multiple songs (seperated by semicolons ';')") async def dplaym_command(self, ctx, *, args): urls = args.split(';') + joined = await self.join_command(ctx) + if joined == False: + return for url in urls: - print("\n*****calling dplay_command with url: " + url) - await self.dplay_command(ctx, url=url) - print("\n*****returning from dplaym_command") + player = await YTDLSource.from_url(url, loop=self.bot.loop) + if player is None: + async with ctx.typing(): + embed = discord.Embed( + title="Error", + description=''.join(self.bad_request_error_message), + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) + continue + self.music_queue.append([player, ctx, url, False]) + self.queued += 1 + async with ctx.typing(): + embed = discord.Embed( + title="Downloaded & Added to queue", + description="\"" + url + "\" requested by " + ctx.author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow(), + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Queue Position", value=len( + self.music_queue), inline=True) + await ctx.send(embed=embed) + self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- - # @commands.command(name="repeat", help="loops the currently playing song") - # async def loop(self, ctx): - # if ctx.voice_client is None: - # async with ctx.typing(): - # embed=self.embed_error_no_vc_dex - # await ctx.send(embed=embed) - # return - # if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): - # async with ctx.typing(): - # embed=discord.Embed( - # title="Error", - # description=''.join("Queue is empty, nothing to loop through\nUse ` play ` to add to queue"), - # colour=0xff0000, - # timestamp=datetime.datetime.utcnow() - # ) - # await ctx.send(embed=embed) - # return - # while True: + @commands.command(name='loop', help="toggles looping of the queue") + async def loop_command(self, ctx, loop_switch: typing.Optional[str]): + + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + + if loop_switch is None: + self.loop_queue = not self.loop_queue + if self.loop_queue: + loop_switch = "on" + else: + loop_switch = "off" + elif loop_switch.lower() == "on": + self.loop_queue = True + elif loop_switch.lower() == "off": + self.loop_queue = False + else: + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0xff0000, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Error", + value="Invalid value provided", + inline=True + ) + await ctx.send(embed=embed) + return + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0x00ff00, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Done", + value="Queue looping is now " + loop_switch.lower(), + inline=True + ) + await ctx.send(embed=embed) + return + # ---------------------------------------------------------------------------------------------------------------------- + + @commands.command(name='repeat', help="toggles repeating of the currently playing song") + async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): + + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + + if repeat_switch is None: + self.repeat_song = not self.repeat_song + if self.repeat_song: + repeat_switch = "on" + else: + repeat_switch = "off" + elif repeat_switch.lower() == "on": + self.repeat_song = True + elif repeat_switch.lower() == "off": + self.repeat_song = False + else: + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0xff0000, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Error", + value="Invalid value provided", + inline=True + ) + await ctx.send(embed=embed) + return + async with ctx.typing(): + embed = discord.Embed( + title="Status", + colour=0x00ff00, + timestamp=datetime.utcnow() + ) + embed.add_field( + name="Done", + value="Song repeat is now " + repeat_switch.lower(), + inline=True + ) + await ctx.send(embed=embed) + return # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="queue", aliases=["view"], help="displays the current queue") @@ -353,11 +490,13 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): if url != "": await self.play_command(ctx, url=url) return + if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) return + if len(self.music_queue) == 0: async with ctx.typing(): embed = discord.Embed( @@ -438,12 +577,13 @@ async def remove_command(self, ctx, pos): self.current -= 1 self.popped -= 1 elif self.current == pos: + self.repeat_song = False self.current -= 1 ctx.voice_client.stop() await ctx.send(embed=embed) # ---------------------------------------------------------------------------------------------------------------------- - @commands.command(name="jump", help="jumps to a song in the queue, takes song position as argument") + @commands.command(name="jump", alises=["jumpto"], help="jumps to a song in the queue, takes song position as argument") async def jump_command(self, ctx, pos): pos = int(pos) if ctx.voice_client is None: @@ -489,6 +629,7 @@ async def jump_command(self, ctx, pos): embed.add_field( name="Queue Looping", value="On" if self.loop_queue else "Off", inline=True) await ctx.send(embed=embed) + self.repeat_song = False self.popped = pos self.current = pos - 1 ctx.voice_client.stop() @@ -518,6 +659,8 @@ async def stop_command(self, ctx): self.current = -1 self.popped = 0 self.queued = 0 + self.loop_queue = False + self.repeat_song = False self.currently_playing_music = None self.currently_playing_player = None if ctx.voice_client is None: @@ -578,6 +721,7 @@ async def skip_command(self, ctx): value=ctx.author.mention, inline=False) embed.set_footer(text="skip requested by "+ctx.author.name) await ctx.send(embed=embed) + self.repeat_song = False self.popped += 1 ctx.voice_client.stop() return diff --git a/src/cogs/report.py b/src/cogs/report.py index d04bb057..1a41dd4e 100644 --- a/src/cogs/report.py +++ b/src/cogs/report.py @@ -20,7 +20,8 @@ async def on_ready(self): if m.bot: bot_count += 1 - channel = self.bot.get_channel(int(os.getenv('DEX_CONSOLE_CHANNEL_ID'))) + channel = self.bot.get_channel( + int(os.getenv('DEX_CONSOLE_CHANNEL_ID'))) embed = discord.Embed(title='Status', colour=0x0023dd, timestamp=datetime.utcnow()) fields = [ From 787ab655530f236fbcf742fa8eb5435bab733471 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 00:25:48 +0530 Subject: [PATCH 05/28] Delete music2.py --- music2.py | 605 ------------------------------------------------------ 1 file changed, 605 deletions(-) delete mode 100644 music2.py diff --git a/music2.py b/music2.py deleted file mode 100644 index 3a4a4e2e..00000000 --- a/music2.py +++ /dev/null @@ -1,605 +0,0 @@ -import discord -import aiohttp -import json -import youtube_dl -import asyncio -import typing -import datetime -from discord.ext import commands - -# Suppress noise about console usage from errors -youtube_dl.utils.bug_reports_message = lambda: '' - - -YTDL_FORMAT_OPTIONS = { - 'format': 'bestaudio/best', - 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', - 'restrictfilenames': True, - 'noplaylist': True, - 'nocheckcertificate': True, - 'ignoreerrors': False, - 'logtostderr': False, - 'quiet': True, - 'no_warnings': True, - 'default_search': 'auto', - # binds to ipv4 since ipv6 addresses cause issues sometimes - 'source_address': '0.0.0.0' -} - -FFMPEG_OPTIONS = { - 'options': '-vn' -} - -ytdl = youtube_dl.YoutubeDL(YTDL_FORMAT_OPTIONS) - - -class YTDLSource(discord.PCMVolumeTransformer): - - def __init__(self, source, *, data, volume=0.5): - super().__init__(source, volume) - - self.data = data - self.title = data.get('title') - self.url = data.get('url') - - @classmethod - async def from_url(cls, url, *, loop=None, stream=False): - loop = loop or asyncio.get_event_loop() - data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) - - if data is not None: - if 'entries' in data: - # take first item from a playlist - data = data['entries'][0] - else: - return None - - filename = data['url'] if stream else ytdl.prepare_filename(data) - return cls(discord.FFmpegPCMAudio(filename, **FFMPEG_OPTIONS), data=data) - - -class Music2(commands.Cog): - - # queue format: - # query/url(str) | download(bool) | ctx(ctx) - music_queue = [] - popped = 0 - currently_playing_music = () - currently_playing_player = None - is_playing = False - - bad_request_error_message = '' - bad_request_error_message += ( - ''.join("Bad response while searching for the music\n\n")) - bad_request_error_message += (''.join("**Possible causes include:**\n")) - bad_request_error_message += ( - ''.join("*1. bad network on the bot's end;\n")) - bad_request_error_message += ( - ''.join("2. the given search query couldn't find matching results;*\n")) - bad_request_error_message += (''.join( - "***3. too many queuing requests made, without letting the bot to respond to them;***\n")) - bad_request_error_message += (''.join( - "\n**To avoid any further unexpected errors, make the bot rejoin the voice channel using ` leave` and then ` join`**\n")) - bad_request_error_message += (''.join("**SORRY FOR THE INCONVENIENCE!**")) - - embed_error_no_vc_dex = discord.Embed( - title="Error", - description=''.join( - "Dex is not in any voice channel\n**Use ` join` to make it connect to one and then use music commands**"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - - MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" - - def __init__(self, bot): - self.bot = bot - self.is_playing = False - self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" - self.currently_playing_music = () - self.currently_playing_player = None - self.music_queue = [] - - @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") - async def join_command(self, ctx): - if ctx.voice_client is None: - if ctx.author.voice is not None: - await ctx.author.voice.channel.connect() - return - else: - embed = discord.Embed( - title="Error", - description=ctx.author.mention + ", you are not connected to a voice channel", - colour=0xFF0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer(text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - return - else: - if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - pass - else: - if ctx.author.voice is not None: - await ctx.voice_client.move_to(ctx.author.voice.channel) - return - else: - embed = discord.Embed( - title="Error", - description=ctx.author.mention + ", you are not connected to a voice channel", - colour=0xFF0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer( - text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - return - embed = discord.Embed( - title="Error", - description=''.join( - "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer(text="join request from " + ctx.author.name) - await ctx.send(embed=embed) - - async def make_join(self, ctx): - if ctx.author.voice: - if ctx.voice_client is None: - await ctx.author.voice.channel.connect() - return True - else: - if (ctx.voice_client.is_playing() or ctx.voice_client.is_paused()) and (ctx.voice_client.channel != ctx.author.voice.channel): - async with ctx.typing(): - mssg_ = "Can't move b/w channels while playing music!\n**NOTE: **You can still add music to the queue!" - embed = discord.Embed( - title="Error", - description=mssg_, - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer( - text="play request from " + ctx.author.name) - await ctx.send(embed=embed) - return True - else: - await ctx.voice_client.move_to(ctx.author.voice.channel) - return True - else: - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=ctx.author.mention + ", you are not connected to a voice channel", - colour=0xFF0000, - timestamp=datetime.datetime.utcnow() - ) - embed.set_footer(text="play request from " + ctx.author.name) - await ctx.send(embed=embed) - return False - - async def play_music_from_player(self, ctx, *, player): - if player is None: - return - self.currently_playing_player = player - embed = discord.Embed( - title="Now Playing", - description="- requested by " + - self.music_queue[0][1].author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Remaining in queue", - value=len(self.music_queue)-1, inline=False) - ctx.voice_client.play(player, after=lambda e: print( - f'Player error: {e}') if e else None) - await ctx.send(embed=embed) - - async def keep_playing(self, ctx): - while len(self.music_queue) > 0: - if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): - self.is_playing = True - await self.play_music_from_player(self.music_queue[0][1], player=self.music_queue[0][0]) - self.music_queue.pop(0) - await asyncio.sleep(0.5) - - @commands.command(name="play", aliases=["stream", "p", "add"], help="streams a song directly from youtube") - async def play_command(self, ctx, *, url: typing.Optional[str]): - if (url is None) and (ctx.message.content[(len(ctx.message.content)-3):(len(ctx.message.content))] != "add"): - if ctx.voice_client is None: - await self.make_join(ctx) - if ctx.voice_client is None: - return - if ctx.voice_client.is_playing(): - return - if ctx.voice_client.is_paused(): - ctx.voice_client.resume() - elif len(self.music_queue) > 0: - if not ctx.voice_client.is_playing(): - await self.keep_playing(ctx) - else: - embed = discord.Embed( - title="Error", - description=''.join( - "Queue is empty, nothing to play\nUse ` play ` to add to queue"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - await ctx.send(embed=embed) - return - elif url is None: - async with ctx.typing(): - embed = discord.Embed( - title="Status", - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - n = "Error" - v = "Missing required arguements" - embed.add_field(name=n, value=v, inline=False) - await ctx.send(embed=embed) - - joined = await self.make_join(ctx) - if joined == False: - return - player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) - if player is None: - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=''.join(self.bad_request_error_message), - colour=0xff0000, - timestamp=datetime.datetime.utcnow(), - ) - await ctx.send(embed=embed) - return - self.music_queue.append([player, ctx]) - async with ctx.typing(): - embed = discord.Embed( - title="Added to queue", - description="\"" + url + "\" requested by " + ctx.author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow(), - ) - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) - await ctx.send(embed=embed) - await self.keep_playing(ctx) - - @commands.command(name='dplay', help="downloads a song and then plays it to reduce any possible lags") - async def dplay_command(self, ctx, *, url): - await self.make_join(ctx) - if ctx.voice_client is None: - return - player = await YTDLSource.from_url(url, loop=self.bot.loop) - if player is None: - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=''.join(self.bad_request_error_message), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - await ctx.send(embed=embed) - return - self.music_queue.append([player, ctx]) - async with ctx.typing(): - embed = discord.Embed( - title="Downloaded & Added to queue", - description="\"" + url + "\" requested by " + ctx.author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow(), - ) - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) - await ctx.send(embed=embed) - await self.keep_playing(ctx) - - # @commands.command(name="repeat", help="loops the currently playing song") - # async def loop(self, ctx): - # if ctx.voice_client is None: - # async with ctx.typing(): - # embed=self.embed_error_no_vc_dex - # await ctx.send(embed=embed) - # return - # if (not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused()): - # async with ctx.typing(): - # embed=discord.Embed( - # title="Error", - # description=''.join("Queue is empty, nothing to loop through\nUse ` play ` to add to queue"), - # colour=0xff0000, - # timestamp=datetime.datetime.utcnow() - # ) - # await ctx.send(embed=embed) - # return - # while True: - - @commands.command(name="queue", aliases=["view"], help="displays the current queue") - async def queue_command(self, ctx, *args): - if (ctx.message.content[(len(ctx.message.content)-5):(len(ctx.message.content))] == "queue"): - url = "".join(args) - if url != "": - await self.play_command(ctx, url) - return - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - return - if len(self.music_queue) == 0: - async with ctx.typing(): - embed = discord.Embed( - title="Queue", - description=''.join( - "Queue is empty, nothing to play\nUse ` play ` to add to queue"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - await ctx.send(embed=embed) - return - embed = discord.Embed( - title="Queue", - colour=0x0000ff, - timestamp=datetime.datetime.utcnow() - ) - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) - size = len(self.music_queue) - for i in range(0, size, 25): - if i + 25 > size: - for j in range(i, size): - embed.add_field( - name=str(j + 1), value=self.music_queue[j][0].title, inline=False) - else: - for j in range(i, i + 25): - embed.add_field( - name=str(j + 1), value=self.music_queue[j][0].title, inline=False) - async with ctx.typing(): - embed.set_footer( - text="Page " + str(int(i / 25) + 1) + " of " + str(int(size / 25) + 1)) - await ctx.send(embed=embed) - - @commands.command(name="remove", help="removes a song from the queue, takes song position as argument") - async def remove_command(self, ctx, pos): - pos = int(pos) - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - return - if len(self.music_queue) < int(pos): - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=''.join("There are only " + str(len(self.music_queue)) + - " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - await ctx.send(embed=embed) - return - async with ctx.typing(): - embed = discord.Embed( - title="Removed from queue", - description="track requested by " + - self.music_queue[int(pos)-1][1].author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - player = self.music_queue[int(pos)-1][0] - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Remove request by", - value=ctx.author.mention, inline=True) - self.music_queue.pop(int(pos)-1) - await ctx.send(embed=embed) - - @commands.command(name="jump", help="jumps to a song in the queue, takes song position as argument") - async def jump_command(self, ctx, pos): - pos = int(pos) - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - return - if len(self.music_queue) < int(pos): - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description=''.join("There are only " + str(len(self.music_queue)) + - " songs in the queue\nNo song at position "+str(pos)+"\n**Use ` queue` to view the queue**"), - colour=0xff0000, - timestamp=datetime.datetime.utcnow() - ) - await ctx.send(embed=embed) - return - async with ctx.typing(): - embed = discord.Embed( - title="Jumping to " + str(pos), - description="- requested by " + ctx.author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - player = self.music_queue[int(pos)-1][0] - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Remaining in queue", value=str( - len(self.music_queue)-pos), inline=True) - await ctx.send(embed=embed) - counter = 0 - while counter < int(pos) - 1: - self.music_queue.pop(0) - counter += 1 - await self.skip_command(ctx) - - @commands.command(name="volume", aliases=["vol"], help="changes the volume of the music player") - async def volume_command(self, ctx, volume: int): - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - return - ctx.voice_client.source.volume = volume / 100 - async with ctx.typing(): - embed = discord.Embed( - title=str(volume) + "%", - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - embed.set_author(name="Volume set to", - icon_url=ctx.author.avatar_url) - await ctx.send(embed=embed) - - @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") - async def stop_command(self, ctx): - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - return - if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - self.music_queue.clear() - ctx.voice_client.stop() - return - - @commands.command(name="pause", help="pauses the music player") - async def pause_command(self, ctx): - if ctx.voice_client is None: - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - elif ctx.voice_client.is_playing(): - ctx.voice_client.pause() - - @commands.command(name="resume", help="resumes the music player") - async def resume_command(self, ctx): - if ctx.voice_client is None: - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - elif ctx.voice_client.is_paused(): - ctx.voice_client.resume() - elif len(self.music_queue) > 0: - if not ctx.voice_client.is_playing(): - await self.keep_playing(ctx) - - @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") - async def leave_command(self, ctx): - self.music_queue.clear() - self.currently_playing_music = None - self.currently_playing_player = None - if ctx.voice_client is None: - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - else: - await ctx.voice_client.disconnect() - - @commands.command(name="skip", aliases=["next"], help="skips the currently playing song") - async def skip_command(self, ctx): - if ctx.voice_client is None: - async with ctx.typing(): - embed = self.embed_error_no_vc_dex - await ctx.send(embed=embed) - else: - if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - async with ctx.typing(): - embed = discord.Embed( - title="Skipping", - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - player = self.currently_playing_player - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author( - name=player.title, url=player.url, icon_url=ctx.author.avatar_url) - embed.add_field( - name="Title", value=player.title, inline=False) - embed.add_field(name="Requested by", - value=ctx.author.mention, inline=False) - embed.set_footer(text="skip requested by "+ctx.author.name) - await ctx.send(embed=embed) - ctx.voice_client.stop() - - async def get_lyrics(self, song_title): - API_URL = "https://some-random-api.ml/lyrics?title=" + song_title - async with aiohttp.ClientSession() as session: - async with session.get(API_URL) as response: - data_json = await response.json() - return data_json - - @commands.command(name='lyrics', help='sends the lyrics of the song') - async def lyrics_command(self, ctx, *args) -> None: - song_title = '' - for arg in args: - song_title += arg+'%20' - if len(song_title) > 0: - song_title = song_title[:-3] - else: - if self.currently_playing_player is None: - async with ctx.typing(): - embed = discord.Embed( - title="Error", - description="No song is currently playing", - color=0xff0000, - timestamp=datetime.datetime.utcnow(), - ) - await ctx.send(embed=embed) - return - args = self.currently_playing_player.title.split() - for arg in args: - song_title += arg+'%20' - song_title = song_title[:-3] - - data = await self.get_lyrics(song_title) - if not 'lyrics' in data.keys(): - err_mssg = data['error'] - embed = discord.Embed( - title="Error", - description=err_mssg + - ('\n'+'[see results from GoogleSearch](https://www.google.com/search?q='+song_title+'+lyrics)'), - colour=0xff0000, - timestamp=datetime.datetime.utcnow(), - ) - await ctx.send(embed=embed) - else: - async with ctx.typing(): - lyrics = data['lyrics'] - extend_text = '\n[see results from GoogleSearch](https://www.google.com/search?q=' + \ - song_title+'+lyrics)' - if len(lyrics) > 3500: - lyrics = lyrics[:3500]+'... ' - extend_text = '[read more](https://www.google.com/search?q=' + \ - song_title+'+lyrics)' - - embed = discord.Embed( - title=data['title'], - description=lyrics+extend_text, - color=0x00ff00, - timestamp=datetime.datetime.utcnow(), - ) - embed.set_author( - name=data['author'], - ) - embed.set_thumbnail(url=data['thumbnail']['genius']) - embed.set_footer( - icon_url=ctx.author.avatar_url, - ) - await ctx.send(embed=embed) - - -# def setup(bot): -# bot.add_cog(Music(bot)) From 6b7315ea6895c85101409852e965f1304f18ecfa Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 00:29:03 +0530 Subject: [PATCH 06/28] removed useless imports --- src/cogs/modset.py | 2 -- src/cogs/music.py | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/cogs/modset.py b/src/cogs/modset.py index 6c1bfb46..3973179f 100644 --- a/src/cogs/modset.py +++ b/src/cogs/modset.py @@ -1,11 +1,9 @@ import discord -import psycopg2 from typing import Optional from datetime import datetime from discord.ext.commands import Cog from discord.ext.commands import command import os -import json class ModSet(Cog): diff --git a/src/cogs/music.py b/src/cogs/music.py index c2814725..ca860997 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -1,8 +1,5 @@ -from ast import alias -from turtle import title import discord import aiohttp -import json import youtube_dl import asyncio import typing From 0e7d14b763f19103c17176271fed5f8537a0c753 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 00:54:58 +0530 Subject: [PATCH 07/28] Update music.py --- src/cogs/music.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index ca860997..23103b1e 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -215,7 +215,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): ctx.voice_client.resume() elif len(self.music_queue) > 0: if not ctx.voice_client.is_playing(): - self.keep_playing(ctx) + await self.keep_playing(ctx) else: embed = discord.Embed( title="Error", @@ -269,7 +269,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - self.keep_playing(ctx) + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @@ -307,7 +307,7 @@ async def playm_command(self, ctx, *, args): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - self.keep_playing(ctx) + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @@ -343,7 +343,7 @@ async def dplay_command(self, ctx, *, url): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - self.keep_playing(ctx) + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @@ -381,7 +381,7 @@ async def dplaym_command(self, ctx, *, args): embed.add_field(name="Queue Position", value=len( self.music_queue), inline=True) await ctx.send(embed=embed) - self.keep_playing(ctx) + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- From 153a475528169778beaae9af252c3e36369bfe03 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 01:05:37 +0530 Subject: [PATCH 08/28] remove useless imports --- src/cogs/modset.py | 20 ++++++++++---------- src/cogs/music.py | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cogs/modset.py b/src/cogs/modset.py index 3973179f..0c7808ec 100644 --- a/src/cogs/modset.py +++ b/src/cogs/modset.py @@ -1,6 +1,6 @@ import discord from typing import Optional -from datetime import datetime +import datetime from discord.ext.commands import Cog from discord.ext.commands import command import os @@ -37,7 +37,7 @@ async def message_tags(self, ctx, switch: Optional[str]): embed = discord.Embed( title="Status", colour=0xff0000, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Error", @@ -56,7 +56,7 @@ async def message_tags(self, ctx, switch: Optional[str]): embed = discord.Embed( title="Status", colour=0x00ff00, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Done", @@ -71,7 +71,7 @@ async def change_prefix(self, ctx, *args): if ctx.guild.id == int(os.environ['DEX_PUBLIC_BOT_SERVER']): embed = discord.Embed(title="Status", colour=0xff0000, - timestamp=datetime.utcnow()) + timestamp=datetime.datetime.utcnow()) embed.add_field( name="Error", value="Prefix changes are not allowed on this server!", inline=True) await ctx.send(embed=embed) @@ -79,7 +79,7 @@ async def change_prefix(self, ctx, *args): if ctx.author != ctx.guild.owner: embed = discord.Embed(title="Status", colour=0xff0000, - timestamp=datetime.utcnow()) + timestamp=datetime.datetime.utcnow()) embed.add_field( name="Error", value="Only server owner can change the prefix!", inline=True) await ctx.send(embed=embed) @@ -93,14 +93,14 @@ async def change_prefix(self, ctx, *args): cur.close() embed = discord.Embed(title="Status", colour=0x00ff00, - timestamp=datetime.utcnow()) + timestamp=datetime.datetime.utcnow()) embed.add_field( name="Done", value="New Prefix is " + prefix, inline=True) await ctx.send(embed=embed) else: embed = discord.Embed(title="Status", colour=0xff0000, - timestamp=datetime.utcnow()) + timestamp=datetime.datetime.utcnow()) embed.add_field( name="Error", value="prefix length must be between (1 - 27)", inline=True) await ctx.send(embed=embed) @@ -110,7 +110,7 @@ async def leave_this_server(self, ctx): if ctx.author != ctx.guild.owner: embed = discord.Embed(title="Status", colour=0xff0000, - timestamp=datetime.utcnow()) + timestamp=datetime.datetime.utcnow()) embed.add_field( name="Error", value="Only server owner can use this command!", inline=True) await ctx.send(embed=embed) @@ -120,7 +120,7 @@ async def leave_this_server(self, ctx): Had a great time in {ctx.guild.name}! Now it's time, I guess! Report any issues: [Here](https://github.com/code-chaser/dex/issues/new) - """, color=0x8e38ce, timestamp=datetime.utcnow()) + """, color=0x8e38ce, timestamp=datetime.datetime.utcnow()) embed.set_image( url="https://user-images.githubusercontent.com/63065397/156924332-3638cd0d-9cf9-4e08-b4de-6f20cedd921a.png") embed.set_author( @@ -145,7 +145,7 @@ async def madeby(self, ctx): **codechaser#0647** **[GitHub](https://github.com/code-chaser)** """, - color=0x8e38ce, timestamp=datetime.utcnow()) + color=0x8e38ce, timestamp=datetime.datetime.utcnow()) embed.set_author(name="dex", url="https://github.com/code-chaser/dex/", icon_url=self.bot.user.avatar_url) diff --git a/src/cogs/music.py b/src/cogs/music.py index 23103b1e..618c7fbe 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -184,7 +184,7 @@ async def play_music_from_player(self, ctx, *, player): # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): - while ((len(self.music_queue) - self.popped > 0) or (self.loop_queue is True)) and (len(self.music_queue) > 0): + while ((len(self.music_queue) - self.current > 1) or (self.loop_queue is True)) and (len(self.music_queue) > 0): if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): self.is_playing = True if (not self.repeat_song) or (self.current == -1): @@ -409,7 +409,7 @@ async def loop_command(self, ctx, loop_switch: typing.Optional[str]): embed = discord.Embed( title="Status", colour=0xff0000, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Error", @@ -422,7 +422,7 @@ async def loop_command(self, ctx, loop_switch: typing.Optional[str]): embed = discord.Embed( title="Status", colour=0x00ff00, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Done", @@ -457,7 +457,7 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): embed = discord.Embed( title="Status", colour=0xff0000, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Error", @@ -470,7 +470,7 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): embed = discord.Embed( title="Status", colour=0x00ff00, - timestamp=datetime.utcnow() + timestamp=datetime.datetime.utcnow() ) embed.add_field( name="Done", From 3f5147ab0faf90b4fb5e7aec62730bf687508766 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 02:04:39 +0530 Subject: [PATCH 09/28] Update music.py --- src/cogs/music.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 618c7fbe..8e0aa5ce 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -63,7 +63,7 @@ async def from_url(cls, url, *, loop=None, stream=False): class Music(commands.Cog): # queue format: - # query/url(str) | download(bool) | ctx(ctx) + # player | ctx | url(from_user) | stream_or_not music_queue = [] popped = 0 current = -1 @@ -514,10 +514,21 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) size = len(self.music_queue) for i in range(0, size, 25): + embed = discord.Embed( + title="Queue", + description=str("Page " + str(i // 25 + 1) + " of " + str(size // 25 + 1)), + colour=0x0000ff, + timestamp=datetime.datetime.utcnow() + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) for j in range(i, min(size, i + 25)): k = "**" if j == self.current else "" embed.add_field( - name=str(j + 1) + (" ***(Currently Playing)***" if j == self.current else ""), value=k+str(self.music_queue[j][0].title)+k, inline=False) + name=str(j + 1) + (" ***(Currently Playing)***" if j == self.current else ""), + value=k+str(self.music_queue[j][0].title)+k, + inline=False + ) async with ctx.typing(): embed.set_footer( text="Page " + str(int(i / 25) + 1) + " of " + str(int(size / 25) + 1)) From 2930f4ce1108e190ff519e89b7b0253bae1ca1d2 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 02:44:42 +0530 Subject: [PATCH 10/28] Update music.py --- src/cogs/music.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/cogs/music.py b/src/cogs/music.py index 8e0aa5ce..01e07123 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -106,6 +106,13 @@ def __init__(self, bot): self.currently_playing_music = () self.currently_playing_player = None self.music_queue = [] + # self.destroy() + # ---------------------------------------------------------------------------------------------------------------------- + + # async def destroy(self, bot): + # while(bot.user.voice.channel is not None): + # await asyncio.sleep(1) + # self.__del__() # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") @@ -159,6 +166,7 @@ async def leave_command(self, ctx): await ctx.send(embed=embed) else: await ctx.voice_client.disconnect() + self.__del__() # ---------------------------------------------------------------------------------------------------------------------- async def play_music_from_player(self, ctx, *, player): From 71e6676e4cc5585221ed1561b3554612ecd80fbd Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 13:46:19 +0530 Subject: [PATCH 11/28] serious issue --- src/cogs/music.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 01e07123..dbd740d5 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -64,15 +64,6 @@ class Music(commands.Cog): # queue format: # player | ctx | url(from_user) | stream_or_not - music_queue = [] - popped = 0 - current = -1 - queued = 0 - loop_queue = False - repeat_song = False - currently_playing_music = () - currently_playing_player = None - is_playing = False bad_request_error_message = '' bad_request_error_message += ( @@ -99,13 +90,22 @@ class Music(commands.Cog): MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" # ---------------------------------------------------------------------------------------------------------------------- - def __init__(self, bot): + async def __init__(self, bot): self.bot = bot self.is_playing = False self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" self.currently_playing_music = () self.currently_playing_player = None self.music_queue = [] + self.music_queue = [] + self.popped = 0 + self.current = -1 + self.queued = 0 + self.loop_queue = False + self.repeat_song = False + self.currently_playing_music = () + self.currently_playing_player = None + self.is_playing = False # self.destroy() # ---------------------------------------------------------------------------------------------------------------------- @@ -166,7 +166,6 @@ async def leave_command(self, ctx): await ctx.send(embed=embed) else: await ctx.voice_client.disconnect() - self.__del__() # ---------------------------------------------------------------------------------------------------------------------- async def play_music_from_player(self, ctx, *, player): @@ -811,6 +810,5 @@ async def lyrics_command(self, ctx, *args) -> None: await ctx.send(embed=embed) # ---------------------------------------------------------------------------------------------------------------------- - def setup(bot): bot.add_cog(Music(bot)) From 0719cce7f22d83090175807ad9532c54926e2fa2 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 13:47:56 +0530 Subject: [PATCH 12/28] Update music.py --- src/cogs/music.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index dbd740d5..f6742aa4 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -90,7 +90,7 @@ class Music(commands.Cog): MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" # ---------------------------------------------------------------------------------------------------------------------- - async def __init__(self, bot): + def __init__(self, bot): self.bot = bot self.is_playing = False self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" From d62a05864c31ea88ceaac138c33388e3c3e30963 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 13:53:09 +0530 Subject: [PATCH 13/28] Update music.py --- src/cogs/music.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index f6742aa4..606e8fdd 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -156,8 +156,8 @@ async def leave_command(self, ctx): self.music_queue.clear() self.currently_playing_music = None self.current = -1 - self.popped = 0 self.queued = 0 + self.popped = 0 self.loop_queue = False self.repeat_song = False self.currently_playing_player = None From e1b34b168d2a7e5e013c8782112366f9580e6c36 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 14:33:27 +0530 Subject: [PATCH 14/28] bugfix --- src/cogs/music.py | 73 ++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 606e8fdd..034888d4 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -63,7 +63,7 @@ async def from_url(cls, url, *, loop=None, stream=False): class Music(commands.Cog): # queue format: - # player | ctx | url(from_user) | stream_or_not + # [guild.id] -> [player | ctx | url(from_user) | stream_or_not] bad_request_error_message = '' bad_request_error_message += ( @@ -96,16 +96,13 @@ def __init__(self, bot): self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" self.currently_playing_music = () self.currently_playing_player = None - self.music_queue = [] - self.music_queue = [] + self.music_queue = {} self.popped = 0 self.current = -1 self.queued = 0 + self.vol = 1 self.loop_queue = False self.repeat_song = False - self.currently_playing_music = () - self.currently_playing_player = None - self.is_playing = False # self.destroy() # ---------------------------------------------------------------------------------------------------------------------- @@ -153,10 +150,11 @@ async def join_command(self, ctx): @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") async def leave_command(self, ctx): - self.music_queue.clear() + self.music_queue[str(ctx.guild.id)].clear() self.currently_playing_music = None self.current = -1 self.queued = 0 + self.vol = 1 self.popped = 0 self.loop_queue = False self.repeat_song = False @@ -175,7 +173,7 @@ async def play_music_from_player(self, ctx, *, player): embed = discord.Embed( title="Now Playing", description="- requested by " + - self.music_queue[self.current][1].author.mention, + self.music_queue[str(ctx.guild.id)][self.current][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) @@ -185,24 +183,25 @@ async def play_music_from_player(self, ctx, *, player): embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Position in queue", value=self.current+1, inline=False) + ctx.voice_client.source.volume = self.vol ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) await ctx.send(embed=embed) # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): - while ((len(self.music_queue) - self.current > 1) or (self.loop_queue is True)) and (len(self.music_queue) > 0): + while ((len(self.music_queue[str(ctx.guild.id)]) - self.current > 1) or (self.loop_queue is True)) and (len(self.music_queue[str(ctx.guild.id)]) > 0): if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): self.is_playing = True if (not self.repeat_song) or (self.current == -1): self.current += 1 - if self.popped == len(self.music_queue): + if self.popped == len(self.music_queue[str(ctx.guild.id)]): self.popped = 0 - if self.current == len(self.music_queue): + if self.current == len(self.music_queue[str(ctx.guild.id)]): self.current = 0 - player = await YTDLSource.from_url(self.music_queue[self.current][2], loop=self.bot.loop, stream=self.music_queue[self.current][3]) - self.music_queue[self.current][0] = player - await self.play_music_from_player(self.music_queue[self.current][1], player=player) + player = await YTDLSource.from_url(self.music_queue[str(ctx.guild.id)][self.current][2], loop=self.bot.loop, stream=self.music_queue[str(ctx.guild.id)][self.current][3]) + self.music_queue[str(ctx.guild.id)][self.current][0] = player + await self.play_music_from_player(self.music_queue[str(ctx.guild.id)][self.current][1], player=player) if not self.repeat_song: self.popped += 1 await asyncio.sleep(0.5) @@ -220,7 +219,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): return if ctx.voice_client.is_paused(): ctx.voice_client.resume() - elif len(self.music_queue) > 0: + elif len(self.music_queue[str(ctx.guild.id)]) > 0: if not ctx.voice_client.is_playing(): await self.keep_playing(ctx) else: @@ -260,7 +259,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): ) await ctx.send(embed=embed) return - self.music_queue.append([player, ctx, url, True]) + self.music_queue[str(ctx.guild.id)].append([player, ctx, url, True]) self.queued += 1 async with ctx.typing(): embed = discord.Embed( @@ -274,7 +273,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) + self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) await self.keep_playing(ctx) return @@ -298,7 +297,7 @@ async def playm_command(self, ctx, *, args): ) await ctx.send(embed=embed) continue - self.music_queue.append([player, ctx, url, True]) + self.music_queue[str(ctx.guild.id)].append([player, ctx, url, True]) self.queued += 1 async with ctx.typing(): embed = discord.Embed( @@ -312,7 +311,7 @@ async def playm_command(self, ctx, *, args): icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) + self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) await self.keep_playing(ctx) return @@ -334,7 +333,7 @@ async def dplay_command(self, ctx, *, url): ) await ctx.send(embed=embed) return - self.music_queue.append([player, ctx, url, False]) + self.music_queue[str(ctx.guild.id)].append([player, ctx, url, False]) self.queued += 1 async with ctx.typing(): embed = discord.Embed( @@ -348,7 +347,7 @@ async def dplay_command(self, ctx, *, url): icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) + self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) await self.keep_playing(ctx) return @@ -372,7 +371,7 @@ async def dplaym_command(self, ctx, *, args): ) await ctx.send(embed=embed) continue - self.music_queue.append([player, ctx, url, False]) + self.music_queue[str(ctx.guild.id)].append([player, ctx, url, False]) self.queued += 1 async with ctx.typing(): embed = discord.Embed( @@ -386,7 +385,7 @@ async def dplaym_command(self, ctx, *, args): icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Queue Position", value=len( - self.music_queue), inline=True) + self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) await self.keep_playing(ctx) return @@ -501,7 +500,7 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): await ctx.send(embed=embed) return - if len(self.music_queue) == 0: + if len(self.music_queue[str(ctx.guild.id)]) == 0: async with ctx.typing(): embed = discord.Embed( title="Queue", @@ -519,7 +518,7 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): ) embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) - size = len(self.music_queue) + size = len(self.music_queue[str(ctx.guild.id)]) for i in range(0, size, 25): embed = discord.Embed( title="Queue", @@ -533,7 +532,7 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): k = "**" if j == self.current else "" embed.add_field( name=str(j + 1) + (" ***(Currently Playing)***" if j == self.current else ""), - value=k+str(self.music_queue[j][0].title)+k, + value=k+str(self.music_queue[str(ctx.guild.id)][j][0].title)+k, inline=False ) async with ctx.typing(): @@ -560,12 +559,12 @@ async def remove_command(self, ctx, pos): ) await ctx.send(embed=embed) return - if (1 > int(pos)) or (len(self.music_queue) < int(pos)): + if (1 > int(pos)) or (len(self.music_queue[str(ctx.guild.id)]) < int(pos)): async with ctx.typing(): embed = discord.Embed( title="Error", description=str( - "Queue Position must be between (1 & "+str(len(self.music_queue))+")"), + "Queue Position must be between (1 & "+str(len(self.music_queue[str(ctx.guild.id)]))+")"), colour=0xff0000, timestamp=datetime.datetime.utcnow() ) @@ -576,18 +575,18 @@ async def remove_command(self, ctx, pos): embed = discord.Embed( title="Removed from queue", description="track requested by " + - self.music_queue[int(pos)][1].author.mention, + self.music_queue[str(ctx.guild.id)][int(pos)][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) - player = self.music_queue[int(pos)][0] + player = self.music_queue[str(ctx.guild.id)][int(pos)][0] embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name=player.title, url=player.url, icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Remove request by", value=ctx.author.mention, inline=True) - self.music_queue.pop(int(pos)) + self.music_queue[str(ctx.guild.id)].pop(int(pos)) if self.current > pos: self.current -= 1 self.popped -= 1 @@ -617,12 +616,12 @@ async def jump_command(self, ctx, pos): ) await ctx.send(embed=embed) return - if (1 > int(pos)) or (len(self.music_queue) < int(pos)): + if (1 > int(pos)) or (len(self.music_queue[str(ctx.guild.id)]) < int(pos)): async with ctx.typing(): embed = discord.Embed( title="Error", description=str( - "Queue Position must be between (1 & "+str(len(self.music_queue))+")"), + "Queue Position must be between (1 & "+str(len(self.music_queue[str(ctx.guild.id)]))+")"), colour=0xff0000, timestamp=datetime.datetime.utcnow() ) @@ -636,7 +635,7 @@ async def jump_command(self, ctx, pos): colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) - player = self.music_queue[int(pos)][0] + player = self.music_queue[str(ctx.guild.id)][int(pos)][0] embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name=player.title, url=player.url, icon_url=ctx.author.avatar_url) @@ -658,6 +657,7 @@ async def volume_command(self, ctx, volume: int): await ctx.send(embed=embed) return ctx.voice_client.source.volume = volume / 100 + self.vol=volume async with ctx.typing(): embed = discord.Embed( title=str(volume) + "%", @@ -674,6 +674,7 @@ async def stop_command(self, ctx): self.current = -1 self.popped = 0 self.queued = 0 + self.vol = 1 self.loop_queue = False self.repeat_song = False self.currently_playing_music = None @@ -684,7 +685,7 @@ async def stop_command(self, ctx): await ctx.send(embed=embed) return if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - self.music_queue.clear() + self.music_queue[str(ctx.guild.id)].clear() ctx.voice_client.stop() return # ---------------------------------------------------------------------------------------------------------------------- @@ -706,7 +707,7 @@ async def resume_command(self, ctx): await ctx.send(embed=embed) elif ctx.voice_client.is_paused(): ctx.voice_client.resume() - elif len(self.music_queue) > 0: + elif len(self.music_queue[str(ctx.guild.id)]) > 0: if not ctx.voice_client.is_playing(): await self.keep_playing(ctx) return From 646202af650b8c428b8a771c8758a3bd08fa0bb0 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 15:01:53 +0530 Subject: [PATCH 15/28] Update music.py --- src/cogs/music.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/cogs/music.py b/src/cogs/music.py index 034888d4..97542699 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -111,9 +111,20 @@ def __init__(self, bot): # await asyncio.sleep(1) # self.__del__() # ---------------------------------------------------------------------------------------------------------------------- + + # async def on_voice_state_update(self): + # pass + # ---------------------------------------------------------------------------------------------------------------------- + + def create_guild_queue(self,ctx): + if str(ctx.guild.id) not in self.music_queue.keys(): + self.music_queue[str(ctx.guild.id)] = [] + return + # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") async def join_command(self, ctx): + self.create_guild_queue(ctx) if ctx.author.voice is None: async with ctx.typing(): embed = discord.Embed( @@ -150,6 +161,7 @@ async def join_command(self, ctx): @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") async def leave_command(self, ctx): + self.create_guild_queue(ctx) self.music_queue[str(ctx.guild.id)].clear() self.currently_playing_music = None self.current = -1 @@ -210,6 +222,7 @@ async def keep_playing(self, ctx): @commands.command(name="play", aliases=["stream", "p", "add"], help="streams a song directly from youtube") async def play_command(self, ctx, *, url: typing.Optional[str]): + self.create_guild_queue(ctx) if (url is None) and (ctx.message.content[(len(ctx.message.content)-3):(len(ctx.message.content))] != "add"): if ctx.voice_client is None: await self.join_command(ctx) @@ -281,6 +294,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): @commands.command(name="playm", aliases=["streamm", "pm", "addm"], help="plays multiple songs (seperated by semicolons ';')") async def playm_command(self, ctx, *, args): + self.create_guild_queue(ctx) urls = args.split(';') joined = await self.join_command(ctx) if joined == False: @@ -319,6 +333,7 @@ async def playm_command(self, ctx, *, args): @commands.command(name='dplay', help="downloads a song and then queues it to reduce any possible lags") async def dplay_command(self, ctx, *, url): + self.create_guild_queue(ctx) joined = await self.join_command(ctx) if joined == False: return @@ -355,6 +370,7 @@ async def dplay_command(self, ctx, *, url): @commands.command(name='dplaym', help="dplays multiple songs (seperated by semicolons ';')") async def dplaym_command(self, ctx, *, args): + self.create_guild_queue(ctx) urls = args.split(';') joined = await self.join_command(ctx) if joined == False: @@ -394,6 +410,8 @@ async def dplaym_command(self, ctx, *, args): @commands.command(name='loop', help="toggles looping of the queue") async def loop_command(self, ctx, loop_switch: typing.Optional[str]): + self.create_guild_queue(ctx) + if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -442,6 +460,8 @@ async def loop_command(self, ctx, loop_switch: typing.Optional[str]): @commands.command(name='repeat', help="toggles repeating of the currently playing song") async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): + self.create_guild_queue(ctx) + if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -489,6 +509,9 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): @commands.command(name="queue", aliases=["view"], help="displays the current queue") async def queue_command(self, ctx, *, url: typing.Optional[str]): + + self.create_guild_queue(ctx) + if url is not None: if url != "": await self.play_command(ctx, url=url) @@ -543,6 +566,7 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): @commands.command(name="remove", help="removes a song from the queue, takes song position as argument") async def remove_command(self, ctx, pos): + self.create_guild_queue(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -599,6 +623,7 @@ async def remove_command(self, ctx, pos): @commands.command(name="jump", alises=["jumpto"], help="jumps to a song in the queue, takes song position as argument") async def jump_command(self, ctx, pos): + self.create_guild_queue(ctx) pos = int(pos) if ctx.voice_client is None: async with ctx.typing(): @@ -671,6 +696,7 @@ async def volume_command(self, ctx, volume: int): @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") async def stop_command(self, ctx): + self.create_guild_queue(ctx) self.current = -1 self.popped = 0 self.queued = 0 @@ -692,6 +718,7 @@ async def stop_command(self, ctx): @commands.command(name="pause", help="pauses the music player") async def pause_command(self, ctx): + self.create_guild_queue(ctx) if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -702,6 +729,7 @@ async def pause_command(self, ctx): @commands.command(name="resume", help="resumes the music player") async def resume_command(self, ctx): + self.create_guild_queue(ctx) if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -715,6 +743,7 @@ async def resume_command(self, ctx): @commands.command(name="skip", aliases=["next"], help="skips the currently playing song") async def skip_command(self, ctx): + self.create_guild_queue(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -753,6 +782,7 @@ async def get_lyrics(self, song_title): @commands.command(name='lyrics', help='sends the lyrics of the song') async def lyrics_command(self, ctx, *args) -> None: + self.create_guild_queue(ctx) song_title = '' for arg in args: song_title += arg+'%20' From 593d856aeb2956e510e4d0cc3137d729a05907ed Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Fri, 24 Jun 2022 15:21:18 +0530 Subject: [PATCH 16/28] Update music.py --- src/cogs/music.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 97542699..0df718be 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -182,23 +182,25 @@ async def play_music_from_player(self, ctx, *, player): if player is None: return self.currently_playing_player = player - embed = discord.Embed( - title="Now Playing", - description="- requested by " + - self.music_queue[str(ctx.guild.id)][self.current][1].author.mention, - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) - embed.add_field(name="Title", value=player.title, inline=False) - embed.add_field(name="Position in queue", - value=self.current+1, inline=False) - ctx.voice_client.source.volume = self.vol + async with ctx.typing(): + embed = discord.Embed( + title="Now Playing", + description="- requested by " + + self.music_queue[str(ctx.guild.id)][self.current][1].author.mention, + colour=0x00ff00, + timestamp=datetime.datetime.utcnow() + ) + embed.set_thumbnail(url=self.MUSIC_ICON) + embed.set_author(name=player.title, url=player.url, + icon_url=ctx.author.avatar_url) + embed.add_field(name="Title", value=player.title, inline=False) + embed.add_field(name="Position in queue", + value=self.current+1, inline=False) + embed.add_field(name="Volume", value=str(self.vol * 100) + "%", inline=False) + await ctx.send(embed=embed) ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) - await ctx.send(embed=embed) + ctx.voice_client.source.volume = self.vol # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): @@ -682,7 +684,7 @@ async def volume_command(self, ctx, volume: int): await ctx.send(embed=embed) return ctx.voice_client.source.volume = volume / 100 - self.vol=volume + self.vol = volume / 100 async with ctx.typing(): embed = discord.Embed( title=str(volume) + "%", From af49c7e129c2201268b12be8985a092649a36e18 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 13:26:41 +0530 Subject: [PATCH 17/28] Update music.py --- src/cogs/music.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 0df718be..45d1b20d 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -63,7 +63,7 @@ async def from_url(cls, url, *, loop=None, stream=False): class Music(commands.Cog): # queue format: - # [guild.id] -> [player | ctx | url(from_user) | stream_or_not] + # [guild.id] -> [0 player | 1 ctx | 2 url(from_user) | 3 stream_or_not] bad_request_error_message = '' bad_request_error_message += ( @@ -512,6 +512,9 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): @commands.command(name="queue", aliases=["view"], help="displays the current queue") async def queue_command(self, ctx, *, url: typing.Optional[str]): + if str(ctx.guild.id) in self.music_queue: # for debugging purposes + print(self.music_queue[str(ctx.guild.id)]) + self.create_guild_queue(ctx) if url is not None: From 18028066901ab04572ea8d3f5a8075adf1374716 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 14:33:39 +0530 Subject: [PATCH 18/28] Update music.py --- src/cogs/music.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cogs/music.py b/src/cogs/music.py index 45d1b20d..4251fca3 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -302,6 +302,7 @@ async def playm_command(self, ctx, *, args): if joined == False: return for url in urls: + url = url.strip() player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) if player is None: async with ctx.typing(): @@ -378,6 +379,7 @@ async def dplaym_command(self, ctx, *, args): if joined == False: return for url in urls: + url = url.strip() player = await YTDLSource.from_url(url, loop=self.bot.loop) if player is None: async with ctx.typing(): From e56bf6ea596d61f0f25523192bcc98d6f612942a Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 14:44:52 +0530 Subject: [PATCH 19/28] Update music.py --- src/cogs/music.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 4251fca3..3d1b6c45 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -301,6 +301,7 @@ async def playm_command(self, ctx, *, args): joined = await self.join_command(ctx) if joined == False: return + last_url = urls.pop() for url in urls: url = url.strip() player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) @@ -330,7 +331,7 @@ async def playm_command(self, ctx, *, args): embed.add_field(name="Queue Position", value=len( self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + await self.play_command(ctx, url=last_url) return # ---------------------------------------------------------------------------------------------------------------------- @@ -378,6 +379,7 @@ async def dplaym_command(self, ctx, *, args): joined = await self.join_command(ctx) if joined == False: return + last_url=urls.pop() for url in urls: url = url.strip() player = await YTDLSource.from_url(url, loop=self.bot.loop) @@ -407,7 +409,7 @@ async def dplaym_command(self, ctx, *, args): embed.add_field(name="Queue Position", value=len( self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + await self.dplay_command(ctx, url=last_url) return # ---------------------------------------------------------------------------------------------------------------------- From 81ee4bc703b53c2194652a61c9f4a15e421f4169 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 17:54:08 +0530 Subject: [PATCH 20/28] some progress (flag:X3s4) --- src/cogs/music.py | 82 +++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 3d1b6c45..3c797bf9 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -97,7 +97,6 @@ def __init__(self, bot): self.currently_playing_music = () self.currently_playing_player = None self.music_queue = {} - self.popped = 0 self.current = -1 self.queued = 0 self.vol = 1 @@ -167,7 +166,6 @@ async def leave_command(self, ctx): self.current = -1 self.queued = 0 self.vol = 1 - self.popped = 0 self.loop_queue = False self.repeat_song = False self.currently_playing_player = None @@ -207,17 +205,12 @@ async def keep_playing(self, ctx): while ((len(self.music_queue[str(ctx.guild.id)]) - self.current > 1) or (self.loop_queue is True)) and (len(self.music_queue[str(ctx.guild.id)]) > 0): if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): self.is_playing = True - if (not self.repeat_song) or (self.current == -1): + if (not self.repeat_song): self.current += 1 - if self.popped == len(self.music_queue[str(ctx.guild.id)]): - self.popped = 0 - if self.current == len(self.music_queue[str(ctx.guild.id)]): - self.current = 0 + self.current %= len(self.music_queue[str(ctx.guild.id)]) player = await YTDLSource.from_url(self.music_queue[str(ctx.guild.id)][self.current][2], loop=self.bot.loop, stream=self.music_queue[str(ctx.guild.id)][self.current][3]) self.music_queue[str(ctx.guild.id)][self.current][0] = player await self.play_music_from_player(self.music_queue[str(ctx.guild.id)][self.current][1], player=player) - if not self.repeat_song: - self.popped += 1 await asyncio.sleep(0.5) return # ---------------------------------------------------------------------------------------------------------------------- @@ -622,7 +615,6 @@ async def remove_command(self, ctx, pos): self.music_queue[str(ctx.guild.id)].pop(int(pos)) if self.current > pos: self.current -= 1 - self.popped -= 1 elif self.current == pos: self.repeat_song = False self.current -= 1 @@ -678,7 +670,6 @@ async def jump_command(self, ctx, pos): name="Queue Looping", value="On" if self.loop_queue else "Off", inline=True) await ctx.send(embed=embed) self.repeat_song = False - self.popped = pos self.current = pos - 1 ctx.voice_client.stop() # ---------------------------------------------------------------------------------------------------------------------- @@ -707,7 +698,6 @@ async def volume_command(self, ctx, volume: int): async def stop_command(self, ctx): self.create_guild_queue(ctx) self.current = -1 - self.popped = 0 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -750,8 +740,8 @@ async def resume_command(self, ctx): return # ---------------------------------------------------------------------------------------------------------------------- - @commands.command(name="skip", aliases=["next"], help="skips the currently playing song") - async def skip_command(self, ctx): + @commands.command(name="next", aliases=["skip"], help="plays the next song in the queue") + async def next_command(self, ctx): self.create_guild_queue(ctx) if ctx.voice_client is None: async with ctx.typing(): @@ -759,24 +749,53 @@ async def skip_command(self, ctx): await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - async with ctx.typing(): - embed = discord.Embed( - title="Skipping", - colour=0x00ff00, - timestamp=datetime.datetime.utcnow() - ) - player = self.currently_playing_player - embed.set_thumbnail(url=self.MUSIC_ICON) - embed.set_author( - name=player.title, url=player.url, icon_url=ctx.author.avatar_url) - embed.add_field( - name="Title", value=player.title, inline=False) - embed.add_field(name="Requested by", - value=ctx.author.mention, inline=False) - embed.set_footer(text="skip requested by "+ctx.author.name) - await ctx.send(embed=embed) + # async with ctx.typing(): + # embed = discord.Embed( + # title="Skipping", + # colour=0x00ff00, + # timestamp=datetime.datetime.utcnow() + # ) + # player = self.currently_playing_player + # embed.set_thumbnail(url=self.MUSIC_ICON) + # embed.set_author( + # name=player.title, url=player.url, icon_url=ctx.author.avatar_url) + # embed.add_field( + # name="Title", value=player.title, inline=False) + # embed.add_field(name="Requested by", + # value=ctx.author.mention, inline=False) + # embed.set_footer(text="skip requested by "+ctx.author.name) + # await ctx.send(embed=embed) + self.repeat_song = False + ctx.voice_client.stop() + return + # ---------------------------------------------------------------------------------------------------------------------- + + @commands.command(name="previous", aliases=["prev"], help="plays the previous song in the queue") + async def next_command(self, ctx): + self.create_guild_queue(ctx) + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + else: + if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): + # async with ctx.typing(): + # embed = discord.Embed( + # title="Skipping", + # colour=0x00ff00, + # timestamp=datetime.datetime.utcnow() + # ) + # player = self.currently_playing_player + # embed.set_thumbnail(url=self.MUSIC_ICON) + # embed.set_author( + # name=player.title, url=player.url, icon_url=ctx.author.avatar_url) + # embed.add_field( + # name="Title", value=player.title, inline=False) + # embed.add_field(name="Requested by", + # value=ctx.author.mention, inline=False) + # embed.set_footer(text="skip requested by "+ctx.author.name) + # await ctx.send(embed=embed) self.repeat_song = False - self.popped += 1 ctx.voice_client.stop() return # ---------------------------------------------------------------------------------------------------------------------- @@ -850,5 +869,6 @@ async def lyrics_command(self, ctx, *args) -> None: await ctx.send(embed=embed) # ---------------------------------------------------------------------------------------------------------------------- + def setup(bot): bot.add_cog(Music(bot)) From a57d7e5d3e1a35a368a839d704759f814f5c72c7 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 21:20:54 +0530 Subject: [PATCH 21/28] using discord.ui.Button --- src/cogs/music.py | 73 ++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 3c797bf9..c4bc7afb 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -5,6 +5,7 @@ import typing import datetime from discord.ext import commands +from discord.ui import Button, Paginator, View # Suppress noise about console usage from errors youtube_dl.utils.bug_reports_message = lambda: '' @@ -97,7 +98,7 @@ def __init__(self, bot): self.currently_playing_music = () self.currently_playing_player = None self.music_queue = {} - self.current = -1 + self.current = 0 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -163,7 +164,7 @@ async def leave_command(self, ctx): self.create_guild_queue(ctx) self.music_queue[str(ctx.guild.id)].clear() self.currently_playing_music = None - self.current = -1 + self.current = 0 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -181,6 +182,7 @@ async def play_music_from_player(self, ctx, *, player): return self.currently_playing_player = player async with ctx.typing(): + # Embed embed = discord.Embed( title="Now Playing", description="- requested by " + @@ -195,7 +197,23 @@ async def play_music_from_player(self, ctx, *, player): embed.add_field(name="Position in queue", value=self.current+1, inline=False) embed.add_field(name="Volume", value=str(self.vol * 100) + "%", inline=False) - await ctx.send(embed=embed) + # View + restart_btn = Button() + previous_btn = Button() + play_btn = Button() + pause_btn = Button() + next_btn = Button() + repeat_song_btn = Button() + loop_queue_btn = Button() + view = discord.ui.View() + view.add_item(restart_btn) + view.add_item(previous_btn) + view.add_item(play_btn) + view.add_item(pause_btn) + view.add_item(next_btn) + view.add_item(repeat_song_btn) + view.add_item(loop_queue_btn) + await ctx.send(embed=embed, view=view) ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) ctx.voice_client.source.volume = self.vol @@ -506,6 +524,19 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): return # ---------------------------------------------------------------------------------------------------------------------- + @commands.command(name='restart', help="restarts the currently playing song") + async def restart_command(self, ctx): + self.create_guild_queue(ctx) + if ctx.voice_client is None: + async with ctx.typing(): + embed = self.embed_error_no_vc_dex + await ctx.send(embed=embed) + return + self.current -= (1 if not self.repeat_song else 0) + ctx.voice_client.stop() + return + # ---------------------------------------------------------------------------------------------------------------------- + @commands.command(name="queue", aliases=["view"], help="displays the current queue") async def queue_command(self, ctx, *, url: typing.Optional[str]): @@ -697,7 +728,7 @@ async def volume_command(self, ctx, volume: int): @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") async def stop_command(self, ctx): self.create_guild_queue(ctx) - self.current = -1 + self.current = 0 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -749,22 +780,7 @@ async def next_command(self, ctx): await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - # async with ctx.typing(): - # embed = discord.Embed( - # title="Skipping", - # colour=0x00ff00, - # timestamp=datetime.datetime.utcnow() - # ) - # player = self.currently_playing_player - # embed.set_thumbnail(url=self.MUSIC_ICON) - # embed.set_author( - # name=player.title, url=player.url, icon_url=ctx.author.avatar_url) - # embed.add_field( - # name="Title", value=player.title, inline=False) - # embed.add_field(name="Requested by", - # value=ctx.author.mention, inline=False) - # embed.set_footer(text="skip requested by "+ctx.author.name) - # await ctx.send(embed=embed) + self.current += 0 self.repeat_song = False ctx.voice_client.stop() return @@ -779,22 +795,7 @@ async def next_command(self, ctx): await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - # async with ctx.typing(): - # embed = discord.Embed( - # title="Skipping", - # colour=0x00ff00, - # timestamp=datetime.datetime.utcnow() - # ) - # player = self.currently_playing_player - # embed.set_thumbnail(url=self.MUSIC_ICON) - # embed.set_author( - # name=player.title, url=player.url, icon_url=ctx.author.avatar_url) - # embed.add_field( - # name="Title", value=player.title, inline=False) - # embed.add_field(name="Requested by", - # value=ctx.author.mention, inline=False) - # embed.set_footer(text="skip requested by "+ctx.author.name) - # await ctx.send(embed=embed) + self.current -= 2 self.repeat_song = False ctx.voice_client.stop() return From 09d19abb4d9ab853b5fd76c2ae2f40445a980f8c Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 21:38:45 +0530 Subject: [PATCH 22/28] sadly, `discord.ui` needs `dpy>=2.0` --- src/cogs/music.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index c4bc7afb..7536efe2 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -5,7 +5,6 @@ import typing import datetime from discord.ext import commands -from discord.ui import Button, Paginator, View # Suppress noise about console usage from errors youtube_dl.utils.bug_reports_message = lambda: '' @@ -198,22 +197,22 @@ async def play_music_from_player(self, ctx, *, player): value=self.current+1, inline=False) embed.add_field(name="Volume", value=str(self.vol * 100) + "%", inline=False) # View - restart_btn = Button() - previous_btn = Button() - play_btn = Button() - pause_btn = Button() - next_btn = Button() - repeat_song_btn = Button() - loop_queue_btn = Button() - view = discord.ui.View() - view.add_item(restart_btn) - view.add_item(previous_btn) - view.add_item(play_btn) - view.add_item(pause_btn) - view.add_item(next_btn) - view.add_item(repeat_song_btn) - view.add_item(loop_queue_btn) - await ctx.send(embed=embed, view=view) + # restart_btn = Button() + # previous_btn = Button() + # play_btn = Button() + # pause_btn = Button() + # next_btn = Button() + # repeat_song_btn = Button() + # loop_queue_btn = Button() + # view = discord.ui.View() + # view.add_item(restart_btn) + # view.add_item(previous_btn) + # view.add_item(play_btn) + # view.add_item(pause_btn) + # view.add_item(next_btn) + # view.add_item(repeat_song_btn) + # view.add_item(loop_queue_btn) + await ctx.send(embed=embed) ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) ctx.voice_client.source.volume = self.vol From a60664f48628486423ef99420a8ff80d6e03ca8b Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 21:44:52 +0530 Subject: [PATCH 23/28] Update music.py --- src/cogs/music.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 7536efe2..810f42b7 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -786,7 +786,7 @@ async def next_command(self, ctx): # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="previous", aliases=["prev"], help="plays the previous song in the queue") - async def next_command(self, ctx): + async def previous_command(self, ctx): self.create_guild_queue(ctx) if ctx.voice_client is None: async with ctx.typing(): From 3e485245bfc0b57383935c19b0eed16546af075e Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 21:48:21 +0530 Subject: [PATCH 24/28] Update music.py --- src/cogs/music.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 810f42b7..a12fce79 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -97,7 +97,7 @@ def __init__(self, bot): self.currently_playing_music = () self.currently_playing_player = None self.music_queue = {} - self.current = 0 + self.current = -1 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -163,7 +163,7 @@ async def leave_command(self, ctx): self.create_guild_queue(ctx) self.music_queue[str(ctx.guild.id)].clear() self.currently_playing_music = None - self.current = 0 + self.current = -1 self.queued = 0 self.vol = 1 self.loop_queue = False @@ -727,7 +727,7 @@ async def volume_command(self, ctx, volume: int): @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") async def stop_command(self, ctx): self.create_guild_queue(ctx) - self.current = 0 + self.current = -1 self.queued = 0 self.vol = 1 self.loop_queue = False From e1b6e5bd1c0b1737cf0c61aebb1e5cc807b747d3 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 22:29:55 +0530 Subject: [PATCH 25/28] Update music.py --- src/cogs/music.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index a12fce79..4466ef44 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -195,7 +195,7 @@ async def play_music_from_player(self, ctx, *, player): embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Position in queue", value=self.current+1, inline=False) - embed.add_field(name="Volume", value=str(self.vol * 100) + "%", inline=False) + embed.add_field(name="Volume", value=str(int(self.vol * 100)) + "%", inline=False) # View # restart_btn = Button() # previous_btn = Button() @@ -779,9 +779,19 @@ async def next_command(self, ctx): await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - self.current += 0 - self.repeat_song = False - ctx.voice_client.stop() + if self.current < len(self.music_queue[str(ctx.guild.id)]) - 1 or self.loop_queue: + self.current += 0 + self.repeat_song = False + ctx.voice_client.stop() + else: + async with ctx.typing(): + embed=discord.Embed( + title="Error", + description="Nothing to play after this", + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) return # ---------------------------------------------------------------------------------------------------------------------- @@ -794,9 +804,19 @@ async def previous_command(self, ctx): await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - self.current -= 2 - self.repeat_song = False - ctx.voice_client.stop() + if self.current > 0 or self.loop_queue: + self.current -= 2 + self.repeat_song = False + ctx.voice_client.stop() + else: + async with ctx.typing(): + embed=discord.Embed( + title="Error", + description="Nothing to play before this", + colour=0xff0000, + timestamp=datetime.datetime.utcnow() + ) + await ctx.send(embed=embed) return # ---------------------------------------------------------------------------------------------------------------------- From b16de19db3635f78c1d146d06b08be2dcfc8711e Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sat, 25 Jun 2022 23:49:36 +0530 Subject: [PATCH 26/28] bugfixes --- src/cogs/music.py | 248 +++++++++++++++++++++++++--------------------- 1 file changed, 133 insertions(+), 115 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 4466ef44..37d6609a 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -62,9 +62,6 @@ async def from_url(cls, url, *, loop=None, stream=False): class Music(commands.Cog): - # queue format: - # [guild.id] -> [0 player | 1 ctx | 2 url(from_user) | 3 stream_or_not] - bad_request_error_message = '' bad_request_error_message += ( ''.join("Bad response while searching for the music\n\n")) @@ -92,30 +89,39 @@ class Music(commands.Cog): def __init__(self, bot): self.bot = bot - self.is_playing = False - self.MUSIC_ICON = "https://user-images.githubusercontent.com/63065397/156855077-ce6e0896-cc81-4d4d-98b8-3e7b70050afe.png" - self.currently_playing_music = () - self.currently_playing_player = None + # ------------------------------------- + self.properties = {} + # "guild_id": str -> { + # "is_playing": False, + # "currently_playing_player": None, + # "current": -1, + # "queued": 0, + # "vol": 1, + # "loop_queue": False, + # "repeat_song": False + # } + # ------------------------------------- self.music_queue = {} - self.current = -1 - self.queued = 0 - self.vol = 1 - self.loop_queue = False - self.repeat_song = False - # self.destroy() - # ---------------------------------------------------------------------------------------------------------------------- - - # async def destroy(self, bot): - # while(bot.user.voice.channel is not None): - # await asyncio.sleep(1) - # self.__del__() + # [guild.id] -> [0 player | 1 ctx | 2 url(from_user) | 3 stream_or_not] + # ------------------------------------- + return # ---------------------------------------------------------------------------------------------------------------------- - + # async def on_voice_state_update(self): # pass # ---------------------------------------------------------------------------------------------------------------------- - - def create_guild_queue(self,ctx): + + def add_guild(self, ctx): + if str(ctx.guild.id) not in self.properties: + self.properties[str(ctx.guild.id)] = { + "is_playing": False, + "currently_playing_player": None, + "current": -1, + "queued": 0, + "vol": 1, + "loop_queue": False, + "repeat_song": False + } if str(ctx.guild.id) not in self.music_queue.keys(): self.music_queue[str(ctx.guild.id)] = [] return @@ -123,7 +129,7 @@ def create_guild_queue(self,ctx): @commands.command(name="join", aliases=["connect"], help="joins the voice channel of the author") async def join_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.author.voice is None: async with ctx.typing(): embed = discord.Embed( @@ -160,15 +166,14 @@ async def join_command(self, ctx): @commands.command(name="leave", aliases=["disconnect, dc"], help="leaves if connected to any voice channel") async def leave_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) self.music_queue[str(ctx.guild.id)].clear() - self.currently_playing_music = None - self.current = -1 - self.queued = 0 - self.vol = 1 - self.loop_queue = False - self.repeat_song = False - self.currently_playing_player = None + self.properties[str(ctx.guild.id)]["current"] = -1 + self.properties[str(ctx.guild.id)]["queued"] = 0 + self.properties[str(ctx.guild.id)]["vol"] = 1 + self.properties[str(ctx.guild.id)]["loop_queue"] = False + self.properties[str(ctx.guild.id)]["repeat_song"] = False + self.properties[str(ctx.guild.id)]["currently_playing_player"] = None if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -179,23 +184,25 @@ async def leave_command(self, ctx): async def play_music_from_player(self, ctx, *, player): if player is None: return - self.currently_playing_player = player + self.properties[str(ctx.guild.id)]["currently_playing_player"] = player async with ctx.typing(): # Embed embed = discord.Embed( title="Now Playing", description="- requested by " + - self.music_queue[str(ctx.guild.id)][self.current][1].author.mention, + self.music_queue[str(ctx.guild.id) + ][self.properties[str(ctx.guild.id)]["current"]][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name=player.title, url=player.url, - icon_url=ctx.author.avatar_url) + icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field(name="Position in queue", - value=self.current+1, inline=False) - embed.add_field(name="Volume", value=str(int(self.vol * 100)) + "%", inline=False) + value=self.properties[str(ctx.guild.id)]["current"]+1, inline=False) + embed.add_field(name="Volume", value=str( + int(self.properties[str(ctx.guild.id)]["vol"] * 100)) + "%", inline=False) # View # restart_btn = Button() # previous_btn = Button() @@ -215,26 +222,29 @@ async def play_music_from_player(self, ctx, *, player): await ctx.send(embed=embed) ctx.voice_client.play(player, after=lambda e: print( f'Player error: {e}') if e else None) - ctx.voice_client.source.volume = self.vol + ctx.voice_client.source.volume = self.properties[str( + ctx.guild.id)]["vol"] # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): - while ((len(self.music_queue[str(ctx.guild.id)]) - self.current > 1) or (self.loop_queue is True)) and (len(self.music_queue[str(ctx.guild.id)]) > 0): + while ((len(self.music_queue[str(ctx.guild.id)]) - self.properties[str(ctx.guild.id)]["current"] > 1) or (self.properties[str(ctx.guild.id)]["loop_queue"] is True)) and (len(self.music_queue[str(ctx.guild.id)]) > 0): if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): - self.is_playing = True - if (not self.repeat_song): - self.current += 1 - self.current %= len(self.music_queue[str(ctx.guild.id)]) - player = await YTDLSource.from_url(self.music_queue[str(ctx.guild.id)][self.current][2], loop=self.bot.loop, stream=self.music_queue[str(ctx.guild.id)][self.current][3]) - self.music_queue[str(ctx.guild.id)][self.current][0] = player - await self.play_music_from_player(self.music_queue[str(ctx.guild.id)][self.current][1], player=player) + self.properties[str(ctx.guild.id)]["is_playing"] = True + if (not self.properties[str(ctx.guild.id)]["repeat_song"]): + self.properties[str(ctx.guild.id)]["current"] += 1 + self.properties[str(ctx.guild.id)]["current"] %= len( + self.music_queue[str(ctx.guild.id)]) + player = await YTDLSource.from_url(self.music_queue[str(ctx.guild.id)][self.properties[str(ctx.guild.id)]["current"]][2], loop=self.bot.loop, stream=self.music_queue[str(ctx.guild.id)][self.properties[str(ctx.guild.id)]["current"]][3]) + self.music_queue[str(ctx.guild.id)][self.properties[str( + ctx.guild.id)]["current"]][0] = player + await self.play_music_from_player(self.music_queue[str(ctx.guild.id)][self.properties[str(ctx.guild.id)]["current"]][1], player=player) await asyncio.sleep(0.5) return # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="play", aliases=["stream", "p", "add"], help="streams a song directly from youtube") async def play_command(self, ctx, *, url: typing.Optional[str]): - self.create_guild_queue(ctx) + self.add_guild(ctx) if (url is None) and (ctx.message.content[(len(ctx.message.content)-3):(len(ctx.message.content))] != "add"): if ctx.voice_client is None: await self.join_command(ctx) @@ -285,7 +295,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): await ctx.send(embed=embed) return self.music_queue[str(ctx.guild.id)].append([player, ctx, url, True]) - self.queued += 1 + self.properties[str(ctx.guild.id)]["queued"] += 1 async with ctx.typing(): embed = discord.Embed( title="Added to queue", @@ -306,7 +316,7 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): @commands.command(name="playm", aliases=["streamm", "pm", "addm"], help="plays multiple songs (seperated by semicolons ';')") async def playm_command(self, ctx, *, args): - self.create_guild_queue(ctx) + self.add_guild(ctx) urls = args.split(';') joined = await self.join_command(ctx) if joined == False: @@ -325,8 +335,9 @@ async def playm_command(self, ctx, *, args): ) await ctx.send(embed=embed) continue - self.music_queue[str(ctx.guild.id)].append([player, ctx, url, True]) - self.queued += 1 + self.music_queue[str(ctx.guild.id)].append( + [player, ctx, url, True]) + self.properties[str(ctx.guild.id)]["queued"] += 1 async with ctx.typing(): embed = discord.Embed( title="Added to queue", @@ -347,7 +358,7 @@ async def playm_command(self, ctx, *, args): @commands.command(name='dplay', help="downloads a song and then queues it to reduce any possible lags") async def dplay_command(self, ctx, *, url): - self.create_guild_queue(ctx) + self.add_guild(ctx) joined = await self.join_command(ctx) if joined == False: return @@ -363,7 +374,7 @@ async def dplay_command(self, ctx, *, url): await ctx.send(embed=embed) return self.music_queue[str(ctx.guild.id)].append([player, ctx, url, False]) - self.queued += 1 + self.properties[str(ctx.guild.id)]["queued"] += 1 async with ctx.typing(): embed = discord.Embed( title="Downloaded & Added to queue", @@ -384,12 +395,12 @@ async def dplay_command(self, ctx, *, url): @commands.command(name='dplaym', help="dplays multiple songs (seperated by semicolons ';')") async def dplaym_command(self, ctx, *, args): - self.create_guild_queue(ctx) + self.add_guild(ctx) urls = args.split(';') joined = await self.join_command(ctx) if joined == False: return - last_url=urls.pop() + last_url = urls.pop() for url in urls: url = url.strip() player = await YTDLSource.from_url(url, loop=self.bot.loop) @@ -403,8 +414,9 @@ async def dplaym_command(self, ctx, *, args): ) await ctx.send(embed=embed) continue - self.music_queue[str(ctx.guild.id)].append([player, ctx, url, False]) - self.queued += 1 + self.music_queue[str(ctx.guild.id)].append( + [player, ctx, url, False]) + self.properties[str(ctx.guild.id)]["queued"] += 1 async with ctx.typing(): embed = discord.Embed( title="Downloaded & Added to queue", @@ -426,8 +438,8 @@ async def dplaym_command(self, ctx, *, args): @commands.command(name='loop', help="toggles looping of the queue") async def loop_command(self, ctx, loop_switch: typing.Optional[str]): - self.create_guild_queue(ctx) - + self.add_guild(ctx) + if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -435,15 +447,16 @@ async def loop_command(self, ctx, loop_switch: typing.Optional[str]): return if loop_switch is None: - self.loop_queue = not self.loop_queue - if self.loop_queue: + self.properties[str(ctx.guild.id)]["loop_queue"] = not self.properties[str( + ctx.guild.id)]["loop_queue"] + if self.properties[str(ctx.guild.id)]["loop_queue"]: loop_switch = "on" else: loop_switch = "off" elif loop_switch.lower() == "on": - self.loop_queue = True + self.properties[str(ctx.guild.id)]["loop_queue"] = True elif loop_switch.lower() == "off": - self.loop_queue = False + self.properties[str(ctx.guild.id)]["loop_queue"] = False else: async with ctx.typing(): embed = discord.Embed( @@ -476,8 +489,8 @@ async def loop_command(self, ctx, loop_switch: typing.Optional[str]): @commands.command(name='repeat', help="toggles repeating of the currently playing song") async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): - self.create_guild_queue(ctx) - + self.add_guild(ctx) + if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -485,15 +498,16 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): return if repeat_switch is None: - self.repeat_song = not self.repeat_song - if self.repeat_song: + self.properties[str(ctx.guild.id)]["repeat_song"] = not self.properties[str( + ctx.guild.id)]["repeat_song"] + if self.properties[str(ctx.guild.id)]["repeat_song"]: repeat_switch = "on" else: repeat_switch = "off" elif repeat_switch.lower() == "on": - self.repeat_song = True + self.properties[str(ctx.guild.id)]["repeat_song"] = True elif repeat_switch.lower() == "off": - self.repeat_song = False + self.properties[str(ctx.guild.id)]["repeat_song"] = False else: async with ctx.typing(): embed = discord.Embed( @@ -525,25 +539,23 @@ async def repeat_command(self, ctx, repeat_switch: typing.Optional[str]): @commands.command(name='restart', help="restarts the currently playing song") async def restart_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) return - self.current -= (1 if not self.repeat_song else 0) + self.properties[str(ctx.guild.id)]["current"] -= ( + 1 if not self.properties[str(ctx.guild.id)]["repeat_song"] else 0) ctx.voice_client.stop() return # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="queue", aliases=["view"], help="displays the current queue") async def queue_command(self, ctx, *, url: typing.Optional[str]): - - if str(ctx.guild.id) in self.music_queue: # for debugging purposes - print(self.music_queue[str(ctx.guild.id)]) - - self.create_guild_queue(ctx) - + + self.add_guild(ctx) + if url is not None: if url != "": await self.play_command(ctx, url=url) @@ -577,17 +589,21 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): for i in range(0, size, 25): embed = discord.Embed( title="Queue", - description=str("Page " + str(i // 25 + 1) + " of " + str(size // 25 + 1)), + description=str("Page " + str(i // 25 + 1) + + " of " + str(size // 25 + 1)), colour=0x0000ff, timestamp=datetime.datetime.utcnow() ) embed.set_thumbnail(url=self.MUSIC_ICON) embed.set_author(name="Dex", icon_url=self.bot.user.avatar_url) for j in range(i, min(size, i + 25)): - k = "**" if j == self.current else "" + k = "**" if j == self.properties[str( + ctx.guild.id)]["current"] else "" embed.add_field( - name=str(j + 1) + (" ***(Currently Playing)***" if j == self.current else ""), - value=k+str(self.music_queue[str(ctx.guild.id)][j][0].title)+k, + name=str( + j + 1) + (" ***(Currently Playing)***" if j == self.properties[str(ctx.guild.id)]["current"] else ""), + value=k + + str(self.music_queue[str(ctx.guild.id)][j][0].title)+k, inline=False ) async with ctx.typing(): @@ -598,7 +614,7 @@ async def queue_command(self, ctx, *, url: typing.Optional[str]): @commands.command(name="remove", help="removes a song from the queue, takes song position as argument") async def remove_command(self, ctx, pos): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -631,7 +647,8 @@ async def remove_command(self, ctx, pos): embed = discord.Embed( title="Removed from queue", description="track requested by " + - self.music_queue[str(ctx.guild.id)][int(pos)][1].author.mention, + self.music_queue[str(ctx.guild.id)][int(pos) + ][1].author.mention, colour=0x00ff00, timestamp=datetime.datetime.utcnow() ) @@ -643,18 +660,18 @@ async def remove_command(self, ctx, pos): embed.add_field(name="Remove request by", value=ctx.author.mention, inline=True) self.music_queue[str(ctx.guild.id)].pop(int(pos)) - if self.current > pos: - self.current -= 1 - elif self.current == pos: - self.repeat_song = False - self.current -= 1 + if self.properties[str(ctx.guild.id)]["current"] > pos: + self.properties[str(ctx.guild.id)]["current"] -= 1 + elif self.properties[str(ctx.guild.id)]["current"] == pos: + self.properties[str(ctx.guild.id)]["repeat_song"] = False + self.properties[str(ctx.guild.id)]["current"] -= 1 ctx.voice_client.stop() await ctx.send(embed=embed) # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="jump", alises=["jumpto"], help="jumps to a song in the queue, takes song position as argument") async def jump_command(self, ctx, pos): - self.create_guild_queue(ctx) + self.add_guild(ctx) pos = int(pos) if ctx.voice_client is None: async with ctx.typing(): @@ -697,22 +714,23 @@ async def jump_command(self, ctx, pos): icon_url=ctx.author.avatar_url) embed.add_field(name="Title", value=player.title, inline=False) embed.add_field( - name="Queue Looping", value="On" if self.loop_queue else "Off", inline=True) + name="Queue Looping", value="On" if self.properties[str(ctx.guild.id)]["loop_queue"] else "Off", inline=True) await ctx.send(embed=embed) - self.repeat_song = False - self.current = pos - 1 + self.properties[str(ctx.guild.id)]["repeat_song"] = False + self.properties[str(ctx.guild.id)]["current"] = pos - 1 ctx.voice_client.stop() # ---------------------------------------------------------------------------------------------------------------------- @commands.command(name="volume", aliases=["vol"], help="changes the volume of the music player") async def volume_command(self, ctx, volume: int): + self.add_guild(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) return ctx.voice_client.source.volume = volume / 100 - self.vol = volume / 100 + self.properties[str(ctx.guild.id)]["vol"] = volume / 100 async with ctx.typing(): embed = discord.Embed( title=str(volume) + "%", @@ -726,14 +744,13 @@ async def volume_command(self, ctx, volume: int): @commands.command(name="stop", aliases=["stfu", "shut"], help="stops the music player and clears the queue") async def stop_command(self, ctx): - self.create_guild_queue(ctx) - self.current = -1 - self.queued = 0 - self.vol = 1 - self.loop_queue = False - self.repeat_song = False - self.currently_playing_music = None - self.currently_playing_player = None + self.add_guild(ctx) + self.properties[str(ctx.guild.id)]["current"] = -1 + self.properties[str(ctx.guild.id)]["queued"] = 0 + self.properties[str(ctx.guild.id)]["vol"] = 1 + self.properties[str(ctx.guild.id)]["loop_queue"] = False + self.properties[str(ctx.guild.id)]["repeat_song"] = False + self.properties[str(ctx.guild.id)]["currently_playing_player"] = None if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex @@ -747,7 +764,7 @@ async def stop_command(self, ctx): @commands.command(name="pause", help="pauses the music player") async def pause_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -758,7 +775,7 @@ async def pause_command(self, ctx): @commands.command(name="resume", help="resumes the music player") async def resume_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -772,20 +789,20 @@ async def resume_command(self, ctx): @commands.command(name="next", aliases=["skip"], help="plays the next song in the queue") async def next_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - if self.current < len(self.music_queue[str(ctx.guild.id)]) - 1 or self.loop_queue: - self.current += 0 - self.repeat_song = False + if self.properties[str(ctx.guild.id)]["current"] < len(self.music_queue[str(ctx.guild.id)]) - 1 or self.properties[str(ctx.guild.id)]["loop_queue"]: + self.properties[str(ctx.guild.id)]["current"] += 0 + self.properties[str(ctx.guild.id)]["repeat_song"] = False ctx.voice_client.stop() else: async with ctx.typing(): - embed=discord.Embed( + embed = discord.Embed( title="Error", description="Nothing to play after this", colour=0xff0000, @@ -797,20 +814,20 @@ async def next_command(self, ctx): @commands.command(name="previous", aliases=["prev"], help="plays the previous song in the queue") async def previous_command(self, ctx): - self.create_guild_queue(ctx) + self.add_guild(ctx) if ctx.voice_client is None: async with ctx.typing(): embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) else: if ctx.voice_client.is_playing() or ctx.voice_client.is_paused(): - if self.current > 0 or self.loop_queue: - self.current -= 2 - self.repeat_song = False + if self.properties[str(ctx.guild.id)]["current"] > 0 or self.properties[str(ctx.guild.id)]["loop_queue"]: + self.properties[str(ctx.guild.id)]["current"] -= 2 + self.properties[str(ctx.guild.id)]["repeat_song"] = False ctx.voice_client.stop() else: async with ctx.typing(): - embed=discord.Embed( + embed = discord.Embed( title="Error", description="Nothing to play before this", colour=0xff0000, @@ -830,14 +847,14 @@ async def get_lyrics(self, song_title): @commands.command(name='lyrics', help='sends the lyrics of the song') async def lyrics_command(self, ctx, *args) -> None: - self.create_guild_queue(ctx) + self.add_guild(ctx) song_title = '' for arg in args: song_title += arg+'%20' if len(song_title) > 0: song_title = song_title[:-3] else: - if self.currently_playing_player is None: + if self.properties[str(ctx.guild.id)]["currently_playing_player"] is None: async with ctx.typing(): embed = discord.Embed( title="Error", @@ -847,7 +864,8 @@ async def lyrics_command(self, ctx, *args) -> None: ) await ctx.send(embed=embed) return - args = self.currently_playing_player.title.split() + args = self.properties[str( + ctx.guild.id)]["currently_playing_player"].title.split() for arg in args: song_title += arg+'%20' song_title = song_title[:-3] From 340050f616e3f40a2e762b7a65105ec71b42c7cd Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sun, 26 Jun 2022 00:13:40 +0530 Subject: [PATCH 27/28] Update music.py --- src/cogs/music.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 37d6609a..b13c7a39 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -98,7 +98,8 @@ def __init__(self, bot): # "queued": 0, # "vol": 1, # "loop_queue": False, - # "repeat_song": False + # "repeat_song": False, + # "inside_keep_playing": False # } # ------------------------------------- self.music_queue = {} @@ -120,7 +121,8 @@ def add_guild(self, ctx): "queued": 0, "vol": 1, "loop_queue": False, - "repeat_song": False + "repeat_song": False, + "inside_keep_playing": False } if str(ctx.guild.id) not in self.music_queue.keys(): self.music_queue[str(ctx.guild.id)] = [] @@ -174,6 +176,8 @@ async def leave_command(self, ctx): self.properties[str(ctx.guild.id)]["loop_queue"] = False self.properties[str(ctx.guild.id)]["repeat_song"] = False self.properties[str(ctx.guild.id)]["currently_playing_player"] = None + self.properties[str(ctx.guild.id)]["is_playing"] = False + self.properties[str(ctx.guild.id)]["inside_keep_playing"] = False if ctx.voice_client is None: embed = self.embed_error_no_vc_dex await ctx.send(embed=embed) @@ -227,6 +231,7 @@ async def play_music_from_player(self, ctx, *, player): # ---------------------------------------------------------------------------------------------------------------------- async def keep_playing(self, ctx): + self.properties[str(ctx.guild.id)]["inside_keep_playing"] = True while ((len(self.music_queue[str(ctx.guild.id)]) - self.properties[str(ctx.guild.id)]["current"] > 1) or (self.properties[str(ctx.guild.id)]["loop_queue"] is True)) and (len(self.music_queue[str(ctx.guild.id)]) > 0): if ((not ctx.voice_client.is_playing()) and (not ctx.voice_client.is_paused())): self.properties[str(ctx.guild.id)]["is_playing"] = True @@ -239,6 +244,7 @@ async def keep_playing(self, ctx): ctx.guild.id)]["current"]][0] = player await self.play_music_from_player(self.music_queue[str(ctx.guild.id)][self.properties[str(ctx.guild.id)]["current"]][1], player=player) await asyncio.sleep(0.5) + self.properties[str(ctx.guild.id)]["inside_keep_playing"] = False return # ---------------------------------------------------------------------------------------------------------------------- @@ -256,7 +262,8 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): ctx.voice_client.resume() elif len(self.music_queue[str(ctx.guild.id)]) > 0: if not ctx.voice_client.is_playing(): - await self.keep_playing(ctx) + if not self.properties[str(ctx.guild.id)]["inside_keep_playing"]: + await self.keep_playing(ctx) else: embed = discord.Embed( title="Error", @@ -310,7 +317,8 @@ async def play_command(self, ctx, *, url: typing.Optional[str]): embed.add_field(name="Queue Position", value=len( self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + if not self.properties[str(ctx.guild.id)]["inside_keep_playing"]: + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @@ -389,7 +397,8 @@ async def dplay_command(self, ctx, *, url): embed.add_field(name="Queue Position", value=len( self.music_queue[str(ctx.guild.id)]), inline=True) await ctx.send(embed=embed) - await self.keep_playing(ctx) + if not self.properties[str(ctx.guild.id)]["inside_keep_playing"]: + await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- @@ -781,8 +790,8 @@ async def resume_command(self, ctx): await ctx.send(embed=embed) elif ctx.voice_client.is_paused(): ctx.voice_client.resume() - elif len(self.music_queue[str(ctx.guild.id)]) > 0: - if not ctx.voice_client.is_playing(): + elif not ctx.voice_client.is_playing(): + if not self.properties[str(ctx.guild.id)]["inside_keep_playing"]: await self.keep_playing(ctx) return # ---------------------------------------------------------------------------------------------------------------------- From bea8d00a0971975a36ae6ac911d8e83d0f2121c0 Mon Sep 17 00:00:00 2001 From: Yashvardhan Baid Date: Sun, 26 Jun 2022 01:03:12 +0530 Subject: [PATCH 28/28] Update music.py --- src/cogs/music.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index b13c7a39..8ec1a7ae 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -91,7 +91,8 @@ def __init__(self, bot): self.bot = bot # ------------------------------------- self.properties = {} - # "guild_id": str -> { + # FORMAT OF DICT self.properties: + # [str(guild.id)] -> { # "is_playing": False, # "currently_playing_player": None, # "current": -1, @@ -103,7 +104,8 @@ def __init__(self, bot): # } # ------------------------------------- self.music_queue = {} - # [guild.id] -> [0 player | 1 ctx | 2 url(from_user) | 3 stream_or_not] + # FORMAT OF DICT self.music_queue: + # [str(guild.id)] -> [0 player | 1 ctx | 2 url(from_user) | 3 stream_or_not] # ------------------------------------- return # ----------------------------------------------------------------------------------------------------------------------