Skip to content

Commit

Permalink
[p]warn ask to ban when user not in server (#6481)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Oliveira <34169552+Flame442@users.noreply.github.com>
  • Loading branch information
Ascensionn and Flame442 authored Dec 8, 2024
1 parent 33e0eac commit 48b2fe7
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions redbot/cogs/warnings/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit 48b2fe7

Please sign in to comment.