aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/skip.py
blob: 7bed9318c51a54222f4cb43f0f8cad0098f6c1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import discord
import datetime
from discord import app_commands
from discord.ext import commands
from cogs.music import Music
import asyncio

from utils.config import create_embed
from utils.custom_sources import LoadError


class Skip(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @app_commands.command()
    @app_commands.check(Music.create_player)
    @app_commands.describe(
        number="Optional: Number of songs to skip, default is 1"
    )
    async def skip(self, interaction: discord.Interaction, number: int = 1):
        "Skips the song that is currently playing"
        player = self.bot.lavalink.player_manager.get(interaction.guild.id)

        if number != 1:
            if number < 1:
                embed = create_embed(
                    title="Invalid Number",
                    description="The number option cannot be less than 1",
                )
                return await interaction.response.send_message(
                    embed=embed, ephemeral=True
                )

            elif number > len(player.queue):
                embed = create_embed(
                    title="Number too Large",
                    description=(
                        "The number you entered is larger than the number of"
                        " songs in queue. If you want to stop playing music"
                        " entirely, try the </stop:1224840890866991305>"
                        " command."
                    ),
                )
                return await interaction.response.send_message(
                    embed=embed, ephemeral=True
                )
            else:
                for i in range(number - 2, -1, -1):
                    player.queue.pop(i)

        # If the queue is empty, but the current song is on repeat
        if player.loop == 1 and not player.queue:
            embed = create_embed(
                title="Song on Repeat",
                description=(
                    "There is nothing in queue, but the current song is on"
                    " repeat. Use </stop:1224840890866991305> to stop"
                    " playing music."
                ),
            )
            return await interaction.response.send_message(
                embed=embed, ephemeral=True
            )
        else:
            pass

        # Skip current track, continue skipping on LoadError
        while True:
            try:
                await player.skip()
                break
            except LoadError as e:
                continue

        if not player.current:
            embed = create_embed(
                title="End of Queue",
                description=(
                    "I have left the voice channel as all songs in the queue"
                    " have been played.\n\n"
                    f"Issued by: {interaction.user.mention}"
                ),
            )
            return await interaction.response.send_message(embed=embed)

        # It takes a sec for the new track to be grabbed and played
        # So just wait a sec before sending the message
        await asyncio.sleep(0.5)
        embed = create_embed(
            title=(
                f"{'Track Skipped' if number == 1 else f'{number} Tracks Skipped'}"
            ),
            description=(
                f"**[{player.current.title}]({player.current.uri})**"
                f" by **{player.current.author}** is now playing\n\n"
                f"Issued by: {interaction.user.mention}"
            ),
            thumbnail=player.current.artwork_url,
        )
        await interaction.response.send_message(embed=embed)


async def setup(bot):
    await bot.add_cog(Skip(bot))