GoldyBot.internal_modules.v4.admin

  1import os
  2from typing import Dict, List
  3
  4import nextcord
  5import GoldyBot
  6import pprint
  7
  8from GoldyBot.errors import ModuleFailedToLoad, ModuleNotFound
  9from GoldyBot.utility.commands import send
 10
 11def get_modules_dict_list():
 12    """This is used in the unload and reload commands."""
 13    dict_list = {}
 14
 15    for module in os.listdir(GoldyBot.paths.INTERNAL_COGS_V4):
 16        if not module in ["__init__.py", "__pycache__"]:
 17            dict_list[f"{module}"] = module
 18
 19    for module in os.listdir(GoldyBot.paths.MODULES):
 20        if not module in ["__pycache__"]:
 21            dict_list[f"{module}"] = module
 22
 23    return dict_list
 24
 25class Admin(GoldyBot.Extension):
 26    """Admin extension."""
 27    def __init__(self, package_module=None):
 28        super().__init__(self, package_module_name=package_module)
 29
 30    def loader(self):
 31        
 32        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True)
 33        async def cache(self:Admin, ctx):
 34            cache_embed = GoldyBot.utility.goldy.embed.Embed(title=self.msg.cache.Embed.title)
 35            cache_embed.color = GoldyBot.utility.goldy.colours.BLUE
 36
 37            cache_string_formatted = pprint.pformat(GoldyBot.cache.main_cache_dict, indent=2)
 38
 39            if len(cache_string_formatted) >= 5000:
 40                cache_embed.description = "Oh oh, the cache is way too long to display in an embed right now. I'll print it in the console instead."
 41                await send(ctx, embed=cache_embed)
 42
 43                GoldyBot.logging.log("info", cache_string_formatted)
 44            else:
 45                cache_embed.description = f"```{cache_string_formatted}```"
 46                await send(ctx, embed=cache_embed)
 47
 48        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
 49            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
 50        })
 51        async def reload(self:Admin, ctx, module_name:str):
 52            embed = GoldyBot.utility.goldy.embed.Embed()
 53            embed.color = GoldyBot.utility.goldy.colours.PURPLE
 54
 55            module_name = module_name.lower()
 56            if module_name.endswith(".py"):
 57                module_name = module_name[:-3]
 58
 59            if module_name == "admin":
 60                embed.title = self.msg.reload.AdminCanNotBeReloadedEmbed.title
 61                embed.description = self.msg.reload.AdminCanNotBeReloadedEmbed.des.format(module_name)
 62                embed.color = self.msg.reload.AdminCanNotBeReloadedEmbed.colour
 63                await send(ctx, embed=embed)
 64                return False
 65
 66            try:
 67                module = GoldyBot.modules.Module(module_file_name = module_name)
 68            except ModuleNotFound:
 69                try:
 70                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
 71                except ModuleNotFound:
 72                    embed.title = self.msg.reload.ModuleNotFoundEmbed.title
 73                    embed.description = self.msg.reload.ModuleNotFoundEmbed.des.format(module_name)
 74                    embed.color = self.msg.reload.ModuleNotFoundEmbed.colour
 75                    await send(ctx, embed=embed)
 76                    return False
 77            
 78            try:
 79                registered_commands = module.reload()
 80
 81                """
 82                for command in reloaded_commands:
 83                    for guild_id in command.guilds_allowed_in:
 84                        payload = command.command.get_payload(guild_id)
 85                        await GoldyBot.cache.client().sync_application_commands([payload], guild_id=guild_id)
 86                """
 87
 88                temp:dict = {}
 89                for guild_id in GoldyBot.utility.guilds.get_guild_ids():
 90                    temp[guild_id] = []
 91
 92                for cmd in registered_commands:
 93                    for guild_id in cmd.guilds_allowed_in:
 94                        temp[guild_id].append(cmd.command)
 95
 96                def test(): GoldyBot.async_loop.run_until_complete(GoldyBot.cache.client().sync_all_application_commands(temp))
 97
 98                GoldyBot.async_loop.call_later(3.0, test)
 99            except ModuleFailedToLoad:
