GoldyBot.utility.nextcordpy.help_command

  1import GoldyBot
  2import nextcord
  3from nextcord.ext import commands
  4
  5"""
  6
  7class goldy_help_command(commands.HelpCommand):
  8    def __init__(self):
  9        super().__init__()
 10
 11        self.config = GoldyBot.config.Config(GoldyBot.files.File(GoldyBot.paths.GOLDY_CONFIG_JSON))
 12
 13    class embed():
 14        @staticmethod
 15        async def create(help_type, description=None, thumbnail_url=None, page_num=None):
 16            if description == None:
 17                description = GoldyBot.utility.msgs.help.Embed.des
 18
 19            embed = nextcord.Embed(title=(GoldyBot.utility.msgs.help.Embed.title).format(help_type), description=description, color=settings.WHITE)
 20            if not thumbnail_url == None:
 21                embed.set_thumbnail(url=thumbnail_url)
 22            if not page_num == None:
 23                embed.set_footer(text=GoldyBot.utility.msgs.footer.type_2 + f" [PAGE {page_num}]")
 24            return embed
 25
 26        class page():
 27            @staticmethod
 28            async def change(msg_obj, page, help_embed_dict): #Method that changes the help command page.
 29                await msg_obj.edit(embed=help_embed_dict[page]["embed_obj"])
 30                pass
 31
 32    async def send_bot_help(self, mapping):
 33        bot_icon = GoldyBot.cache.main_cache_dict["client"].user.display_avatar.url
 34        BUTTONS = ["◀️", "▶️"]
 35        help_embed_dict = {}
 36        extenstion_count = 0
 37        page_count = 1
 38        embed = await goldy_help_command.embed.create("Overview", thumbnail_url=bot_icon, page_num=page_count)
 39        help_embed_dict[page_count] = {}
 40        help_embed_dict[page_count]["embed_obj"] = embed
 41        value = False
 42
 43        for cog in mapping:
 44            if not cog == None:
 45                if extenstion_count >= self.config.read("max_extenstions")*page_count:
 46                    page_count += 1
 47                    new_embed = await goldy_help_command.embed.create("Overview", thumbnail_url=bot_icon, page_num=page_count)
 48
 49                    help_embed_dict[page_count] = {}
 50                    help_embed_dict[page_count]["embed_obj"] = new_embed
 51
 52                help_embed_dict[page_count]["embed_context"] = ""
 53
 54                if servers.cogs.is_allowed(self.get_destination().guild.id, cog.cog_name):
 55                    if cog.cog_name == "core":
 56                        value = False
 57                    else:
 58                        value = True
 59
 60                    if value:
 61                        for command in mapping[cog]:
 62                            help_embed_dict[page_count]["embed_context"] += f"**•** ``!{command.name}``\n"
 63
 64                        if not help_embed_dict[page_count]["embed_context"] == "":
 65                            index = cog.help_command_index
 66                            #Add to embed.
 67                            if not index == None:
 68                                help_embed_dict[page_count]["embed_obj"].insert_field_at(index=index, name=f"__{(cog.qualified_name)}__", value=str(help_embed_dict[page_count]["embed_context"]))
 69                            else:
 70                                help_embed_dict[page_count]["embed_obj"].add_field(name=f"__{(cog.qualified_name)}__", value=str(help_embed_dict[page_count]["embed_context"]))
 71
 72                            extenstion_count += 1
 73        
 74        message = await self.get_destination().send(embed=help_embed_dict[1]["embed_obj"])
 75        
 76        #Add reactions.
 77        for button in BUTTONS:
 78            await message.add_reaction(button)
 79
 80        #Wait for user inputs.
 81        current_page = 1
 82        while True:
 83            try:
 84                reaction, user = await goldy_cache.client.wait_for('reaction_add', timeout=720)
 85                if not user == goldy_cache.client.user: #Ignores reactions from itself.
 86                    if reaction.message == message:
 87                        if reaction.emoji == BUTTONS[0]: #Backwards button.
 88                            if not current_page <= 1: #Stops page num from going under 1.
 89                                current_page -= 1
 90                                await goldy_help_command.embed.page.change(message, page=current_page, help_embed_dict=help_embed_dict)
 91                                await message.remove_reaction(reaction.emoji, user)
 92                            else:
 93                                await message.remove_reaction(reaction.emoji, user)
 94
 95                        if reaction.emoji == BUTTONS[1]: #Forwards button.
 96                            if not current_page >= page_count:
 97                                current_page += 1 #Stops page num from going over max amount.
 98                                await goldy_help_command.embed.page.change(message, page=current_page, help_embed_dict=help_embed_dict)
 99                                await message.remove_reaction(reaction.emoji, user)
100                            else:
101                                await message.remove_reaction(reaction.emoji, user)
102
103            except asyncio.TimeoutError:
104                #Clear reactions first.
105                for button in BUTTONS:
106                    await message.clear_reaction(button)
107
108                break #Stop loop
109
110
111    async def send_cog_help(self, cog):
112        embed = await goldy_help_command.embed.create(cog.qualified_name, 
113        description="*Here's all the commands this Extenstion/Cog offers.*", 
114        thumbnail_url=msg.images.other.url_1)
115        
116        if not cog == None:
117            if servers.cogs.is_allowed(self.get_destination().guild.id, cog.cog_name):
118                embed_context = ""
119                for command in cog.get_commands():
120                    embed_context += f"**•** ``!{command.name}`` **- {command.description}** \n"
121
122                #Cog/Extenstion Github Repo Link
123                try:
124                    if not cog.github_repo == None:
125                        github = f"**[[GitHub]({cog.github_repo})]**"
126                        embed_context += f"\n {github}"
127                except AttributeError:
128                    pass
129
130                if not embed_context == "":
131                    embed.add_field(name=f"__{cog.qualified_name}'s Commands__", value=str(embed_context))
132                
133                #Cog/Extenstion Version
134                try:
135                    if not cog.version == None:
136                        embed.set_footer(text=f"❤️ Ext Version: V{cog.version}")
137                except AttributeError:
138                    pass
139
140                await self.get_destination().send(embed=embed)
141
142    async def send_command_help(self, command):
143        description = command.description
144        if description == "":
145            description = "None"
146
147        usage = command.usage
148        
149        command_context = f"\n
150        **❗┃ Name: **``!{command.name}``
151        **📋┃ Description: *{description}***
152        **🧡┃ Usage: ``{usage}``**
153
154        **💙 From: __{command.cog.qualified_name}__**
155        "
156        
157        if servers.cogs.is_allowed(self.get_destination().guild.id, command.cog.cog_name):
158            embed = await goldy_help_command.embed.create(f"__!{command.name}__", 
159            description=command_context)
160
161            await self.get_destination().send(embed=embed)
162
163    async def send_error_message(self, error):
164        '''
165        #Try and correct known command errors.
166        import re
167        command = re.search('"(.*)"', error).group(1)
168        await self.send_command_help(await self.get_command_signature(command[1:len(command)]))
169        '''
170        await self.get_destination().send(GoldyBot.utility.msgs.help.command_not_found)
171
172"""