GoldyBot.ext.commands

  1from __future__ import annotations
  2import asyncio
  3from typing import Dict
  4import nextcord
  5from nextcord.ext import commands
  6
  7import GoldyBot
  8from GoldyBot.errors import GuildNotRegistered, MemberHasNoPermsForCommand
  9
 10MODULE_NAME = "COMMANDS"
 11
 12def command(command_name:str=None, required_roles:list=[], help_des:str=None, hidden=False, slash_cmd_only=False, normal_cmd_only=False, slash_options:Dict[str, nextcord.SlashOption]={}):
 13    """
 14    Add a command to Goldy Bot with this decorator.
 15    
 16    ---------------
 17    ### ***``Example:``***
 18
 19    This is how you create a command in GoldyBot. 😀
 20
 21    ```python
 22    @GoldyBot.command()
 23    async def uwu(ctx):
 24        await ctx.send(f'Hi, {ctx.author.mention}! UwU!')
 25    ```
 26    """
 27    def decorate(func):
 28        def inner(func, command_name, required_roles, help_des, hidden, slash_cmd_only, normal_cmd_only, slash_options) -> GoldyBot.objects.command.Command:
 29            func:function = func
 30            
 31            goldy_command = GoldyBot.objects.command.Command(func, command_name=command_name, required_roles=required_roles, slash_options=slash_options, help_des=help_des, hidden=hidden)
 32            print(goldy_command.params)
 33
 34            if command_name == None: command_name = goldy_command.code_name
 35
 36            toggle_slash_cmd = True
 37            toggle_normal_cmd = True
 38
 39            if slash_cmd_only == True: toggle_normal_cmd = False
 40            if normal_cmd_only == True: toggle_slash_cmd = False
 41
 42            #  Add command to nextcord.
 43            #============================
 44            # Grab client object from cache.
 45            client:commands.Bot = GoldyBot.cache.main_cache_dict["client"]
 46            
 47            # Get command's arguments.
 48            params = goldy_command.params
 49
 50            # Create command usage embed.
 51            command_usage_args = f"!{command_name}"
 52            for param in params[1:]:
 53                command_usage_args += (" {" + param + "}")
 54
 55            command_usage_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandUsage.Embed.title)
 56            command_usage_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandUsage.Embed.thumbnail)
 57
 58            no_perms_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.title)
 59            no_perms_embed.color = GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.colour
 60            no_perms_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.thumbnail)
 61
 62            guild_not_registered_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.title)
 63            guild_not_registered_embed.color = GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.colour
 64            guild_not_registered_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.thumbnail)
 65
 66            # Preparing to add command.
 67            # Checking if this command is in an extension.
 68            is_in_extension = goldy_command.in_extension
 69            class_ = goldy_command.extension
 70
 71            if not params[0] == "ctx": # Check if command has 'ctx' as first argument.
 72                GoldyBot.logging.log("warn", f"Failed to load the command '{command_name}', it must contain 'ctx' as it's first argument!")
 73                return None
 74
 75            # Add command.
 76            #----------------
 77            if toggle_normal_cmd:
 78                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Creating normal command...")
 79                if is_in_extension == True: # Run in EXTENSION!
 80                    @client.command(name=command_name, help_message=help_des)
 81                    async def command_(ctx=params[0], *params):
 82                        command_usage_embed.description = GoldyBot.utility.msgs.bot.CommandUsage.Embed.des.format(ctx.author.mention, command_usage_args)
 83
 84                        #Run command.
 85                        try:
 86                            if goldy_command.allowed_to_run(ctx):
 87                                await func(class_, ctx, *params)
 88                                GoldyBot.logging.log(f"[{MODULE_NAME}] The command '{goldy_command.code_name}' was executed.")
 89                        
 90                        except TypeError as e: 
 91                            if not goldy_command.any_args_missing(params): # Command Arguments are missing.
 92                                message = await ctx.send(embed=command_usage_embed)
 93                                await asyncio.sleep(15)
 94                                await message.delete()                    
 95
 96                            else: # Then it's an actual error.
 97                                GoldyBot.logging.log("error", e)
 98
 99                        except MemberHasNoPermsForCommand:
