1
0
Fork 0
mirror of synced 2024-06-17 18:14:32 +12:00
Bonfire/cogs/utils/checks.py

104 lines
3.8 KiB
Python
Raw Normal View History

import asyncio
import rethinkdb as r
2016-07-09 13:27:19 +12:00
from discord.ext import commands
import discord
2016-07-09 13:27:19 +12:00
from . import config
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',
'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',
2017-06-07 20:30:19 +12:00
'twitch': 'member_id',
'user_playlists': 'member_id'
2017-03-08 17:28:30 +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")
quit()
2017-03-08 11:35:30 +13:00
return
# 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():
print("Creating table {}...".format(table))
2017-03-08 17:28:30 +13:00
await r.table_create(table, primary_key=key).run(conn)
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():
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)
print("Done checking tables!")
2016-07-09 13:27:19 +12:00
2017-03-08 11:35:30 +13:00
def is_owner(ctx):
if not hasattr(ctx.bot, "owner"):
return False
return ctx.bot.owner.id == ctx.message.author.id
2016-07-09 13:27:19 +12:00
2017-04-20 15:18:26 +12:00
2017-06-07 20:30:19 +12:00
def should_ignore(bot, message):
2017-04-17 13:58:20 +12:00
if message.guild is None:
return False
2017-06-07 20:30:19 +12:00
ignored = bot.db.load('server_settings', key=message.guild.id, pluck='ignored')
if not ignored:
return False
2017-06-07 20:30:19 +12:00
return str(message.author.id) in ignored['members'] or str(message.channel.id) in ignored['channels']
2017-04-11 17:22:11 +12:00
def custom_perms(**perms):
def predicate(ctx):
# Return true if this is a private channel, we'll handle that in the registering of the command
if type(ctx.message.channel) is discord.DMChannel:
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
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-06-07 20:30:19 +12:00
required_perm_value = ctx.bot.db.load('server_settings', key=ctx.message.guild.id, pluck='permissions') or {}
required_perm_value = required_perm_value.get(ctx.command.qualified_name)
if required_perm_value:
2017-03-08 17:28:30 +13:00
required_perm = discord.Permissions(required_perm_value)
2016-10-06 07:43:49 +13:00
# Now just check if the person running the command has these permissions
return member_perms >= required_perm
predicate.perms = perms
return commands.check(predicate)