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-09-01 15:24:21 +12:00
|
|
|
import pendulum
|
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-09-01 07:09:11 +12:00
|
|
|
|
|
|
|
# This is a simple class for the cache concept, all it holds is it's own key and the values
|
|
|
|
# With a method that gets content based on it's key
|
|
|
|
class Cache:
|
|
|
|
def __init__(self, key):
|
|
|
|
self.key = key
|
2016-09-01 08:28:52 +12:00
|
|
|
self.values = {}
|
2016-09-01 15:24:21 +12:00
|
|
|
self.refreshed = pendulum.utcnow()
|
2016-09-01 07:09:11 +12:00
|
|
|
loop.create_task(self.update())
|
|
|
|
|
|
|
|
async def update(self):
|
|
|
|
self.values = await _get_content(self.key)
|
2016-09-01 15:24:21 +12:00
|
|
|
self.refreshed = pendulum.utcnow()
|
2016-08-31 12:44:24 +12:00
|
|
|
|
|
|
|
|
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-09-01 07:09:11 +12:00
|
|
|
default_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', "")
|
2016-09-21 10:22:02 +12:00
|
|
|
# The client ID for twitch requsets
|
|
|
|
twitch_key = global_config.get('twitch_key', "")
|
2016-08-19 13:56:41 +12:00
|
|
|
# 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-09-01 08:21:52 +12:00
|
|
|
# The user and password assigned
|
|
|
|
db_user = global_config.get('db_user', 'admin')
|
|
|
|
db_pass = global_config.get('db_pass', '')
|
2016-08-31 13:48:30 +12:00
|
|
|
# We've set all the options we need to be able to connect
|
|
|
|
# so create a dictionary that we can use to unload to connect
|
2016-09-01 08:21:52 +12:00
|
|
|
# db_opts = {'host': db_host, 'db': db_name, 'port': db_port, 'ssl':
|
|
|
|
# {'ca_certs': db_cert}, 'user': db_user, 'password': db_pass}
|
|
|
|
db_opts = {'host': db_host, 'db': db_name, 'port': db_port, 'user': db_user, 'password': db_pass}
|
2016-09-01 07:09:11 +12:00
|
|
|
|
2016-09-01 07:17:33 +12:00
|
|
|
possible_keys = ['prefixes', 'battling', 'battle_records', 'boops', 'server_alerts', 'user_notifications',
|
|
|
|
'nsfw_channels', 'custom_permissions', 'rules', 'overwatch', 'picarto', 'twitch', 'strawpolls', 'tags',
|
2016-09-11 20:28:57 +12:00
|
|
|
'tictactoe', 'bot_data', 'command_manage']
|
|
|
|
|
2016-09-01 07:09:11 +12:00
|
|
|
# This will be a dictionary that holds the cache object, based on the key that is saved
|
|
|
|
cache = {}
|
|
|
|
|
2016-09-01 09:19:39 +12:00
|
|
|
sharded_data = {}
|
|
|
|
|
2016-09-01 07:09:11 +12:00
|
|
|
# Populate cache with each object
|
2016-09-01 15:24:21 +12:00
|
|
|
for k in possible_keys:
|
|
|
|
cache[k] = Cache(k)
|
2016-09-01 09:45:00 +12:00
|
|
|
|
2016-09-01 07:09:11 +12:00
|
|
|
|
|
|
|
def command_prefix(bot, message):
|
|
|
|
# We do not want to make a query for every message that is sent
|
|
|
|
# So assume it's in cache, or it doesn't exist
|
|
|
|
# If the prefix does exist in the database and isn't in our cache; too bad, something has messed up
|
|
|
|
# But it is not worth a query for every single message the bot detects, to fix
|
2016-09-01 10:53:33 +12:00
|
|
|
try:
|
|
|
|
prefix = cache['prefixes'].values.get(message.server.id)
|
|
|
|
return prefix or default_prefix
|
|
|
|
except KeyError:
|
|
|
|
return default_prefix
|
2016-09-01 07:09:11 +12:00
|
|
|
|
2016-09-26 16:58:12 +13:00
|
|
|
async def add_content(table, filter, content):
|
|
|
|
r.set_loop_type("asyncio")
|
|
|
|
conn = await r.connect(**db_opts)
|
|
|
|
# First we need to make sure that this entry doesn't exist
|
|
|
|
# For all rethinkDB cares, multiple entries can exist with the same content
|
|
|
|
# For our purposes however, we do not want this
|
|
|
|
try:
|
|
|
|
cursor = await r.table(table).filter(filter).run(conn)
|
|
|
|
if len(list(cursor)) > 0:
|
|
|
|
await conn.close()
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
await r.table(table).insert(content).run(conn)
|
|
|
|
await conn.close()
|
|
|
|
return True
|
|
|
|
except r.ReqlOpFailedError:
|
|
|
|
# This means the table does not exist
|
|
|
|
await r.create_table(table).run(conn)
|
|
|
|
await r.table(table).filter(filter).insert(content).run(conn)
|
|
|
|
await con.close()
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def update_content(table, filter, content):
|
|
|
|
r.set_loop_type("asyncio")
|
|
|
|
conn = await r.connect(**db_opts)
|
|
|
|
# This method is only for updating content, so if we find that it doesn't exist, just return false
|
|
|
|
try:
|
|
|
|
# Update based on the content and filter passed to us
|
|
|
|
# rethinkdb allows you to do many many things inside of update
|
|
|
|
# This is why we're accepting a variable and using it, whatever it may be, as the query
|
|
|
|
# Will take some testing, but I might not even need the add_content method above
|
|
|
|
await r.table(table).filter(filter).update(content).run(conn)
|
|
|
|
except r.ReqlOpFailedError:
|
|
|
|
await conn.close()
|
|
|
|
return False
|
2016-09-11 20:28:57 +12:00
|
|
|
await conn.close()
|
2016-09-26 16:58:12 +13:00
|
|
|
return True
|
|
|
|
|
|
|
|
async def get_content(key: str, filter):
|
|
|
|
r.set_loop_type("asyncio")
|
|
|
|
conn = await r.connect(**db_opts)
|
|
|
|
try:
|
|
|
|
cursor = await r.table(key).filter(filter).run(conn)
|
|
|
|
content = list(cursor.items)[0]
|
|
|
|
del content['id']
|
|
|
|
except (IndexError, r.ReqlOpFailedError):
|
|
|
|
content = None
|
2016-09-01 16:41:43 +12:00
|
|
|
await conn.close()
|
2016-09-26 16:58:12 +13:00
|
|
|
return content
|
|
|
|
|