100                            if hidden == False:
101                                no_perms_embed.description = GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.des.format(ctx.author.mention)
102                                message = await ctx.send(embed=no_perms_embed)
103                                await asyncio.sleep(15)
104                                await message.delete()
105
106                        except GuildNotRegistered:
107                            if hidden == False:
108                                guild_not_registered_embed.description = GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.des.format(ctx.author.mention)
109                                message = await ctx.send(embed=guild_not_registered_embed)
110                                await asyncio.sleep(15)
111                                await message.delete()
112                                
113                        except Exception as e:
114                            GoldyBot.logging.log("error", e)
115
116                else: # Run as NORMAL command!
117                    @client.command(name=command_name, help_message=help_des)
118                    async def command_(ctx=params[0], *params):
119                        command_usage_embed.description = GoldyBot.utility.msgs.bot.CommandUsage.Embed.des.format(ctx.author.mention, command_usage_args)
120
121                        # Run command.
122                        try: 
123                            if goldy_command.allowed_to_run(ctx):
124                                await func(ctx, *params)
125                                GoldyBot.logging.log(f"[{MODULE_NAME}] The command '{goldy_command.code_name}' was executed.")
126                        
127                        except TypeError as e:
128                            if not goldy_command.any_args_missing(params): # Command Arguments are missing.
129                                message = await ctx.send(embed=command_usage_embed)
130                                await asyncio.sleep(15)
131                                await message.delete()
132
133                            else: # Then it's an actual error.
134                                GoldyBot.logging.log("error", e)
135
136                        except MemberHasNoPermsForCommand:
137                            if hidden == False:
138                                message = await ctx.send(embed=no_perms_embed)
139                                await asyncio.sleep(6)
140                                await message.delete()
141
142                        except GuildNotRegistered:
143                            if hidden == False:
144                                message = await ctx.send(embed=guild_not_registered_embed)
145                                await asyncio.sleep(15)
146                                await message.delete()
147
148                        except Exception as e:
149                            GoldyBot.logging.log("error", e)
150                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Normal command created!")
151
152            # Add slash command
153            #--------------------
154            if toggle_slash_cmd:
155                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Creating slash command...")
156                goldy_command.create_slash()
157            GoldyBot.logging.log("info_3", f"[{MODULE_NAME}] Command '{command_name}' has been loaded.")
158
159            return goldy_command
160
161        return inner(func, command_name, required_roles, help_des, hidden, slash_cmd_only, normal_cmd_only, slash_options)
162
163    return decorate
def command( command_name: str = None, required_roles: list = [], help_des: str = None, hidden=False, slash_cmd_only=False, normal_cmd_only=False, slash_options: Dict[str, nextcord.application_command.SlashOption] = {}):
 13def command(command_name:str=None, required_roles:list=[], help_des:str=None, hidden=False, slash_cmd_only=False, normal_cmd_only=False, slash_options:Dict[str, nextcord.SlashOption]={}):
 14    """
 15    Add a command to Goldy Bot with this decorator.
 16    
 17    ---------------
 18    ### ***``Example:``***
 19
 20    This is how you create a command in GoldyBot. 😀
 21
 22    ```python
 23    @GoldyBot.command()
 24    async def uwu(ctx):
 25        await ctx.send(f'Hi, {ctx.author.mention}! UwU!')
 26    ```
 27    """
 28    def decorate(func):
 29        def inner(func, command_name, required_roles, help_des, hidden, slash_cmd_only, normal_cmd_only, slash_options) -> GoldyBot.objects.command.Command:
 30            func:function = func
 31            
 32            goldy_command = GoldyBot.objects.command.Command(func, command_name=command_name, required_roles=required_roles, slash_options=slash_options, help_des=help_des, hidden=hidden)
 33            print(goldy_command.params)
 34
 35            if command_name == None: command_name = goldy_command.code_name
 36
 37            toggle_slash_cmd = True
 38            toggle_normal_cmd = True
 39
 40            if slash_cmd_only == True: toggle_normal_cmd = False
 41            if normal_cmd_only == True: toggle_slash_cmd = False
 42
 43            #  Add command to nextcord.
 44            #============================
 45            # Grab client object from cache.
 46            client:commands.Bot = GoldyBot.cache.main_cache_dict["client"]
 47            
 48            # Get command's arguments.
 49            params = goldy_command.params
 50
 51            # Create command usage embed.
 52            command_usage_args = f"!{command_name}"
 53            for param in params[1:]:
 54                command_usage_args += (" {" + param + "}")
 55
 56            command_usage_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandUsage.Embed.title)
 57            command_usage_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandUsage.Embed.thumbnail)
 58
 59            no_perms_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.title)
 60            no_perms_embed.color = GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.colour
 61            no_perms_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.thumbnail)
 62
 63            guild_not_registered_embed = GoldyBot.utility.goldy.embed.Embed(title=GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.title)
 64            guild_not_registered_embed.color = GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.colour
 65            guild_not_registered_embed.set_thumbnail(url=GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.thumbnail)
 66
 67            # Preparing to add command.
 68            # Checking if this command is in an extension.
 69            is_in_extension = goldy_command.in_extension
 70            class_ = goldy_command.extension
 71
 72            if not params[0] == "ctx": # Check if command has 'ctx' as first argument.
 73                GoldyBot.logging.log("warn", f"Failed to load the command '{command_name}', it must contain 'ctx' as it's first argument!")
 74                return None
 75
 76            # Add command.
 77            #----------------
 78            if toggle_normal_cmd:
 79                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Creating normal command...")
 80                if is_in_extension == True: # Run in EXTENSION!
 81                    @client.command(name=command_name, help_message=help_des)
 82                    async def command_(ctx=params[0], *params):
 83                        command_usage_embed.description = GoldyBot.utility.msgs.bot.CommandUsage.Embed.des.format(ctx.author.mention, command_usage_args)
 84
 85                        #Run command.
 86                        try:
 87                            if goldy_command.allowed_to_run(ctx):
 88                                await func(class_, ctx, *params)
 89                                GoldyBot.logging.log(f"[{MODULE_NAME}] The command '{goldy_command.code_name}' was executed.")
 90                        
 91                        except TypeError as e: 
 92                            if not goldy_command.any_args_missing(params): # Command Arguments are missing.
 93                                message = await ctx.send(embed=command_usage_embed)
 94                                await asyncio.sleep(15)
 95                                await message.delete()                    
 96
 97                            else: # Then it's an actual error.
 98                                GoldyBot.logging.log("error", e)
 99
