aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/slash_handlers.py
blob: 9f8f2010025f62b202da1c5c54a9cc2712795109 (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
import discord
from discord.ext import commands
from discord import app_commands
from discord.ext.commands.errors import *
import datetime

from global_variables import BOT_COLOR
from custom_source import LoadError


class slash_handlers(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        bot.tree.on_error = self.on_error

    async def on_error(self, interaction: discord.Interaction, error):
        music_commands = [
            "play",
            "clear",
            "np",
            "pause",
            "queue",
            "remove",
            "resume",
            "shuffle",
            "skip",
            "stop",
        ]

        if isinstance(error, CommandNotFound):
            return

        # elif isinstance(error, app_commands.MissingPermissions):
        #     embed = discord.Embed(
        #         title="Missing Permissions!",
        #         description=f"{error}",
        #         color=BOT_COLOR
        #     )
        #     await interaction.response.send_message(embed=embed, ephemeral=True)

        # elif isinstance(error, app_commands.BotMissingPermissions):
        #     embed = discord.Embed(
        #         title="Bot Missing Permissions!",
        #         description=f"{error}",
        #         color=BOT_COLOR
        #     )
        #     await interaction.response.send_message(embed=embed, ephemeral=True)

        elif isinstance(error, app_commands.AppCommandError):
            embed = discord.Embed(
                title=error.args[0]["title"],
                description=error.args[0]["description"],
                color=BOT_COLOR,
            )
            embed.set_footer(
                text=datetime.datetime.now(datetime.timezone.utc).strftime(
                    "%Y-%m-%d %H:%M:%S"
                )
                + " UTC"
            )
            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif (
            isinstance(error, app_commands.CheckFailure)
            and interaction.command.name in music_commands
        ):
            embed = discord.Embed(
                title="Player Creation Error",
                description="An error occured when creating a player. Please try again.",
                color=BOT_COLOR,
            )
            embed.set_footer(
                text=datetime.datetime.now(datetime.timezone.utc).strftime(
                    "%Y-%m-%d %H:%M:%S"
                )
                + " UTC"
            )
            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif isinstance(error, LoadError):
            embed = discord.Embed(
                title="Nothing Found",
                description="Spotify does not allow direct play, meaning songs have to be found on a supported provider. In this case, the song couldn't be found. Please try again with a different song, or try searching for just the name and artist manually rather than sending a link.",
                color=BOT_COLOR,
            )
            embed.set_footer(
                text=datetime.datetime.now(datetime.timezone.utc).strftime(
                    "%Y-%m-%d %H:%M:%S"
                )
                + " UTC"
            )
            await interaction.response.send_message(embed=embed, ephemeral=True)

        else:
            raise error

    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        return


async def setup(bot: commands.Bot):
    await bot.add_cog(slash_handlers(bot))