GoldyBot.utility.views.dropdown

 1import GoldyBot
 2from typing import Callable, List
 3
 4MODULE_NAME = "DROPDOWN"
 5
 6async def dropdown(ctx:GoldyBot.InteractionToCtx, options:List[GoldyBot.nextcord.SelectOption], min_max_value:tuple=(1, 1) , placeholder_msg="🎯 Pick an Option", warning=True, warning_msg="🛑 Are you sure!", callback:Callable=None):
 7    """
 8    Goldy Bot's dropdown view. (Only the command author can interact with this!)
 9    """
10
11    class Dropdown(GoldyBot.nextcord.ui.Select):
12        def __init__(self, author:GoldyBot.Member, options:List[GoldyBot.nextcord.SelectOption], placeholder_msg:str, warning:bool, warning_msg:str, callback:Callable):
13            self.author = author
14            self.warning = warning
15            self.warning_msg = warning_msg
16            self.callback_ = callback
17
18            self.options_values = {}
19
20            # Embeds
21            self.your_not_author_embed = GoldyBot.Embed(
22                title="❌ Author Only!",
23                description="***❌{}, this view can only be interacted by the command author.***",
24                colour=GoldyBot.Colours.RED
25            )
26
27            super().__init__(
28                placeholder=placeholder_msg,
29                min_values=min_max_value[0],
30                max_values=min_max_value[1],
31                options=options,
32            )
33
34        async def callback(self, interaction: GoldyBot.nextcord.Interaction):
35            if self.author.member.id == interaction.user.id: # Is this the author.
36                view = await GoldyBot.utility.views.confirm.yes_or_no(interaction)
37                
38                if self.warning:
39                    message = await interaction.send(self.warning_msg, view=view)
40                    await view.wait()
41                    await message.delete()
42                else:
43                    view.value = True
44
45                if view.value == True:
46                    if not self.callback_ == None:
47                        await self.callback_(self.values) # Execute callback
48
49                        await interaction.delete_original_message()
50
51                        GoldyBot.logging.log(f"[{MODULE_NAME}] Dropdown view for '{self.author.name}' executed it's function '{self.callback_.__name__}'!")
52
53            else:
54                # View not for you message.
55                embed_thumbnail = GoldyBot.File(GoldyBot.paths.ASSETS + "/angry_mirai_kuriyama.gif")
56
57                your_not_author_embed = self.your_not_author_embed.copy()
58                your_not_author_embed.description = self.your_not_author_embed.description.format(interaction.user.mention)
59                your_not_author_embed.set_thumbnail("attachment://image.gif")
60
61                message = await interaction.send(embed=your_not_author_embed, file=GoldyBot.nextcord.File(embed_thumbnail.file_path, filename="image.gif"))
62                await message.delete(delay=6)
63
64                #raise GoldyBot.errors.YourNotAuthor(f"[{MODULE_NAME}] '{interaction.user.name}' tried interacting with a command author only view.")
65
66    class DropdownView(GoldyBot.nextcord.ui.View):
67        def __init__(self, author:GoldyBot.Member, options:List[GoldyBot.nextcord.SelectOption], placeholder_msg:str, warning:bool, warning_msg:str, callback:Callable):
68            super().__init__()
69
70            self.add_item(Dropdown(author, options, placeholder_msg, warning, warning_msg, callback))
71
72    return DropdownView(GoldyBot.Member(ctx), options, placeholder_msg, warning, warning_msg, callback)