[Mod] Make tempbans permanent when using [p]hackban (#4025)

* Remove users from tempban unban list when hackbanning them

* black and missing bracket

* make sure this actually gets processed

* let the user know when a tempban was upgraded

* say more things

* reduce config calls

* jack loves performance

* adress review

* review the 2nd
This commit is contained in:
Dav 2020-07-07 23:15:42 +00:00 committed by GitHub
parent 5b612b8ac7
commit 49b19450fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ from typing import Optional, Union
import discord import discord
from redbot.core import commands, i18n, checks, modlog from redbot.core import commands, i18n, checks, modlog
from redbot.core.utils import AsyncIter from redbot.core.utils import AsyncIter
from redbot.core.utils.chat_formatting import pagify, humanize_number, bold from redbot.core.utils.chat_formatting import pagify, humanize_number, bold, humanize_list
from redbot.core.utils.mod import is_allowed_by_hierarchy, get_audit_reason from redbot.core.utils.mod import is_allowed_by_hierarchy, get_audit_reason
from .abc import MixinMeta from .abc import MixinMeta
from .converters import RawUserIds from .converters import RawUserIds
@ -292,6 +292,7 @@ class KickBanMixin(MixinMeta):
using this command""" using this command"""
banned = [] banned = []
errors = {} errors = {}
upgrades = []
async def show_results(): async def show_results():
text = _("Banned {num} users from the server.").format( text = _("Banned {num} users from the server.").format(
@ -300,6 +301,11 @@ class KickBanMixin(MixinMeta):
if errors: if errors:
text += _("\nErrors:\n") text += _("\nErrors:\n")
text += "\n".join(errors.values()) text += "\n".join(errors.values())
if upgrades:
text += _(
"\nFollowing user IDs have been upgraded from a temporary to a permanent ban:\n"
)
text += humanize_list(upgrades)
for p in pagify(text): for p in pagify(text):
await ctx.send(p) await ctx.send(p)
@ -326,13 +332,19 @@ class KickBanMixin(MixinMeta):
if not guild.me.guild_permissions.ban_members: if not guild.me.guild_permissions.ban_members:
return await ctx.send(_("I lack the permissions to do this.")) return await ctx.send(_("I lack the permissions to do this."))
tempbans = await self.config.guild(guild).current_tempbans()
ban_list = await guild.bans() ban_list = await guild.bans()
for entry in ban_list: for entry in ban_list:
for user_id in user_ids: for user_id in user_ids:
if entry.user.id == user_id: if entry.user.id == user_id:
errors[user_id] = _("User {user_id} is already banned.").format( if user_id in tempbans:
user_id=user_id # We need to check if a user is tempbanned here because otherwise they won't be processed later on.
) continue
else:
errors[user_id] = _("User {user_id} is already banned.").format(
user_id=user_id
)
user_ids = remove_processed(user_ids) user_ids = remove_processed(user_ids)
@ -343,21 +355,25 @@ class KickBanMixin(MixinMeta):
for user_id in user_ids: for user_id in user_ids:
user = guild.get_member(user_id) user = guild.get_member(user_id)
if user is not None: if user is not None:
# Instead of replicating all that handling... gets attr from decorator if user_id in tempbans:
try: # We need to check if a user is tempbanned here because otherwise they won't be processed later on.
result = await self.ban_user( continue
user=user, ctx=ctx, days=days, reason=reason, create_modlog_case=True else:
) # Instead of replicating all that handling... gets attr from decorator
if result is True: try:
banned.append(user_id) result = await self.ban_user(
else: user=user, ctx=ctx, days=days, reason=reason, create_modlog_case=True
errors[user_id] = _("Failed to ban user {user_id}: {reason}").format( )
user_id=user_id, reason=result if result is True:
banned.append(user_id)
else:
errors[user_id] = _("Failed to ban user {user_id}: {reason}").format(
user_id=user_id, reason=result
)
except Exception as e:
errors[user_id] = _("Failed to ban user {user_id}: {reason}").format(
user_id=user_id, reason=e
) )
except Exception as e:
errors[user_id] = _("Failed to ban user {user_id}: {reason}").format(
user_id=user_id, reason=e
)
user_ids = remove_processed(user_ids) user_ids = remove_processed(user_ids)
@ -369,19 +385,32 @@ class KickBanMixin(MixinMeta):
user = discord.Object(id=user_id) user = discord.Object(id=user_id)
audit_reason = get_audit_reason(author, reason) audit_reason = get_audit_reason(author, reason)
queue_entry = (guild.id, user_id) queue_entry = (guild.id, user_id)
try: async with self.config.guild(guild).current_tempbans() as tempbans:
await guild.ban(user, reason=audit_reason, delete_message_days=days) if user_id in tempbans:
log.info("{}({}) hackbanned {}".format(author.name, author.id, user_id)) tempbans.remove(user_id)
except discord.NotFound: upgrades.append(str(user_id))
errors[user_id] = _("User {user_id} does not exist.").format(user_id=user_id) log.info(
continue "{}({}) upgraded the tempban for {} to a permaban.".format(
except discord.Forbidden: author.name, author.id, user_id
errors[user_id] = _("Could not ban {user_id}: missing permissions.").format( )
user_id=user_id )
) banned.append(user_id)
continue else:
else: try:
banned.append(user_id) await guild.ban(user, reason=audit_reason, delete_message_days=days)
log.info("{}({}) hackbanned {}".format(author.name, author.id, user_id))
except discord.NotFound:
errors[user_id] = _("User {user_id} does not exist.").format(
user_id=user_id
)
continue
except discord.Forbidden:
errors[user_id] = _(
"Could not ban {user_id}: missing permissions."
).format(user_id=user_id)
continue
else:
banned.append(user_id)
try: try:
await modlog.create_case( await modlog.create_case(