100                embed.title = self.msg.reload.FailedToLoadEmbed.title
101                embed.description = self.msg.reload.FailedToLoadEmbed.des.format(module.name)
102                embed.color = self.msg.reload.FailedToLoadEmbed.colour
103                await send(ctx, embed=embed)
104                return False
105
106            embed.title = self.msg.reload.ReloadedEmbed.title
107            embed.description = self.msg.reload.ReloadedEmbed.des.format(module.name)
108            embed.color = self.msg.reload.ReloadedEmbed.colour
109            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
110            await send(ctx, embed=embed)
111            return True
112
113
114
115
116        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
117            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
118        })
119        async def unload(self:Admin, ctx, module_name:str):
120            command_msg = self.msg.unload
121            embed = GoldyBot.utility.goldy.embed.Embed()
122            embed.color = GoldyBot.utility.goldy.colours.GREY
123            module_name = module_name.lower()
124
125            if module_name.endswith(".py"):
126                module_name = module_name[:-3]
127
128            if module_name == "admin":
129                embed.title = command_msg.AdminCanNotBeUnloadedEmbed.title
130                embed.description = command_msg.AdminCanNotBeUnloadedEmbed.des.format(module_name)
131                embed.color = command_msg.AdminCanNotBeUnloadedEmbed.colour
132                await send(embed=embed)
133                return False
134
135            try:
136                module = GoldyBot.modules.Module(module_file_name = module_name)
137            except ModuleNotFound:
138                try:
139                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
140                except ModuleNotFound:
141                    embed.title = command_msg.ModuleNotFoundEmbed.title
142                    embed.description = command_msg.ModuleNotFoundEmbed.des.format(module_name)
143                    embed.color = command_msg.ModuleNotFoundEmbed.colour
144                    await send(embed=embed)
145                    return False
146            
147            module.unload()
148
149            embed.title = command_msg.UnloadedEmbed.title
150            embed.description = command_msg.UnloadedEmbed.des.format(module.name)
151            embed.color = command_msg.UnloadedEmbed.colour
152            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
153            await send(embed=embed)
154            return True
155
156def load():
157    Admin(__name__)
def get_modules_dict_list():
12def get_modules_dict_list():
13    """This is used in the unload and reload commands."""
14    dict_list = {}
15
16    for module in os.listdir(GoldyBot.paths.INTERNAL_COGS_V4):
17        if not module in ["__init__.py", "__pycache__"]:
18            dict_list[f"{module}"] = module
19
20    for module in os.listdir(GoldyBot.paths.MODULES):
21        if not module in ["__pycache__"]:
22            dict_list[f"{module}"] = module
23
24    return dict_list

This is used in the unload and reload commands.

class Admin(GoldyBot.ext.extensions.Extension):
 26class Admin(GoldyBot.Extension):
 27    """Admin extension."""
 28    def __init__(self, package_module=None):
 29        super().__init__(self, package_module_name=package_module)
 30
 31    def loader(self):
 32        
 33        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True)
 34        async def cache(self:Admin, ctx):
 35            cache_embed = GoldyBot.utility.goldy.embed.Embed(title=self.msg.cache.Embed.title)
 36            cache_embed.color = GoldyBot.utility.goldy.colours.BLUE
 37
 38            cache_string_formatted = pprint.pformat(GoldyBot.cache.main_cache_dict, indent=2)
 39
 40            if len(cache_string_formatted) >= 5000:
 41                cache_embed.description = "Oh oh, the cache is way too long to display in an embed right now. I'll print it in the console instead."
 42                await send(ctx, embed=cache_embed)
 43
 44                GoldyBot.logging.log("info", cache_string_formatted)
 45            else:
 46                cache_embed.description = f"```{cache_string_formatted}```"
 47                await send(ctx, embed=cache_embed)
 48
 49        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
 50            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
 51        })
 52        async def reload(self:Admin, ctx, module_name:str):
 53            embed = GoldyBot.utility.goldy.embed.Embed()
 54            embed.color = GoldyBot.utility.goldy.colours.PURPLE
 55
 56            module_name = module_name.lower()
 57            if module_name.endswith(".py"):
 58                module_name = module_name[:-3]
 59
 60            if module_name == "admin":
 61                embed.title = self.msg.reload.AdminCanNotBeReloadedEmbed.title
 62                embed.description = self.msg.reload.AdminCanNotBeReloadedEmbed.des.format(module_name)
 63                embed.color = self.msg.reload.AdminCanNotBeReloadedEmbed.colour
 64                await send(ctx, embed=embed)
 65                return False
 66
 67            try:
 68                module = GoldyBot.modules.Module(module_file_name = module_name)
 69            except ModuleNotFound:
 70                try:
 71                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
 72                except ModuleNotFound:
 73                    embed.title = self.msg.reload.ModuleNotFoundEmbed.title
 74                    embed.description = self.msg.reload.ModuleNotFoundEmbed.des.format(module_name)
 75                    embed.color = self.msg.reload.ModuleNotFoundEmbed.colour
 76                    await send(ctx, embed=embed)
 77                    return False
 78            
 79            try:
 80                registered_commands = module.reload()
 81
 82                """
 83                for command in reloaded_commands:
 84                    for guild_id in command.guilds_allowed_in:
 85                        payload = command.command.get_payload(guild_id)
 86                        await GoldyBot.cache.client().sync_application_commands([payload], guild_id=guild_id)
 87                """
 88
 89                temp:dict = {}
 90                for guild_id in GoldyBot.utility.guilds.get_guild_ids():
 91                    temp[guild_id] = []
 92
 93                for cmd in registered_commands:
 94                    for guild_id in cmd.guilds_allowed_in:
 95                        temp[guild_id].append(cmd.command)
 96
 97                def test(): GoldyBot.async_loop.run_until_complete(GoldyBot.cache.client().sync_all_application_commands(temp))
 98
 99                GoldyBot.async_loop.call_later(3.0, test)
