diff --git a/.gitignore b/.gitignore index 9057862..2ac0209 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ gen /audio/*/ /test2.py /user.db -/prefix.json +/*.json diff --git a/test.py b/test.py index 52f9e21..aa331cd 100644 --- a/test.py +++ b/test.py @@ -83,25 +83,7 @@ if not path.isfile('prefix.json'): def determine_prefix(bot, msg): guild = msg.guild - base = DEFAULT_PREFIX - try: - with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON - jsonObject = json.load(fp) # Load the custom prefixes - - except: - jsonObject = {} - - if guild: # If the guild exists - try: - guild_conf = jsonObject[f"{guild.id}"] - try: - prefix = guild_conf["prefix"] # Get the prefix - base = prefix - except: - pass - except KeyError: - pass - + base = Commands._read_json(guild.id, 'prefix') or DEFAULT_PREFIX # Get bot role return base @@ -112,7 +94,7 @@ inter_client = InteractionClient(bot) class Commands: - async def setprefix(ctx, prefix: str): + async def set_prefix(ctx, prefix: str): with open('prefix.json', 'r', encoding='utf-8') as fp: try: jsonObject = json.load(fp) @@ -144,6 +126,45 @@ class Commands: return files + async def _read_json(guild, param): + parameter = None + try: + with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON + jsonObject = json.load(fp) # Load the custom prefixes + + except: + jsonObject = {} + + if guild: # If the guild exists + try: + guild_conf = jsonObject[f"{guild}"] + try: + parameter = guild_conf[f"{param}"] + except: + pass + except KeyError: + pass + + return parameter + + async def _write_json(guild, param_name, param): + with open('prefix.json', 'r', encoding='utf-8') as fp: + try: + jsonObject = json.load(fp) + except: + jsonObject = {} + try: + jsonObject[f"{guild}"][f"{param_name}"] = param + except KeyError: + json_param = jsonObject[f"{guild}"] = {f'{param_name}': ''} + new = {f"{param_name}": param} + json_param.update(new) + jsonObject[str(guild)] = json_param + + with open("prefix.json", "w") as fl: + json.dump(jsonObject, fl) + fl.close() + @bot.event async def on_ready(): @@ -165,23 +186,8 @@ async def on_guild_join(guild): async def on_member_join(member): DB._fill_bd(member.name, member.id, member.bot, member.nick, member.guild.id) - try: - with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON - jsonObject = json.load(fp) # Load the custom prefixes - - except: - jsonObject = {} - - if member.guild.id: # If the guild exists - try: - guild_conf = jsonObject[f"{member.guild.id}"] - try: - bot_role = guild_conf["bot_role"] # Get bot role - guest_role = guild_conf["guest_role"] # Get guest role - except: - pass - except KeyError: - pass + bot_role = Commands._read_json(member.guild.id, 'bot_role') # Get bot role + guest_role = Commands._read_json(member.guild.id, 'guest_role') # Get guest role if bot_role and guest_role: if member.bot == 0: @@ -247,7 +253,7 @@ async def upload_audio(ctx, user=None): @bot.command(name="set_prefix") @commands.has_permissions(administrator=True) async def command_setprefix(ctx, prefix: str): - await Commands.setprefix(ctx, prefix) + await Commands.set_prefix(ctx, prefix) @inter_client.slash_command( @@ -258,7 +264,7 @@ async def command_setprefix(ctx, prefix: str): ] ) async def slash_setprefix(ctx, prefix: str): - await Commands.setprefix(ctx, prefix) + await Commands.set_prefix(ctx, prefix) @inter_client.slash_command( @@ -308,25 +314,9 @@ async def info(ctx, user=None): ) @commands.has_permissions(administrator=True) async def set_trigger_role(ctx, role): - with open('prefix.json', 'r', encoding='utf-8') as fp: - try: - jsonObject = json.load(fp) - except: - jsonObject = {} - try: - jsonObject[f"{ctx.guild.id}"]["tigger_role"] = role.id - except KeyError: - trigger_role = jsonObject[f"{ctx.guild.id}"] = {'tigger_role': ''} - new = {"tigger_role": role.id} - trigger_role.update(new) - jsonObject[str(ctx.guild.id)] = trigger_role - + await Commands._write_json(ctx.guild.id, "tigger_role", role.id) await ctx.send(f"Role to trigger set to : `{role.name}`") - with open("prefix.json", "w") as fl: - json.dump(jsonObject, fl) - fl.close() - @inter_client.slash_command( name="set_bot_role", @@ -337,25 +327,9 @@ async def set_trigger_role(ctx, role): ) @commands.has_permissions(administrator=True) async def set_bot_role(ctx, role): - with open('prefix.json', 'r', encoding='utf-8') as fp: - try: - jsonObject = json.load(fp) - except: - jsonObject = {} - try: - jsonObject[f"{ctx.guild.id}"]["bot_role"] = role.id - except KeyError: - bot_role = jsonObject[f"{ctx.guild.id}"] = {'bot_role': ''} - new = {"bot_role": role.id} - bot_role.update(new) - jsonObject[str(ctx.guild.id)] = bot_role - + await Commands._write_json(ctx.guild.id, "bot_role", role.id) await ctx.send(f"Setted up bot role to: `{role.name}`") - with open("prefix.json", "w") as fl: - json.dump(jsonObject, fl) - fl.close() - @inter_client.slash_command( name="set_guest_role", @@ -366,24 +340,8 @@ async def set_bot_role(ctx, role): ) @commands.has_permissions(administrator=True) async def set_guest_role(ctx, role): - with open('prefix.json', 'r', encoding='utf-8') as fp: - try: - jsonObject = json.load(fp) - except: - jsonObject = {} - try: - jsonObject[f"{ctx.guild.id}"]["guest_role"] = role.id - except KeyError: - bot_role = jsonObject[f"{ctx.guild.id}"] = {'guest_role': ''} - new = {"guest_role": role.id} - bot_role.update(new) - jsonObject[str(ctx.guild.id)] = bot_role - + await Commands._write_json(ctx.guild.id, "guest_role", role.id) await ctx.send(f"Setted up bot role to: `{role.name}`") - with open("prefix.json", "w") as fl: - json.dump(jsonObject, fl) - fl.close() - bot.run('OTQ3OTUzOTAxNzgzNjIxNjYy.GTXbMv.KrztaTO7-ivsPEAVjsyikSQ-GP-ANwULmDraig')