Changed saving method to rethinkdb instead of config file
This commit is contained in:
parent
11e832027a
commit
2643dc6d80
1 changed files with 14 additions and 30 deletions
|
@ -1,6 +1,5 @@
|
||||||
import ruamel.yaml as yaml
|
import ruamel.yaml as yaml
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
import rethinkdb as r
|
import rethinkdb as r
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
@ -13,7 +12,6 @@ except FileNotFoundError:
|
||||||
print("You have no config file setup! Please use config.yml.sample to setup a valid config file")
|
print("You have no config file setup! Please use config.yml.sample to setup a valid config file")
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bot_token = global_config["bot_token"]
|
bot_token = global_config["bot_token"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -29,9 +27,9 @@ except KeyError:
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
# Default bot's description
|
# Default bot's description
|
||||||
botDescription = global_config.get("description")
|
bot_description = global_config.get("description")
|
||||||
# Bot's default prefix for commands
|
# Bot's default prefix for commands
|
||||||
commandPrefix = global_config.get("command_prefix", "!")
|
command_prefix = global_config.get("command_prefix", "!")
|
||||||
# The key for bots.discord.pw and carbonitex
|
# The key for bots.discord.pw and carbonitex
|
||||||
discord_bots_key = global_config.get('discord_bots_key', "")
|
discord_bots_key = global_config.get('discord_bots_key', "")
|
||||||
carbon_key = global_config.get('carbon_key', "")
|
carbon_key = global_config.get('carbon_key', "")
|
||||||
|
@ -43,7 +41,7 @@ shard_count = global_config.get('shard_count', '')
|
||||||
shard_id = global_config.get('shard_id', '')
|
shard_id = global_config.get('shard_id', '')
|
||||||
|
|
||||||
# A list of all the outputs for the battle command
|
# A list of all the outputs for the battle command
|
||||||
battleWins = global_config.get("battleWins", [])
|
battle_wins = global_config.get("battleWins", [])
|
||||||
# The default status the bot will use
|
# The default status the bot will use
|
||||||
default_status = global_config.get("default_status", "")
|
default_status = global_config.get("default_status", "")
|
||||||
# The steam API key
|
# The steam API key
|
||||||
|
@ -59,42 +57,28 @@ db_cert = global_config.get('db_cert', '')
|
||||||
db_port = global_config.get('db_port', 28015)
|
db_port = global_config.get('db_port', 28015)
|
||||||
|
|
||||||
|
|
||||||
def save_content(key: str, content):
|
async def save_content(table: str, content):
|
||||||
try:
|
|
||||||
with open("config.json", "r+") as jf:
|
|
||||||
data = json.load(jf)
|
|
||||||
data[key] = content
|
|
||||||
jf.seek(0)
|
|
||||||
json.dumps(data)
|
|
||||||
jf.truncate()
|
|
||||||
json.dump(data, jf, indent=4)
|
|
||||||
except FileNotFoundError:
|
|
||||||
with open("config.json", "w+") as jf:
|
|
||||||
json.dump({key: content}, jf, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
# Not in use yet
|
|
||||||
async def _save_content(table: str, content):
|
|
||||||
r.set_loop_type("asyncio")
|
r.set_loop_type("asyncio")
|
||||||
opts = {'host': db_host, 'db': db_name, 'port': db_port, 'ssl': {'ca_certs': db_cert}}
|
opts = {'host': db_host, 'db': db_name, 'port': db_port, 'ssl': {'ca_certs': db_cert}}
|
||||||
conn = await r.connect(**opts)
|
conn = await r.connect(**opts)
|
||||||
# We need to make at least one query to ensure the key exists, so attempt to create it as our query
|
# We need to make at least one query to ensure the key exists, so attempt to create it as our query
|
||||||
try:
|
try:
|
||||||
r.table_create(table).run(conn)
|
await r.table_create(table).run(conn)
|
||||||
except r.ReqlOpFailedError:
|
except r.ReqlOpFailedError:
|
||||||
pass
|
pass
|
||||||
# So now either the table was created already, or it has now been created, we can update the code
|
# So now either the table was created already, or it has now been created, we can update the code
|
||||||
# Since we're handling everything that is rewritten in the code itself, we just need to delet then insert
|
# Since we're handling everything that is rewritten in the code itself, we just need to delet then insert
|
||||||
r.table(table).delete().run(conn)
|
await r.table(table).delete().run(conn)
|
||||||
r.table(table).insert(content).run(conn)
|
await r.table(table).insert(content).run(conn)
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
|
|
||||||
def get_content(key: str):
|
async def get_content(key: str):
|
||||||
|
r.set_loop_type("asyncio")
|
||||||
|
opts = {'host': db_host, 'db': db_name, 'port': db_port, 'ssl': {'ca_certs': db_cert}}
|
||||||
|
conn = await r.connect(**opts)
|
||||||
|
cursor = await r.table(key).run(conn)
|
||||||
try:
|
try:
|
||||||
with open("config.json", "r+") as jf:
|
return list(cursor)[0]
|
||||||
return json.load(jf)[key]
|
except IndexError:
|
||||||
except KeyError:
|
|
||||||
return None
|
|
||||||
except FileNotFoundError:
|
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in a new issue