100                        except MemberHasNoPermsForCommand:
101                            if hidden == False:
102                                no_perms_embed.description = GoldyBot.utility.msgs.bot.CommandNoPerms.Embed.des.format(ctx.author.mention)
103                                message = await ctx.send(embed=no_perms_embed)
104                                await asyncio.sleep(15)
105                                await message.delete()
106
107                        except GuildNotRegistered:
108                            if hidden == False:
109                                guild_not_registered_embed.description = GoldyBot.utility.msgs.bot.CommandGuildNotRegistered.Embed.des.format(ctx.author.mention)
110                                message = await ctx.send(embed=guild_not_registered_embed)
111                                await asyncio.sleep(15)
112                                await message.delete()
113                                
114                        except Exception as e:
115                            GoldyBot.logging.log("error", e)
116
117                else: # Run as NORMAL command!
118                    @client.command(name=command_name, help_message=help_des)
119                    async def command_(ctx=params[0], *params):
120                        command_usage_embed.description = GoldyBot.utility.msgs.bot.CommandUsage.Embed.des.format(ctx.author.mention, command_usage_args)
121
122                        # Run command.
123                        try: 
124                            if goldy_command.allowed_to_run(ctx):
125                                await func(ctx, *params)
126                                GoldyBot.logging.log(f"[{MODULE_NAME}] The command '{goldy_command.code_name}' was executed.")
127                        
128                        except TypeError as e:
129                            if not goldy_command.any_args_missing(params): # Command Arguments are missing.
130                                message = await ctx.send(embed=command_usage_embed)
131                                await asyncio.sleep(15)
132                                await message.delete()
133
134                            else: # Then it's an actual error.
135                                GoldyBot.logging.log("error", e)
136
137                        except MemberHasNoPermsForCommand:
138                            if hidden == False:
139                                message = await ctx.send(embed=no_perms_embed)
140                                await asyncio.sleep(6)
141                                await message.delete()
142
143                        except GuildNotRegistered:
144                            if hidden == False:
145                                message = await ctx.send(embed=guild_not_registered_embed)
146                                await asyncio.sleep(15)
147                                await message.delete()
148
149                        except Exception as e:
150                            GoldyBot.logging.log("error", e)
151                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Normal command created!")
152
153            # Add slash command
154            #--------------------
155            if toggle_slash_cmd:
156                GoldyBot.logging.log(f"[{MODULE_NAME}] [{command_name.upper()}] Creating slash command...")
157                goldy_command.create_slash()
158            GoldyBot.logging.log("info_3", f"[{MODULE_NAME}] Command '{command_name}' has been loaded.")
159
160            return goldy_command
161
162        return inner(func, command_name, required_roles, help_des, hidden, slash_cmd_only, normal_cmd_only, slash_options)
163
164    return decorate

Add a command to Goldy Bot with this decorator.


Example:

This is how you create a command in GoldyBot. 😀

@GoldyBot.command()
async def uwu(ctx):
    await ctx.send(f'Hi, {ctx.author.mention}! UwU!')