2016-08-31 12:15:42 +12:00
|
|
|
import asyncio
|
2017-02-26 10:45:39 +13:00
|
|
|
import rethinkdb as r
|
2016-08-31 12:15:42 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2016-07-30 23:58:01 +12:00
|
|
|
import discord
|
2016-07-09 13:27:19 +12:00
|
|
|
from . import config
|
|
|
|
|
2016-08-31 12:15:42 +12:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
2017-03-08 17:28:30 +13:00
|
|
|
# The tables needed for the database, as well as their primary keys
|
|
|
|
required_tables = {
|
|
|
|
'battle_records': 'member_id',
|
|
|
|
'boops': 'member_id',
|
|
|
|
'command_usage': 'command',
|
|
|
|
'motd': 'date',
|
|
|
|
'overwatch': 'member_id',
|
|
|
|
'picarto': 'member_id',
|
2017-03-08 21:08:01 +13:00
|
|
|
'server_settings': 'server_id',
|
2017-03-08 17:28:30 +13:00
|
|
|
'raffles': 'id',
|
2017-03-08 21:08:01 +13:00
|
|
|
'strawpolls': 'server_id',
|
2017-03-11 13:11:21 +13:00
|
|
|
'osu': 'member_id',
|
2017-03-08 21:08:01 +13:00
|
|
|
'tags': 'server_id',
|
2017-03-08 17:28:30 +13:00
|
|
|
'tictactoe': 'member_id',
|
|
|
|
'twitch': 'member_id'
|
|
|
|
}
|
2017-02-26 10:45:39 +13:00
|
|
|
|
|
|
|
|
|
|
|
async def db_check():
|
|
|
|
"""Used to check if the required database/tables are setup"""
|
|
|
|
db_opts = config.db_opts
|
|
|
|
|
|
|
|
r.set_loop_type('asyncio')
|
|
|
|
# First try to connect, and see if the correct information was provided
|
|
|
|
try:
|
|
|
|
conn = await r.connect(**db_opts)
|
|
|
|
except r.errors.ReqlDriverError:
|
|
|
|
print("Cannot connect to the RethinkDB instance with the following information: {}".format(db_opts))
|
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
print("The RethinkDB instance you have setup may be down, otherwise please ensure you setup a"
|
|
|
|
" RethinkDB instance, and you have provided the correct database information in config.yml")
|
2017-02-26 10:45:39 +13:00
|
|
|
quit()
|
2017-03-08 11:35:30 +13:00
|
|
|
return
|
2017-02-26 10:45:39 +13:00
|
|
|
|
|
|
|
# Get the current databases and check if the one we need is there
|
|
|
|
dbs = await r.db_list().run(conn)
|
|
|
|
if db_opts['db'] not in dbs:
|
|
|
|
# If not, we want to create it
|
|
|
|
print('Couldn\'t find database {}...creating now'.format(db_opts['db']))
|
|
|
|
await r.db_create(db_opts['db']).run(conn)
|
|
|
|
# Then add all the tables
|
2017-03-08 17:28:30 +13:00
|
|
|
for table, key in required_tables.items():
|
2017-02-26 10:45:39 +13:00
|
|
|
print("Creating table {}...".format(table))
|
2017-03-08 17:28:30 +13:00
|
|
|
await r.table_create(table, primary_key=key).run(conn)
|
2017-02-26 10:45:39 +13:00
|
|
|
print("Done!")
|
|
|
|
else:
|
|
|
|
# Otherwise, if the database is setup, make sure all the required tables are there
|
|
|
|
tables = await r.table_list().run(conn)
|
2017-03-08 17:28:30 +13:00
|
|
|
for table, key in required_tables.items():
|
2017-02-26 10:45:39 +13:00
|
|
|
if table not in tables:
|
|
|
|
print("Creating table {}...".format(table))
|
2017-03-08 17:28:30 +13:00
|
|
|
await r.table_create(table, primary_key=key).run(conn)
|
2017-02-26 10:45:39 +13:00
|
|
|
print("Done checking tables!")
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
def is_owner(ctx):
|
2016-08-05 02:30:34 +12:00
|
|
|
return ctx.message.author.id in config.owner_ids
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
def custom_perms(**perms):
|
2016-07-30 23:58:01 +12:00
|
|
|
def predicate(ctx):
|
2016-10-07 14:16:24 +13:00
|
|
|
# Return true if this is a private channel, we'll handle that in the registering of the command
|
2017-03-20 11:43:42 +13:00
|
|
|
if type(ctx.message.channel) is discord.DMChannel:
|
2016-10-07 14:16:24 +13:00
|
|
|
return True
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-10-06 07:43:49 +13:00
|
|
|
# Get the member permissions so that we can compare
|
2016-07-30 23:58:01 +12:00
|
|
|
member_perms = ctx.message.author.permissions_in(ctx.message.channel)
|
2016-10-06 07:43:49 +13:00
|
|
|
# Next, set the default permissions if one is not used, based on what was passed
|
|
|
|
# This will be overriden later, if we have custom permissions
|
|
|
|
required_perm = discord.Permissions.none()
|
2016-07-31 12:20:55 +12:00
|
|
|
for perm, setting in perms.items():
|
2016-10-06 07:43:49 +13:00
|
|
|
setattr(required_perm, perm, setting)
|
|
|
|
|
2017-03-08 17:28:30 +13:00
|
|
|
try:
|
2017-03-08 21:38:26 +13:00
|
|
|
server_settings = config.cache.get('server_settings').values
|
|
|
|
required_perm_value = [x for x in server_settings if x['server_id'] == str(
|
|
|
|
ctx.message.guild.id)][0]['permissions'][ctx.command.qualified_name]
|
2017-03-08 17:28:30 +13:00
|
|
|
required_perm = discord.Permissions(required_perm_value)
|
2017-03-08 19:20:23 +13:00
|
|
|
except (TypeError, IndexError, KeyError):
|
2017-03-08 17:28:30 +13:00
|
|
|
pass
|
2016-10-06 07:43:49 +13:00
|
|
|
|
|
|
|
# Now just check if the person running the command has these permissions
|
2016-07-30 23:58:01 +12:00
|
|
|
return member_perms >= required_perm
|
2016-08-31 12:15:42 +12:00
|
|
|
|
2016-08-28 15:00:43 +12:00
|
|
|
predicate.perms = perms
|
2016-07-30 23:58:01 +12:00
|
|
|
return commands.check(predicate)
|