aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/exts/cloudflare/zones.py
blob: aadeac9fb118c3cc5158aedaa7b5b4c1a4a9c8d5 (plain) (blame)
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
"""The zones cog helps with managing Cloudflare zones."""
import discord
from discord.ext import commands

from arthur.apis.cloudflare import zones
from arthur.bot import KingArthur
from arthur.utils import generate_error_message


class ZonesView(discord.ui.View):
    """This view allows users to select and purge the zones specified."""

    OPTIONS = [
        discord.SelectOption(label="pythondiscord.com", emoji="🌐"),
        discord.SelectOption(label="pythondiscord.org", emoji="🌐"),
        discord.SelectOption(label="pydis.com", emoji="🌐"),
        discord.SelectOption(label="pydis.org", emoji="🌐")
    ]

    def __init__(self) -> None:
        super().__init__()

    async def interaction_check(self, interaction: discord.Interaction) -> bool:
        """Ensure the user has the DevOps role."""
        return KingArthur._is_devops(interaction)

    @discord.ui.select(
        placeholder="The zone(s) which should be purged...",
        min_values=1,
        max_values=4,
        options=OPTIONS,
        # This is needed in order to identify the drop down later on
        # because discord.Item.type is NotImplemented
        custom_id="select"
    )
    async def select_zones(
        self, dropdown: discord.ui.Select,
        interaction: discord.Interaction
    ) -> None:
        """Drop down menu contains the list of zones."""
        pass

    @discord.ui.button(label="Purge zones!", style=discord.ButtonStyle.primary)
    async def purge_zones(
        self,
        button: discord.ui.Button,
        interaction: discord.Interaction
    ) -> None:
        """Button to purge the zones."""
        for zone_name in [child for child in self.children if child._provided_custom_id][0].values:
            pydis_zones = await zones.list_zones(zone_name)
            required_id = pydis_zones[zone_name]
            purge_attempt_response = await zones.purge_zone(required_id)
            if purge_attempt_response["success"]:
                message = ":white_check_mark:"
                message += " **Cache cleared!** "
                message += f"The Cloudflare cache for `{zone_name}` was cleared."
            else:
                description_content = f"The cache for `{zone_name}` couldn't be cleared.\n"
                if errors := purge_attempt_response["errors"]:
                    for error in errors:
                        description_content += f"`{error['code']}`: {error['message']}\n"
                message = generate_error_message(description=description_content, emote=":x:")
            await interaction.response.send(message)


class Zones(commands.Cog):
    """Commands for working with Cloudflare zones."""

    def __init__(self, bot: KingArthur) -> None:
        self.bot = bot

    @commands.group(name="zones", invoke_without_command=True)
    async def zones(self, ctx: commands.Context) -> None:
        """Commands for working with Cloudflare zones."""
        await ctx.send_help(ctx.command)

    @zones.command(name="purge")
    async def purge(self, ctx: commands.Context) -> None:
        """Command to clear the Cloudflare cache of the specified zone."""
        view = ZonesView()
        await ctx.send(
            "Pick which zone(s) that should have their cache purged :cloud_lightning:",
            view=view
        )


def setup(bot: KingArthur) -> None:
    """Add the extension to the bot."""
    bot.add_cog(Zones(bot))