Skip to content

Commit

Permalink
Automatically removes depreciated setting options in settings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
PieTw3lve committed Jul 19, 2024
1 parent 3425336 commit c33835b
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from lightbulb.ext import tasks

VERSION = '1.3.1'
VERSION = '1.3.2'

## Functions ##

Expand All @@ -19,7 +19,7 @@ def install(package):

def get_setting_json():
bot = {
'version': '1.3.0', # DO NOT CHANGE
'version': VERSION, # DO NOT CHANGE
'token': '', # BOT TOKEN (REQUIRED)
'admin_guild_id': [000000000000000000], # ADMIN COMMAND ENABLED GUILDS (OPTIONAL)
'wordnik_api_key': '', # WORDNIK API KEY (OPTIONAL)
Expand Down Expand Up @@ -90,6 +90,7 @@ def update_settings():
with open('settings.json', 'r') as openfile:
data = json.load(openfile)

# Add or update settings
for section in settings:
if section not in data:
data[section] = settings[section]
Expand All @@ -98,6 +99,16 @@ def update_settings():
if option not in data[section]:
data[section][option] = settings[section][option]

# Remove settings not present in get_setting_json()
sections_to_remove = [section for section in data if section not in settings]
for section in sections_to_remove:
del data[section]

for section in data:
options_to_remove = [option for option in data[section] if option not in settings[section]]
for option in options_to_remove:
del data[section][option]

with open('settings.json', 'w') as openfile:
json.dump(data, openfile, indent=4)

Expand Down Expand Up @@ -164,27 +175,25 @@ def get_commands(bot: lightbulb.BotApp) -> dict:
## Script ##

if __name__ == '__main__':

# Generate settings.json if not found
if not os.path.isfile('settings.json') or not os.access('settings.json', os.R_OK): # checks if file exists
dictionary = get_setting_json()

with io.open(os.path.join('', 'settings.json'), 'w') as openfile:
openfile.write(json.dumps(dictionary, indent=4))

settings = 'settings.json'
if not os.path.isfile(settings) or not os.access(settings, os.R_OK):
settings = get_setting_json()
with io.open(settings, 'w') as file:
file.write(json.dumps(settings, indent=4))
print('Please add your bot information to settings.json')
sys.exit(0)
else:
version = get_setting('bot', 'version')
if version != VERSION:
write_setting('bot', 'version', VERSION)
update_settings()

version = get_setting('bot', 'version')
if version != VERSION:
write_setting('bot', 'version', VERSION)
update_settings()

# Check if bot token is set
token = get_setting('bot', 'token')
admin_guild_id = get_setting('bot', 'admin_guild_id')
if not token:
print('Please add your bot token to settings.json')
print('Please add your bot information to settings.json')
sys.exit(0)

# Create a bot instance
Expand All @@ -201,9 +210,9 @@ async def on_started(event):
# Generate database directory if not found
if not os.path.exists('database'):
os.makedirs('database')

db = sqlite3.connect(get_setting('settings', 'database_data_dir'))
cursor = db.cursor() # checks if db exists
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS economy (
user_id INTEGER,
balance INTEGER,
Expand Down Expand Up @@ -244,5 +253,9 @@ async def on_started(event):
bot.load_extensions_from('extensions', recursive=True)
bot.run(
status=hikari.Status.DO_NOT_DISTURB,
activity=hikari.Activity(name='Rushsite Season 4', type=hikari.ActivityType.STREAMING, url='https://www.twitch.tv/ryqb')
activity=hikari.Activity(
name='Rushsite Season 4',
type=hikari.ActivityType.STREAMING,
url='https://www.twitch.tv/ryqb'
)
)

0 comments on commit c33835b

Please sign in to comment.