Store bot data in a data/ directory

This commit is contained in:
Astrid 2021-01-06 11:53:51 +01:00
parent 2c638970de
commit 47b5788a09
3 changed files with 17 additions and 7 deletions

View File

@ -2,12 +2,13 @@
import os, json, datetime, re import os, json, datetime, re
import sqlite3 as sql import sqlite3 as sql
data_dir = "data"
def create_connection(): def create_connection():
#create connection, create db if doesn't exist #create connection, create db if doesn't exist
conn = None conn = None
try: try:
conn = sql.connect("matteo.db") conn = sql.connect(os.path.join(data_dir, "matteo.db"))
return conn return conn
except: except:
print("oops, db connection no work") print("oops, db connection no work")

View File

@ -2,8 +2,13 @@ import json, random, os, math, jsonpickle
from enum import Enum from enum import Enum
import database as db import database as db
data_dir = "data"
games_config_file = os.path.join(data_dir, "games_config.json")
def config(): def config():
if not os.path.exists("games_config.json"): if not os.path.exists(os.path.dirname(games_config_file)):
os.makedirs(os.path.dirname(games_config_file))
if not os.path.exists(games_config_file):
#generate default config #generate default config
config_dic = { config_dic = {
"default_length" : 3, "default_length" : 3,
@ -16,11 +21,11 @@ def config():
"stolen_base_chance_mod" : 1, "stolen_base_chance_mod" : 1,
"stolen_base_success_mod" : 1 "stolen_base_success_mod" : 1
} }
with open("games_config.json", "w") as config_file: with open(games_config_file, "w") as config_file:
json.dump(config_dic, config_file, indent=4) json.dump(config_dic, config_file, indent=4)
return config_dic return config_dic
else: else:
with open("games_config.json") as config_file: with open(games_config_file) as config_file:
return json.load(config_file) return json.load(config_file)
def all_weathers(): def all_weathers():

View File

@ -4,6 +4,8 @@ import onomancer as ono
from flask import Flask from flask import Flask
from uuid import uuid4 from uuid import uuid4
data_dir = "data"
config_filename = os.path.join(data_dir, "config.json")
class Command: class Command:
def isauthorized(self, user): def isauthorized(self, user):
@ -605,7 +607,9 @@ thread1 = threading.Thread(target=main_controller.update_loop)
thread1.start() thread1.start()
def config(): def config():
if not os.path.exists("config.json"): if not os.path.exists(os.path.dirname(config_filename)):
os.makedirs(os.path.dirname(config_filename))
if not os.path.exists(config_filename):
#generate default config #generate default config
config_dic = { config_dic = {
"token" : "", "token" : "",
@ -617,12 +621,12 @@ def config():
"soulscream channel id" : 0, "soulscream channel id" : 0,
"game_freeze" : 0 "game_freeze" : 0
} }
with open("config.json", "w") as config_file: with open(config_filename, "w") as config_file:
json.dump(config_dic, config_file, indent=4) json.dump(config_dic, config_file, indent=4)
print("please fill in bot token and any bot admin discord ids to the new config.json file!") print("please fill in bot token and any bot admin discord ids to the new config.json file!")
quit() quit()
else: else:
with open("config.json") as config_file: with open(config_filename) as config_file:
return json.load(config_file) return json.load(config_file)
@client.event @client.event