Fixing loop error and adding automatic cleaning of bot messages

This commit is contained in:
Rafael Vargas 2022-01-03 21:39:58 -04:00
parent e5b88727a6
commit fe51f13f13

View File

@ -25,18 +25,12 @@ class Music(commands.Cog):
'options': '-vn'} 'options': '-vn'}
def __play_next(self, error, ctx): def __play_next(self, error, ctx):
while True:
if len(self.__playlist) > 0:
source = self.__playlist.next_song() source = self.__playlist.next_song()
if source == None: # If there is not a source for the song if source != None: # If there is not a source for the song
continue
coro = self.__play_music(ctx, source) coro = self.__play_music(ctx, source)
self.__bot.loop.create_task(coro) self.__bot.loop.create_task(coro)
break
else: else:
self.__playing = False self.__playing = False
break
async def __play_music(self, ctx, song): async def __play_music(self, ctx, song):
self.__playing = True self.__playing = True
@ -200,6 +194,7 @@ class Music(commands.Cog):
title = 'Song Playing Now' title = 'Song Playing Now'
current_song = self.__playlist.current current_song = self.__playlist.current
await self.__clean_messages(ctx)
await ctx.send(embed=current_song.embed(title=title)) await ctx.send(embed=current_song.embed(title=title))
@commands.command(name='shuffle', help=config.HELP_SHUFFLE) @commands.command(name='shuffle', help=config.HELP_SHUFFLE)
@ -244,6 +239,21 @@ class Music(commands.Cog):
) )
await ctx.send(embed=embedvc) await ctx.send(embed=embedvc)
async def __clean_messages(self, ctx):
"""Clear Bot messages if send recently"""
last_messages = await ctx.channel.history(limit=5).flatten()
for message in last_messages:
try:
if message.author == self.__bot.user:
if len(message.embeds) > 0:
embed = message.embeds[0]
if embed.title == 'Song Playing Now':
await message.delete()
except Exception as e:
print(e)
continue
def setup(bot): def setup(bot):
bot.add_cog(Music(bot)) bot.add_cog(Music(bot))