[Core] Target any user with blacklist / whitelist commands (#1068)

This commit is contained in:
Twentysix 2017-11-06 23:51:33 +01:00 committed by GitHub
parent 4f61daf51a
commit 58d669d07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import discord
from discord.ext import commands
from cogs.utils import checks
from cogs.utils.converters import GlobalUser
from __main__ import set_cog
from .utils.dataIO import dataIO
from .utils.chat_formatting import pagify, box
@ -535,7 +536,7 @@ class Owner:
await self.bot.send_cmd_help(ctx)
@blacklist.command(name="add")
async def _blacklist_add(self, user: discord.Member):
async def _blacklist_add(self, user: GlobalUser):
"""Adds user to Red's global blacklist"""
if user.id not in self.global_ignores["blacklist"]:
self.global_ignores["blacklist"].append(user.id)
@ -545,7 +546,7 @@ class Owner:
await self.bot.say("User is already blacklisted.")
@blacklist.command(name="remove")
async def _blacklist_remove(self, user: discord.Member):
async def _blacklist_remove(self, user: GlobalUser):
"""Removes user from Red's global blacklist"""
if user.id in self.global_ignores["blacklist"]:
self.global_ignores["blacklist"].remove(user.id)
@ -583,7 +584,7 @@ class Owner:
await self.bot.send_cmd_help(ctx)
@whitelist.command(name="add")
async def _whitelist_add(self, user: discord.Member):
async def _whitelist_add(self, user: GlobalUser):
"""Adds user to Red's global whitelist"""
if user.id not in self.global_ignores["whitelist"]:
if not self.global_ignores["whitelist"]:
@ -597,7 +598,7 @@ class Owner:
await self.bot.say("User is already whitelisted.")
@whitelist.command(name="remove")
async def _whitelist_remove(self, user: discord.Member):
async def _whitelist_remove(self, user: GlobalUser):
"""Removes user from Red's global whitelist"""
if user.id in self.global_ignores["whitelist"]:
self.global_ignores["whitelist"].remove(user.id)
@ -927,7 +928,7 @@ class Owner:
for user_id in _list:
user = discord.utils.get(self.bot.get_all_members(), id=user_id)
if user:
users.append(str(user))
users.append("{} ({})".format(user, user.id))
if users:
not_found = total - len(users)

46
cogs/utils/converters.py Normal file
View File

@ -0,0 +1,46 @@
from discord.ext.commands.converter import IDConverter
from discord.ext.commands.errors import BadArgument
import re
# This could've been imported but since it's an internal it's safer
# to get it here
def _get_from_servers(bot, getter, argument):
result = None
for server in bot.servers:
result = getattr(server, getter)(argument)
if result:
return result
return result
class GlobalUser(IDConverter):
"""
This is an (almost) straight copy of discord.py's Member converter
The key difference is that if the command is issued in a server it will
first attempt to get the user from that server and upon failing it will
attempt to fish it from the global pool
"""
def convert(self):
message = self.ctx.message
bot = self.ctx.bot
match = self._get_id_match() or re.match(r'<@!?([0-9]+)>$', self.argument)
server = message.server
result = None
if match is None:
# not a mention...
if server:
result = server.get_member_named(self.argument)
if result is None:
result = _get_from_servers(bot, 'get_member_named', self.argument)
else:
user_id = match.group(1)
if server:
result = server.get_member(user_id)
if result is None:
result = _get_from_servers(bot, 'get_member', user_id)
if result is None:
raise BadArgument('User "{}" not found'.format(self.argument))
return result