2016-08-31 07:28:58 +12:00
|
|
|
import ruamel.yaml as yaml
|
2016-07-09 13:27:19 +12:00
|
|
|
import asyncio
|
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-31 07:51:04 +12:00
|
|
|
try:
|
|
|
|
bot_token = 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()
|
|
|
|
|
|
|
|
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-08-19 13:56:41 +12:00
|
|
|
# Default bot's description
|
2016-08-31 10:33:37 +12:00
|
|
|
bot_description = global_config.get("description")
|
2016-08-19 13:56:41 +12:00
|
|
|
# Bot's default prefix for commands
|
2016-08-31 10:33:37 +12:00
|
|
|
command_prefix = 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-08-31 10:33:37 +12:00
|
|
|
battle_wins = global_config.get("battleWins", [])
|
2016-08-19 13:56:41 +12:00
|
|
|
# The default status the bot will use
|
2016-08-31 07:51:04 +12:00
|
|
|
default_status = 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
|
2016-08-31 07:51:04 +12:00
|
|
|
db_host = global_config.get('db_host', 'localhost')
|
2016-08-31 07:21:17 +12:00
|
|
|
# The rethinkdb database name
|
2016-08-31 07:51:04 +12:00
|
|
|
db_name = global_config.get('db_name', 'Discord_Bot')
|
2016-08-31 07:21:17 +12:00
|
|
|
# The rethinkdb certification
|
2016-08-31 07:51:04 +12:00
|
|
|
db_cert = global_config.get('db_cert', '')
|
2016-08-31 07:21:17 +12:00
|
|
|
# The rethinkdb port
|
|
|
|
db_port = global_config.get('db_port', 28015)
|
|
|
|
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-08-31 10:33:37 +12:00
|
|
|
async def save_content(table: str, content):
|
2016-08-31 07:21:17 +12:00
|
|
|
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:
|
2016-08-31 10:33:37 +12:00
|
|
|
await r.table_create(table).run(conn)
|
2016-08-31 07:21:17 +12:00
|
|
|
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
|
2016-08-31 10:33:37 +12:00
|
|
|
await r.table(table).delete().run(conn)
|
|
|
|
await r.table(table).insert(content).run(conn)
|
2016-08-31 07:21:17 +12:00
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
|
2016-08-31 10:33:37 +12:00
|
|
|
async def get_content(key: str):
|
2016-08-31 11:59:55 +12:00
|
|
|
# We need to make sure we're using asyncio
|
2016-08-31 10:33:37 +12:00
|
|
|
r.set_loop_type("asyncio")
|
2016-08-31 11:59:55 +12:00
|
|
|
# Just connect to the database
|
2016-08-31 10:33:37 +12:00
|
|
|
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)
|
2016-08-31 11:59:55 +12:00
|
|
|
# We should only ever get one result, so use it if it exists, otherwise return none
|
2016-07-18 03:17:47 +12:00
|
|
|
try:
|
2016-08-31 11:59:55 +12:00
|
|
|
items = list(cursor.items)[0]
|
2016-08-31 10:33:37 +12:00
|
|
|
except IndexError:
|
2016-08-12 14:14:30 +12:00
|
|
|
return None
|
2016-08-31 11:59:55 +12:00
|
|
|
# Rethink db stores an internal id per table, delete this and return the rest
|
|
|
|
del items['id']
|
|
|
|
return items
|