diff --git a/redbot/cogs/warnings/warnings.py b/redbot/cogs/warnings/warnings.py index acf84255485..ba22417a28d 100644 --- a/redbot/cogs/warnings/warnings.py +++ b/redbot/cogs/warnings/warnings.py @@ -15,9 +15,10 @@ ) from redbot.core import Config, commands, modlog from redbot.core.bot import Red -from redbot.core.commands import UserInputOptional +from redbot.core.commands import UserInputOptional, RawUserIdConverter from redbot.core.i18n import Translator, cog_i18n from redbot.core.utils import AsyncIter +from redbot.core.utils.views import ConfirmView from redbot.core.utils.chat_formatting import warning, pagify from redbot.core.utils.menus import menu @@ -373,7 +374,7 @@ async def actionlist(self, ctx: commands.Context): async def warn( self, ctx: commands.Context, - member: discord.Member, + user: Union[discord.Member, RawUserIdConverter], points: UserInputOptional[int] = 1, *, reason: str, @@ -386,6 +387,49 @@ async def warn( or a custom reason if ``[p]warningset allowcustomreasons`` is set. """ guild = ctx.guild + member = None + if isinstance(user, discord.Member): + member = user + elif isinstance(user, int): + if not ctx.channel.permissions_for(ctx.guild.me).ban_members: + await ctx.send(_("User `{user}` is not in the server.").format(user=user)) + return + user_obj = self.bot.get_user(user) or discord.Object(id=user) + + confirm = ConfirmView(ctx.author, timeout=30) + confirm.message = await ctx.send( + _( + "User `{user}` is not in the server. Would you like to ban them instead?" + ).format(user=user), + view=confirm, + ) + await confirm.wait() + if confirm.result: + try: + await ctx.guild.ban(user_obj, reason=reason) + await modlog.create_case( + self.bot, + guild, + ctx.message.created_at, + "hackban", + user, + ctx.author, + reason, + until=None, + channel=None, + ) + except discord.HTTPException as error: + await ctx.send( + _("An error occurred while trying to ban the user. Error: {error}").format( + error=error + ) + ) + else: + confirm.message = await ctx.send(_("No action taken.")) + + await ctx.tick() + return + if member == ctx.author: return await ctx.send(_("You cannot warn yourself.")) if member.bot: