[V3 Core] local whitelist/blacklist (#1776)

* implements local whitelist/blacklist which had unused bot.db settings

This includes a role listing

* format pass

* Update core_commands.py

* .

* black format pass
This commit is contained in:
Michael H 2018-06-05 16:19:44 -04:00 committed by palmtree5
parent 0b78664792
commit 4aaef9558a
2 changed files with 175 additions and 2 deletions

View File

@ -1383,6 +1383,177 @@ class Core(CoreLogic):
await ctx.bot.db.blacklist.set([]) await ctx.bot.db.blacklist.set([])
await ctx.send(_("blacklist has been cleared.")) await ctx.send(_("blacklist has been cleared."))
@commands.group()
@commands.guild_only()
@checks.admin_or_permissions(administrator=True)
async def localwhitelist(self, ctx):
"""
Whitelist management commands.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help()
@localwhitelist.command(name="add")
async def localwhitelist_add(self, ctx, *, user_or_role: str):
"""
Adds a user or role to the whitelist.
"""
try:
obj = await commands.MemberConverter().convert(ctx, user_or_role)
except commands.BadArgument:
obj = await commands.RoleConverter().convert(ctx, user_or_role)
user = False
else:
user = True
async with ctx.bot.db.guild(ctx.guild).whitelist() as curr_list:
if obj.id not in curr_list:
curr_list.append(obj.id)
if user:
await ctx.send(_("User added to whitelist."))
else:
await ctx.send(_("Role added to whitelist."))
@localwhitelist.command(name="list")
async def localwhitelist_list(self, ctx):
"""
Lists whitelisted users and roles.
"""
curr_list = await ctx.bot.db.guild(ctx.guild).whitelist()
msg = _("Whitelisted Users and roles:")
for obj in curr_list:
msg += "\n\t- {}".format(obj)
for page in pagify(msg):
await ctx.send(box(page))
@localwhitelist.command(name="remove")
async def localwhitelist_remove(self, ctx, *, user_or_role: str):
"""
Removes user or role from whitelist.
"""
try:
obj = await commands.MemberConverter().convert(ctx, user_or_role)
except commands.BadArgument:
obj = await commands.RoleConverter().convert(ctx, user_or_role)
user = False
else:
user = True
removed = False
async with ctx.bot.db.guild(ctx.guild).whitelist() as curr_list:
if obj.id in curr_list:
removed = True
curr_list.remove(obj.id)
if removed:
if user:
await ctx.send(_("User has been removed from whitelist."))
else:
await ctx.send(_("Role has been removed from whitelist."))
else:
if user:
await ctx.send(_("User was not in the whitelist."))
else:
await ctx.send(_("Role was not in the whitelist."))
@localwhitelist.command(name="clear")
async def localwhitelist_clear(self, ctx):
"""
Clears the whitelist.
"""
await ctx.bot.db.guild(ctx.guild).whitelist.set([])
await ctx.send(_("Whitelist has been cleared."))
@commands.group()
@commands.guild_only()
@checks.admin_or_permissions(administrator=True)
async def localblacklist(self, ctx):
"""
blacklist management commands.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help()
@localblacklist.command(name="add")
async def localblacklist_add(self, ctx, *, user_or_role: str):
"""
Adds a user or role to the blacklist.
"""
try:
obj = await commands.MemberConverter().convert(ctx, user_or_role)
except commands.BadArgument:
obj = await commands.RoleConverter().convert(ctx, user_or_role)
user = False
else:
user = True
if user and await ctx.bot.is_owner(obj):
ctx.send(_("You cannot blacklist an owner!"))
return
async with ctx.bot.db.guild(ctx.guild).blacklist() as curr_list:
if obj.id not in curr_list:
curr_list.append(obj.id)
if user:
await ctx.send(_("User added to blacklist."))
else:
await ctx.send(_("Role added to blacklist."))
@localblacklist.command(name="list")
async def localblacklist_list(self, ctx):
"""
Lists blacklisted users and roles.
"""
curr_list = await ctx.bot.db.guild(ctx.guild).blacklist()
msg = _("blacklisted Users and Roles:")
for obj in curr_list:
msg += "\n\t- {}".format(obj)
for page in pagify(msg):
await ctx.send(box(page))
@localblacklist.command(name="remove")
async def localblacklist_remove(self, ctx, *, user_or_role: str):
"""
Removes user or role from blacklist.
"""
removed = False
try:
obj = await commands.MemberConverter().convert(ctx, user_or_role)
except commands.BadArgument:
obj = await commands.RoleConverter().convert(ctx, user_or_role)
user = False
else:
user = True
async with ctx.bot.db.guild(ctx.guild).blacklist() as curr_list:
if obj.id in curr_list:
removed = True
curr_list.remove(obj.id)
if removed:
if user:
await ctx.send(_("User has been removed from blacklist."))
else:
await ctx.send(_("Role has been removed from blacklist."))
else:
if user:
await ctx.send(_("User was not in the blacklist."))
else:
await ctx.send(_("Role was not in the blacklist."))
@localblacklist.command(name="clear")
async def localblacklist_clear(self, ctx):
"""
Clears the blacklist.
"""
await ctx.bot.db.guild(ctx.guild).blacklist.set([])
await ctx.send(_("blacklist has been cleared."))
# RPC handlers # RPC handlers
async def rpc_load(self, request): async def rpc_load(self, request):
cog_name = request.params[0] cog_name = request.params[0]

View File

@ -26,10 +26,12 @@ def init_global_checks(bot):
local_blacklist = await guild_settings.blacklist() local_blacklist = await guild_settings.blacklist()
local_whitelist = await guild_settings.whitelist() local_whitelist = await guild_settings.whitelist()
_ids = [r.id for r in ctx.author.roles if not r.is_default]
_ids.append(ctx.author.id)
if local_whitelist: if local_whitelist:
return ctx.author.id in local_whitelist return any(i in local_whitelist for i in _ids)
return ctx.author.id not in local_blacklist return not any(i in local_blacklist for i in _ids)
@bot.check @bot.check
async def bots(ctx): async def bots(ctx):