100            except ModuleFailedToLoad:
101                embed.title = self.msg.reload.FailedToLoadEmbed.title
102                embed.description = self.msg.reload.FailedToLoadEmbed.des.format(module.name)
103                embed.color = self.msg.reload.FailedToLoadEmbed.colour
104                await send(ctx, embed=embed)
105                return False
106
107            embed.title = self.msg.reload.ReloadedEmbed.title
108            embed.description = self.msg.reload.ReloadedEmbed.des.format(module.name)
109            embed.color = self.msg.reload.ReloadedEmbed.colour
110            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
111            await send(ctx, embed=embed)
112            return True
113
114
115
116
117        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
118            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
119        })
120        async def unload(self:Admin, ctx, module_name:str):
121            command_msg = self.msg.unload
122            embed = GoldyBot.utility.goldy.embed.Embed()
123            embed.color = GoldyBot.utility.goldy.colours.GREY
124            module_name = module_name.lower()
125
126            if module_name.endswith(".py"):
127                module_name = module_name[:-3]
128
129            if module_name == "admin":
130                embed.title = command_msg.AdminCanNotBeUnloadedEmbed.title
131                embed.description = command_msg.AdminCanNotBeUnloadedEmbed.des.format(module_name)
132                embed.color = command_msg.AdminCanNotBeUnloadedEmbed.colour
133                await send(embed=embed)
134                return False
135
136            try:
137                module = GoldyBot.modules.Module(module_file_name = module_name)
138            except ModuleNotFound:
139                try:
140                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
141                except ModuleNotFound:
142                    embed.title = command_msg.ModuleNotFoundEmbed.title
143                    embed.description = command_msg.ModuleNotFoundEmbed.des.format(module_name)
144                    embed.color = command_msg.ModuleNotFoundEmbed.colour
145                    await send(embed=embed)
146                    return False
147            
148            module.unload()
149
150            embed.title = command_msg.UnloadedEmbed.title
151            embed.description = command_msg.UnloadedEmbed.des.format(module.name)
152            embed.color = command_msg.UnloadedEmbed.colour
153            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
154            await send(embed=embed)
155            return True

Admin extension.

Admin(package_module=None)
28    def __init__(self, package_module=None):
29        super().__init__(self, package_module_name=package_module)

Tells Goldy Bot to Load this class as an extension.

