diff --git a/Config.py b/Config.py index 727638d..64f104c 100644 --- a/Config.py +++ b/Config.py @@ -46,6 +46,7 @@ async def cfg(self, ctx): if ctx.invoked_subcommand == None: ctx.send(ERROR_NO_SUBCOMMAND) + @cfg.command() async def init(self, ctx): #creating new hidden channel only the owner can see @@ -55,6 +56,11 @@ async def init(self, ctx): } self.config_channels[ctx.guild.id] = await ctx.guild.create_text_channel("cli-bot-config") + #making conf file if it doesn't exist + if not is_init(): + with open(f"{ctx.guild.id}.json", "w") as file: + pass + #starting all configurations await self.config_channels[ctx.guild.id].send(f'''You are about to start the configuration of {ctx.me.mention}. If you are unfamiliar with CLI (Command Line Interface) you may want to check the documentation on github ({WEBSITE}). The same goes if you don't know the bot's functionnalities\n*Starting full configuration...*''') await self.config_channels[ctx.guild.id].send("This will overwrite all of your existing configurations. Do you want to continue ? [y/n]") @@ -76,6 +82,8 @@ async def init(self, ctx): local_logger.exception(e) + @cfg.command() + @is_init() async def chg(self, ctx, setting): try: eval("self.chg_"+setting) @@ -110,7 +118,7 @@ async def cfg_poll(self, ctx): #building string with all the channels that will be marked for polls poll_channels_str = "" - for chan in response.channel_mentions: + for chan in poll_channels: poll_channels_str+= " "+chan.mention await self.config_channels[ctx.guild.id].send(f"You are about to make {poll_channels_str} poll channels. Do you want to continue? [y/n]") @@ -126,36 +134,15 @@ async def cfg_poll(self, ctx): retry = False - #making the data to be saved - with open(POLL_ALLOWED_CHANNELS_FILE, "r") as file: - to_write = [] - for line in file.readlines(): - '''the file is organized like this: - \nguild_id;poll_chan_1_id;poll_chan_2_id;''' - segments = line.split(";") - if not int(segments[0])==ctx.guild.id: - to_write.append(line) - guild_chans = f"{ctx.guild.id};" - for chan in poll_channels: - guild_chans+= f"{chan.id};" + old_conf = get_conf(ctx.guild.id) + old_conf["poll_channels"] = poll_channels - #removing the last ";" to prevent Poll from trying to convert it to an int - guild_chans = guild_chans[:-1] + "\n" + if update_conf(ctx.guild.id, old_conf) == False: + await self.config_channels[ctx.guild.id].send(ERR_UNEXCPECTED) - to_write.append(guild_chans) - local_logger.info(str(guild_chans)) - - #writting to the file - write_str = "" - for line in to_write: - write_str+=line - - local_logger.info(write_str) - with open(POLL_ALLOWED_CHANNELS_FILE, "w") as file: - file.write(write_str) - - await self.config_channels[ctx.guild.id].send("Poll configuration is done.") + else: + await self.config_channels[ctx.guild.id].send("Poll configuration is done.") local_logger.info(f"Configuration of poll for server {ctx.guild.name} ({ctx.guild.id}) has been completed.") @@ -206,42 +193,22 @@ async def cfg_roll(self, ctx): #adding to master role list new_roles.append(new_role) - #writting configuration to file - with open(ROLES_FILE, "r") as file: - '''the file is written like this: - guild_id;management_role_1|management_role_2;admin_role_1|admin_role_2''' - for line in file.readlines(): - segments = line.split(";") - to_write = "" - if int(segments[0]) == ctx.guild.id: - continue - #if the line isn't defining the server's role settings, marking it for rewrite - to_write += line #giving admin roles the manager clearance for m_role in new_roles[1]: new_roles[0].append(m_role) - #adding management roles - guild_line = f"{ctx.guild.id};" - for role in new_roles[0]: - guild_line += f"{role}|" - - #adding strong separator between clearance levels - guild_line+=";" + old_conf = get_conf(ctx.guild.id) - #adding admin roles - for role in new_roles[1]: - guild_line += f"{role}|" - - #addind the modified guild role configuration - to_write+= guild_line[:-1]+"\n" - - #writting to file - with open(ROLES_FILE, "w") as file: - file.write(to_write) + #updating the values + old_conf["roles"]["manager"] = new_roles[0] + old_conf["roles"]["admin"] = new_roles[1] + if update_conf(ctx.guild.id, old_conf) == False: + await self.config_channels[ctx.guild.id]send(ERR_UNEXCPECTED) + else: + await self.config_channels[ctx.guild.id]send("Successfully updated role configuration") except Exception as e: @@ -249,16 +216,12 @@ async def cfg_roll(self, ctx): raise e - - - - - async def cfg_todo(self, ctx): pass + async def cfg_welcome(self, ctx): - pass + @cfg.command() diff --git a/settings.py b/settings.py index 5a54ae6..e717a09 100644 --- a/settings.py +++ b/settings.py @@ -30,14 +30,6 @@ "no_entry_sign": "\U0001F6AB" } -ROLES_FILE = "roles.txt" -SLAPPED_LOG_FILE = "slapped.txt" -ENABLED_EXTENSIONS_FILE = "enabled_ext.txt" -POLL_ALLOWED_CHANNELS_FILE = "poll_channels.txt" -TODO_CHANNEL_FILE = "todo_channel.txt" -TODO_TYPES_FILE = "todo_types.txt" -WELCOME_MESSAGE_FILE = "welcome_messages.txt" - #data used only for Todo -> maybe remove it ? PUBLIC_REPOST="Public repost" @@ -56,14 +48,4 @@ ERR_UNEXCPECTED = "An unexcpected error occured. Please report a bug in {} or contact an admin of your server." ERR_NOT_ENOUGH_ARG = "This command requires additional arguments. See `::help ` to get more information on the command's usage" ERR_UNSUFFICIENT_PRIVILEGE = "You don't have the permission to do this..." - - - -'''#DEPRECATED -CHANNELS = { - "rules": 566569408416186377, - "faq": 566618400307019776 -} -''' - - For a complete list of all commands enbaled for this clearance see {WEBSITE} \ No newline at end of file +ERR_NOT_SETUP = "This server hasn't been configured. If you're the owner of the server you can initialise the bot by doing `::cfg init` in any channel. You won't be able to use the bot before that." \ No newline at end of file diff --git a/utilities.py b/utilities.py index 44e2d24..c7b3134 100644 --- a/utilities.py +++ b/utilities.py @@ -33,6 +33,17 @@ def check_condition(ctx): ctx.send(ERR_UNSUFFICIENT_PRIVILEGE) return result +def is_init(): + def check_condition(ctx): + conf_files = os.listdir() + file_name = f"{ctx.guild.id}.json" + return file_name in conf_files + + result = commands.check(check_condition) + if result == False: + ctx.send(ERR_NOT_SETUP) + return result + ######################################### # # @@ -43,10 +54,33 @@ def check_condition(ctx): ######################################### def get_m_time(file): - return os.getmtime(file+".conf") + return os.getmtime(file+"json") def has_changed(server, last_time): last_update = get_m_time(file) if last_update != last_time: return True - return False \ No newline at end of file + return False + +def get_conf(guild_id): + with open(f"{guild_id}.json", "r") as file: + return conf = json.load(file) + +def update_conf(guild_id, conf_dict): + try: + with open(f"{guild_id}.json", "r") as file: + json.dump(file, conf_dict) + return True + + except Exception as e: + local_logger.exception(e) + return False + +def del_conf(guild_id): + try: + os.remove(f"{guild_id}.json") + return True + + except Exception as e: + local_logger.exception(e) + return False \ No newline at end of file