Discord Class

when ran, the reload_all_servers command will remove unused cogs (But it doesnt)
mail@pastecode.io avatar
unknown
python
5 months ago
5.0 kB
5
Indexable
import discord
from discord.ext import commands
import json
import os

#ALL COGS FOR THE BOT
from commands.application import application 
from commands.clockin import clockin
from commands.feedback import feedback
from commands.enabler import enabler
from commands.reports import report
from commands.rolesets import rolesets
from commands.sessions import session

def get_server_data(guild_id): 
    with open(os.getcwd() + f'/data/server_data/{str(guild_id)}.json') as json_file:
        return json.load(json_file)

def save_server_data(guild_id, data):
    with open(os.getcwd() + f'/data/server_data/{str(guild_id)}.json', 'w') as json_file:
        json.dump(data, json_file, sort_keys=True, indent=4)


class reloader():


    async def reload_all_servers(bot: commands.Bot, serverIdList = list[str]):
        for i in serverIdList: #Only files is ['1184599191390011483', '1268285151872422022']
            data = get_server_data(i) #A dict of data
            for v in data: #For every command data section
                if v == "Error Logs":
                    continue
                if data[v]['Loaded'] == True: #If the command is loaded:
                    if v == "Application Data":
                        try:
                            await bot.add_cog(application.appCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Clock Data":
                        try:
                            await bot.add_cog(clockin.clockCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Feedback Data":
                        try:
                            await bot.add_cog(feedback.feedbackCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Report Data":
                        try:
                            await bot.add_cog(report.reportCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Session Data":
                        try:
                            await bot.add_cog(session.sessionCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Roleset Data":
                        try:
                            await bot.add_cog(rolesets.rolesetCog(bot), guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    #print("Added", v, "to", bot.get_guild(int(i)).name) #Never prints (EVER)
                elif data[v]['Loaded'] == False:
                    if v == "Application Data":
                        try:
                            await bot.remove_cog('appCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Clock Data":
                        try:
                            await bot.remove_cog('clockCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Feedback Data":
                        try:
                            await bot.remove_cog('feedbackCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Report Data":
                        try:
                            await bot.remove_cog('reportCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Session Data":
                        try:
                            await bot.remove_cog('sessionCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    elif v == "Roleset Data":
                        try:
                            await bot.remove_cog('rolesetCog', guild = bot.get_guild(int(i)))
                        except discord.errors.ClientException:
                            pass
                    #print("Removed", v, "from", bot.get_guild(int(i)).name) #Prints many times
            await bot.tree.sync(guild=bot.get_guild(int(i)))
            print("Done with server") #Prints twice
        await bot.add_cog(enabler.enableCog(bot))
        await bot.tree.sync() #just to make sure, plus to add the enabler cog
        print("Done") #Does print
Leave a Comment