2016-08-31 07:28:58 +12:00
|
|
|
import ruamel.yaml as yaml
|
2016-07-09 13:27:19 +12:00
|
|
|
import asyncio
|
2016-07-17 08:26:30 +12:00
|
|
|
import json
|
2016-08-31 07:21:17 +12:00
|
|
|
import rethinkdb as r
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
2016-08-19 13:56:41 +12:00
|
|
|
# Ensure that the required config.yml file actually exists
|
2016-08-12 14:14:30 +12:00
|
|
|
try:
|
|
|
|
with open("config.yml", "r") as f:
|
|
|
|
global_config = yaml.load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("You have no config file setup! Please use config.yml.sample to setup a valid config file")
|
|
|
|
quit()
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-08-19 13:56:41 +12:00
|
|
|
# Default bot's description
|
2016-07-09 13:27:19 +12:00
|
|
|
botDescription = global_config.get("description")
|
2016-08-19 13:56:41 +12:00
|
|
|
# Bot's default prefix for commands
|
2016-08-05 02:30:34 +12:00
|
|
|
commandPrefix = global_config.get("command_prefix", "!")
|
2016-08-19 13:56:41 +12:00
|
|
|
# The key for bots.discord.pw and carbonitex
|
2016-08-12 14:14:30 +12:00
|
|
|
discord_bots_key = global_config.get('discord_bots_key', "")
|
2016-08-19 13:56:41 +12:00
|
|
|
carbon_key = global_config.get('carbon_key', "")
|
|
|
|
# The invite link for the server made for the bot
|
2016-08-16 15:30:52 +12:00
|
|
|
dev_server = global_config.get("dev_server", "")
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-08-31 07:21:17 +12:00
|
|
|
# The variables needed for sharding
|
|
|
|
shard_count = global_config.get('shard_count', '')
|
|
|
|
shard_id = global_config.get('shard_id', '')
|
|
|
|
|
2016-08-19 13:56:41 +12:00
|
|
|
# A list of all the outputs for the battle command
|
2016-07-09 13:27:19 +12:00
|
|
|
battleWins = global_config.get("battleWins", [])
|
2016-08-19 13:56:41 +12:00
|
|
|
# The default status the bot will use
|
2016-07-09 13:27:19 +12:00
|
|
|
defaultStatus = global_config.get("default_status", "")
|
2016-08-19 18:48:15 +12:00
|
|
|
# The steam API key
|
|
|
|
steam_key = global_config.get("steam_key", "")
|
|
|
|
|
2016-08-31 07:21:17 +12:00
|
|
|
# The rethinkdb hostname
|
|
|
|
db_host = global_config.get('db_host', '')
|
|
|
|
# The rethinkdb database name
|
|
|
|
db_name = global_config.get('db_name', '')
|
|
|
|
# The rethinkdb certification
|
|
|
|
db_cert = global_config.get('db_cert', 'localhost')
|
|
|
|
# The rethinkdb port
|
|
|
|
db_port = global_config.get('db_port', 28015)
|
|
|
|
|
2016-08-12 14:14:30 +12:00
|
|
|
try:
|
|
|
|
botToken = global_config["bot_token"]
|
|
|
|
except KeyError:
|
|
|
|
print("You have no bot_token saved, this is a requirement for running a bot.")
|
|
|
|
print("Please use config.yml.sample to setup a valid config file")
|
|
|
|
quit()
|
2016-08-31 07:21:17 +12:00
|
|
|
|
2016-08-12 14:14:30 +12:00
|
|
|
try:
|
|
|
|
owner_ids = global_config["owner_id"]
|
|
|
|
except KeyError:
|
|
|
|
print("You have no owner_id saved! You're not going to be able to run certain commands without this.")
|
|
|
|
print("Please use config.yml.sample to setup a valid config file")
|
|
|
|
quit()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
def save_content(key: str, content):
|
2016-08-13 07:52:44 +12:00
|
|
|
try:
|
|
|
|
with open("config.json", "r+") as jf:
|
|
|
|
data = json.load(jf)
|
|
|
|
data[key] = content
|
|
|
|
jf.seek(0)
|
2016-08-14 15:52:55 +12:00
|
|
|
json.dumps(data)
|
2016-08-13 07:52:44 +12:00
|
|
|
jf.truncate()
|
|
|
|
json.dump(data, jf, indent=4)
|
|
|
|
except FileNotFoundError:
|
|
|
|
with open("config.json", "w+") as jf:
|
|
|
|
json.dump({key: content}, jf, indent=4)
|
2016-07-18 05:12:57 +12:00
|
|
|
|
2016-07-18 05:11:47 +12:00
|
|
|
|
2016-08-31 07:21:17 +12:00
|
|
|
# Not in use yet
|
|
|
|
async def _save_content(table: str, content):
|
|
|
|
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)
|
|
|
|
# We need to make at least one query to ensure the key exists, so attempt to create it as our query
|
|
|
|
try:
|
|
|
|
r.table_create(table).run(conn)
|
|
|
|
except r.ReqlOpFailedError:
|
|
|
|
pass
|
|
|
|
# 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
|
|
|
|
r.table(table).delete().run(conn)
|
|
|
|
r.table(table).insert(content).run(conn)
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
def get_content(key: str):
|
2016-07-18 03:17:47 +12:00
|
|
|
try:
|
2016-08-12 14:14:30 +12:00
|
|
|
with open("config.json", "r+") as jf:
|
2016-07-18 23:53:32 +12:00
|
|
|
return json.load(jf)[key]
|
2016-07-18 03:17:47 +12:00
|
|
|
except KeyError:
|
|
|
|
return None
|
2016-08-12 14:14:30 +12:00
|
|
|
except FileNotFoundError:
|
|
|
|
return None
|