[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,10 +332,16 @@ 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:
if user_id in tempbans:
# 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( errors[user_id] = _("User {user_id} is already banned.").format(
user_id=user_id user_id=user_id
) )
@ -343,6 +355,10 @@ 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:
if user_id in tempbans:
# We need to check if a user is tempbanned here because otherwise they won't be processed later on.
continue
else:
# Instead of replicating all that handling... gets attr from decorator # Instead of replicating all that handling... gets attr from decorator
try: try:
result = await self.ban_user( result = await self.ban_user(
@ -369,17 +385,30 @@ 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)
async with self.config.guild(guild).current_tempbans() as tempbans:
if user_id in tempbans:
tempbans.remove(user_id)
upgrades.append(str(user_id))
log.info(
"{}({}) upgraded the tempban for {} to a permaban.".format(
author.name, author.id, user_id
)
)
banned.append(user_id)
else:
try: try:
await guild.ban(user, reason=audit_reason, delete_message_days=days) await guild.ban(user, reason=audit_reason, delete_message_days=days)
log.info("{}({}) hackbanned {}".format(author.name, author.id, user_id)) log.info("{}({}) hackbanned {}".format(author.name, author.id, user_id))
except discord.NotFound: except discord.NotFound:
errors[user_id] = _("User {user_id} does not exist.").format(user_id=user_id) errors[user_id] = _("User {user_id} does not exist.").format(
continue
except discord.Forbidden:
errors[user_id] = _("Could not ban {user_id}: missing permissions.").format(
user_id=user_id user_id=user_id
) )
continue continue
except discord.Forbidden:
errors[user_id] = _(
"Could not ban {user_id}: missing permissions."
).format(user_id=user_id)
continue
else: else:
banned.append(user_id) banned.append(user_id)