mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[Core] Target any user with blacklist / whitelist commands (#1068)
This commit is contained in:
parent
4f61daf51a
commit
58d669d07e
@ -1,6 +1,7 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from cogs.utils import checks
|
from cogs.utils import checks
|
||||||
|
from cogs.utils.converters import GlobalUser
|
||||||
from __main__ import set_cog
|
from __main__ import set_cog
|
||||||
from .utils.dataIO import dataIO
|
from .utils.dataIO import dataIO
|
||||||
from .utils.chat_formatting import pagify, box
|
from .utils.chat_formatting import pagify, box
|
||||||
@ -535,7 +536,7 @@ class Owner:
|
|||||||
await self.bot.send_cmd_help(ctx)
|
await self.bot.send_cmd_help(ctx)
|
||||||
|
|
||||||
@blacklist.command(name="add")
|
@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"""
|
"""Adds user to Red's global blacklist"""
|
||||||
if user.id not in self.global_ignores["blacklist"]:
|
if user.id not in self.global_ignores["blacklist"]:
|
||||||
self.global_ignores["blacklist"].append(user.id)
|
self.global_ignores["blacklist"].append(user.id)
|
||||||
@ -545,7 +546,7 @@ class Owner:
|
|||||||
await self.bot.say("User is already blacklisted.")
|
await self.bot.say("User is already blacklisted.")
|
||||||
|
|
||||||
@blacklist.command(name="remove")
|
@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"""
|
"""Removes user from Red's global blacklist"""
|
||||||
if user.id in self.global_ignores["blacklist"]:
|
if user.id in self.global_ignores["blacklist"]:
|
||||||
self.global_ignores["blacklist"].remove(user.id)
|
self.global_ignores["blacklist"].remove(user.id)
|
||||||
@ -583,7 +584,7 @@ class Owner:
|
|||||||
await self.bot.send_cmd_help(ctx)
|
await self.bot.send_cmd_help(ctx)
|
||||||
|
|
||||||
@whitelist.command(name="add")
|
@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"""
|
"""Adds user to Red's global whitelist"""
|
||||||
if user.id not in self.global_ignores["whitelist"]:
|
if user.id not in self.global_ignores["whitelist"]:
|
||||||
if not self.global_ignores["whitelist"]:
|
if not self.global_ignores["whitelist"]:
|
||||||
@ -597,7 +598,7 @@ class Owner:
|
|||||||
await self.bot.say("User is already whitelisted.")
|
await self.bot.say("User is already whitelisted.")
|
||||||
|
|
||||||
@whitelist.command(name="remove")
|
@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"""
|
"""Removes user from Red's global whitelist"""
|
||||||
if user.id in self.global_ignores["whitelist"]:
|
if user.id in self.global_ignores["whitelist"]:
|
||||||
self.global_ignores["whitelist"].remove(user.id)
|
self.global_ignores["whitelist"].remove(user.id)
|
||||||
@ -927,7 +928,7 @@ class Owner:
|
|||||||
for user_id in _list:
|
for user_id in _list:
|
||||||
user = discord.utils.get(self.bot.get_all_members(), id=user_id)
|
user = discord.utils.get(self.bot.get_all_members(), id=user_id)
|
||||||
if user:
|
if user:
|
||||||
users.append(str(user))
|
users.append("{} ({})".format(user, user.id))
|
||||||
|
|
||||||
if users:
|
if users:
|
||||||
not_found = total - len(users)
|
not_found = total - len(users)
|
||||||
|
|||||||
46
cogs/utils/converters.py
Normal file
46
cogs/utils/converters.py
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user