def loader(self):
 31    def loader(self):
 32        
 33        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True)
 34        async def cache(self:Admin, ctx):
 35            cache_embed = GoldyBot.utility.goldy.embed.Embed(title=self.msg.cache.Embed.title)
 36            cache_embed.color = GoldyBot.utility.goldy.colours.BLUE
 37
 38            cache_string_formatted = pprint.pformat(GoldyBot.cache.main_cache_dict, indent=2)
 39
 40            if len(cache_string_formatted) >= 5000:
 41                cache_embed.description = "Oh oh, the cache is way too long to display in an embed right now. I'll print it in the console instead."
 42                await send(ctx, embed=cache_embed)
 43
 44                GoldyBot.logging.log("info", cache_string_formatted)
 45            else:
 46                cache_embed.description = f"```{cache_string_formatted}```"
 47                await send(ctx, embed=cache_embed)
 48
 49        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
 50            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
 51        })
 52        async def reload(self:Admin, ctx, module_name:str):
 53            embed = GoldyBot.utility.goldy.embed.Embed()
 54            embed.color = GoldyBot.utility.goldy.colours.PURPLE
 55
 56            module_name = module_name.lower()
 57            if module_name.endswith(".py"):
 58                module_name = module_name[:-3]
 59
 60            if module_name == "admin":
 61                embed.title = self.msg.reload.AdminCanNotBeReloadedEmbed.title
 62                embed.description = self.msg.reload.AdminCanNotBeReloadedEmbed.des.format(module_name)
 63                embed.color = self.msg.reload.AdminCanNotBeReloadedEmbed.colour
 64                await send(ctx, embed=embed)
 65                return False
 66
 67            try:
 68                module = GoldyBot.modules.Module(module_file_name = module_name)
 69            except ModuleNotFound:
 70                try:
 71                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
 72                except ModuleNotFound:
 73                    embed.title = self.msg.reload.ModuleNotFoundEmbed.title
 74                    embed.description = self.msg.reload.ModuleNotFoundEmbed.des.format(module_name)
 75                    embed.color = self.msg.reload.ModuleNotFoundEmbed.colour
 76                    await send(ctx, embed=embed)
 77                    return False
 78            
 79            try:
 80                registered_commands = module.reload()
 81
 82                """
 83                for command in reloaded_commands:
 84                    for guild_id in command.guilds_allowed_in:
 85                        payload = command.command.get_payload(guild_id)
 86                        await GoldyBot.cache.client().sync_application_commands([payload], guild_id=guild_id)
 87                """
 88
 89                temp:dict = {}
 90                for guild_id in GoldyBot.utility.guilds.get_guild_ids():
 91                    temp[guild_id] = []
 92
 93                for cmd in registered_commands:
 94                    for guild_id in cmd.guilds_allowed_in:
 95                        temp[guild_id].append(cmd.command)
 96
 97                def test(): GoldyBot.async_loop.run_until_complete(GoldyBot.cache.client().sync_all_application_commands(temp))
 98
 99                GoldyBot.async_loop.call_later(3.0, test)
100            except ModuleFailedToLoad:
101                embed.title = self.msg.reload.FailedToLoadEmbed.title
102                embed.description = self.msg.reload.FailedToLoadEmbed.des.format(module.name)
103                embed.color = self.msg.reload.FailedToLoadEmbed.colour
104                await send(ctx, embed=embed)
105                return False
106
107            embed.title = self.msg.reload.ReloadedEmbed.title
108            embed.description = self.msg.reload.ReloadedEmbed.des.format(module.name)
109            embed.color = self.msg.reload.ReloadedEmbed.colour
110            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
111            await send(ctx, embed=embed)
112            return True
113
114
115
116
117        @GoldyBot.command(required_roles=["bot_dev", "bot_admin"], hidden=True, slash_options={
118            "module_name" : nextcord.SlashOption(choices=get_modules_dict_list())
119        })
120        async def unload(self:Admin, ctx, module_name:str):
121            command_msg = self.msg.unload
122            embed = GoldyBot.utility.goldy.embed.Embed()
123            embed.color = GoldyBot.utility.goldy.colours.GREY
124            module_name = module_name.lower()
125
126            if module_name.endswith(".py"):
127                module_name = module_name[:-3]
128
129            if module_name == "admin":
130                embed.title = command_msg.AdminCanNotBeUnloadedEmbed.title
131                embed.description = command_msg.AdminCanNotBeUnloadedEmbed.des.format(module_name)
132                embed.color = command_msg.AdminCanNotBeUnloadedEmbed.colour
133                await send(embed=embed)
134                return False
135
136            try:
137                module = GoldyBot.modules.Module(module_file_name = module_name)
138            except ModuleNotFound:
139                try:
140                    module = GoldyBot.modules.Module(module_file_name = module_name + ".py")
141                except ModuleNotFound:
142                    embed.title = command_msg.ModuleNotFoundEmbed.title
143                    embed.description = command_msg.ModuleNotFoundEmbed.des.format(module_name)
144                    embed.color = command_msg.ModuleNotFoundEmbed.colour
145                    await send(embed=embed)
146                    return False
147            
148            module.unload()
149
150            embed.title = command_msg.UnloadedEmbed.title
151            embed.description = command_msg.UnloadedEmbed.des.format(module.name)
152            embed.color = command_msg.UnloadedEmbed.colour
153            embed.set_thumbnail(url=self.msg.reload.ReloadedEmbed.thumbnail)
154            await send(embed=embed)
155            return True

The extension's command loader. This is what Goldy Bot uses to load your commands in an extension.

def load():
157def load():
158    Admin(__name__)