Add clear command to music bot

This commit is contained in:
CramMK
2020-04-29 10:50:19 +02:00
parent b6e7c36781
commit fc2a0af7b9

View File

@@ -218,10 +218,7 @@ class VoiceState:
self.next.clear() self.next.clear()
if not self.loop: if not self.loop:
# Try to get the next song within 3 minutes. # 3 minute timeout
# If no song will be added to the queue in time,
# the player will disconnect due to performance
# reasons.
try: try:
async with timeout(180): # 3 minutes async with timeout(180): # 3 minutes
self.current = await self.songs.get() self.current = await self.songs.get()
@@ -349,6 +346,14 @@ class Music(commands.Cog):
await ctx.message.add_reaction('') await ctx.message.add_reaction('')
ctx.voice_state.skip() ctx.voice_state.skip()
@commands.command(name="clear")
async def clear(self, ctx: commands.Context):
"""
Clears the current queue
"""
ctx.voice_state.songs.clear()
await ctx.message.add_reaction('')
@commands.command(name='play') @commands.command(name='play')
async def play(self, ctx: commands.Context, *, search: str): async def play(self, ctx: commands.Context, *, search: str):
""" """
@@ -391,6 +396,7 @@ class Music(commands.Cog):
# ctx.voice_state.loop = not ctx.voice_state.loop # ctx.voice_state.loop = not ctx.voice_state.loop
# await ctx.message.add_reaction('✅') # await ctx.message.add_reaction('✅')
@commands.command(name="queue") @commands.command(name="queue")
async def queue(self, ctx: commands.Context): async def queue(self, ctx: commands.Context):
""" """
@@ -399,17 +405,22 @@ class Music(commands.Cog):
if len(ctx.voice_state.songs) == 0: if len(ctx.voice_state.songs) == 0:
return await ctx.send("The queue is empty.") return await ctx.send("The queue is empty.")
start = 0 page = 1
end = 9
queue = "" items_per_page = 10
pages = math.ceil(len(ctx.voice_state.songs) / items_per_page)
start = (page - 1) * items_per_page
end = start + items_per_page
queue = ''
for i, song in enumerate(ctx.voice_state.songs[start:end], start=start): for i, song in enumerate(ctx.voice_state.songs[start:end], start=start):
queue += '`{0}.` [**{1.source.title}**]({1.source.url})\n'.format(i + 1, song) queue += '`{0}.` [**{1.source.title}**]({1.source.url})\n'.format(i + 1, song)
embed = discord.Embed(description='**{} tracks:**\n\n{}'.format(len(ctx.voice_state.songs), queue)) embed = (discord.Embed(description='**{} tracks:**\n\n{}'.format(len(ctx.voice_state.songs), queue))
.set_footer(text='Viewing page {}/{}'.format(page, pages)))
await ctx.send(embed=embed) await ctx.send(embed=embed)
# COG ENDING # COG ENDING
def setup(bot): def setup(bot):
bot.add_cog(Music(bot)) bot.add_cog(Music(bot))