Skip to content

Commit

Permalink
changed Config to use the new configuration structure (see #4 )
Browse files Browse the repository at this point in the history
  • Loading branch information
s0lst1ce committed Jun 2, 2019
1 parent d00ae4c commit 5477a83
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 83 deletions.
87 changes: 25 additions & 62 deletions Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]")
Expand All @@ -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)
Expand Down Expand Up @@ -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]")
Expand All @@ -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.")
Expand Down Expand Up @@ -206,59 +193,35 @@ 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:
local_logger.exception(e)
raise e







async def cfg_todo(self, ctx):
pass


async def cfg_welcome(self, ctx):
pass



@cfg.command()
Expand Down
20 changes: 1 addition & 19 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 <command>` 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}
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."
38 changes: 36 additions & 2 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


#########################################
# #
Expand All @@ -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
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

0 comments on commit 5477a83

Please sign in to comment.