mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 18:03:12 +00:00
feat: Heavily optimize ban command
This commit is contained in:
parent
629474efc8
commit
4e3ddaee07
4 changed files with 45 additions and 55 deletions
|
@ -226,7 +226,7 @@
|
|||
"lumi_exception_generic": "An error occurred. Please try again later.",
|
||||
"mod_ban_dm": "**{0}** you have been banned from `{1}`.\n\n**Reason:** `{2}`",
|
||||
"mod_banned_author": "User Banned",
|
||||
"mod_banned_user": "user with ID `{0}` has been banned.",
|
||||
"mod_banned_user": "user `{0}` has been banned.",
|
||||
"mod_dm_not_sent": "Failed to notify them in DM",
|
||||
"mod_dm_sent": "notified them in DM",
|
||||
"mod_kick_dm": "**{0}** you have been kicked from `{1}`.\n\n**Reason:** `{2}`",
|
||||
|
@ -245,7 +245,7 @@
|
|||
"mod_timed_out_author": "User Timed Out",
|
||||
"mod_timed_out_user": "user `{0}` has been timed out.",
|
||||
"mod_timeout_dm": "**{0}** you have been timed out in `{1}` for `{2}`.\n\n**Reason:** `{3}`",
|
||||
"mod_unbanned": "user with ID `{0}` has been unbanned.",
|
||||
"mod_unbanned": "user `{0}` has been unbanned.",
|
||||
"mod_unbanned_author": "User Unbanned",
|
||||
"mod_untimed_out": "timeout has been removed for user `{0}`.",
|
||||
"mod_untimed_out_author": "User Timeout Removed",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import contextlib
|
||||
from typing import cast
|
||||
|
||||
import discord
|
||||
|
@ -11,19 +12,26 @@ from lib.const import CONST
|
|||
from ui.embeds import Builder
|
||||
|
||||
|
||||
@commands.has_permissions(ban_members=True)
|
||||
class Ban(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.hybrid_command(name="ban")
|
||||
async def ban(self, ctx: commands.Context[commands.Bot], target: discord.User, *, reason: str | None = None):
|
||||
@commands.has_permissions(ban_members=True)
|
||||
@commands.guild_only()
|
||||
async def ban(
|
||||
self,
|
||||
ctx: commands.Context[commands.Bot],
|
||||
target: discord.Member | discord.User,
|
||||
*,
|
||||
reason: str | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Ban a user from the guild.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
target: discord.User
|
||||
target: discord.Member | discord.User
|
||||
The user to ban.
|
||||
reason: str | None
|
||||
The reason for the ban.
|
||||
|
@ -32,20 +40,22 @@ class Ban(commands.Cog):
|
|||
assert ctx.author
|
||||
assert ctx.bot.user
|
||||
|
||||
# see if user is in guild
|
||||
member = await commands.MemberConverter().convert(ctx, str(target.id))
|
||||
output_reason = reason or CONST.STRINGS["mod_no_reason"]
|
||||
formatted_reason = CONST.STRINGS["mod_reason"].format(
|
||||
ctx.author.name,
|
||||
lib.format.shorten(output_reason, 200),
|
||||
)
|
||||
|
||||
# member -> user is in the guild, check role hierarchy
|
||||
if member:
|
||||
dm_sent = False
|
||||
if isinstance(target, discord.Member):
|
||||
bot_member = await commands.MemberConverter().convert(ctx, str(ctx.bot.user.id))
|
||||
await async_actionable(member, cast(discord.Member, ctx.author), bot_member)
|
||||
await async_actionable(target, cast(discord.Member, ctx.author), bot_member)
|
||||
|
||||
try:
|
||||
await member.send(
|
||||
with contextlib.suppress(discord.HTTPException, discord.Forbidden):
|
||||
await target.send(
|
||||
embed=Builder.create_embed(
|
||||
theme="warning",
|
||||
user_name=member.name,
|
||||
user_name=target.name,
|
||||
author_text=CONST.STRINGS["mod_banned_author"],
|
||||
description=CONST.STRINGS["mod_ban_dm"].format(
|
||||
target.name,
|
||||
|
@ -57,51 +67,26 @@ class Ban(commands.Cog):
|
|||
)
|
||||
dm_sent = True
|
||||
|
||||
except (discord.HTTPException, discord.Forbidden):
|
||||
dm_sent = False
|
||||
await ctx.guild.ban(target, reason=formatted_reason)
|
||||
|
||||
await member.ban(
|
||||
reason=CONST.STRINGS["mod_reason"].format(
|
||||
ctx.author.name,
|
||||
lib.format.shorten(output_reason, 200),
|
||||
),
|
||||
delete_message_seconds=86400,
|
||||
)
|
||||
embed = Builder.create_embed(
|
||||
theme="success",
|
||||
user_name=ctx.author.name,
|
||||
author_text=CONST.STRINGS["mod_banned_author"],
|
||||
description=CONST.STRINGS["mod_banned_user"].format(target.name),
|
||||
)
|
||||
if isinstance(target, discord.Member):
|
||||
embed.set_footer(text=CONST.STRINGS["mod_dm_sent"] if dm_sent else CONST.STRINGS["mod_dm_not_sent"])
|
||||
|
||||
respond_task = ctx.send(
|
||||
embed=Builder.create_embed(
|
||||
theme="success",
|
||||
user_name=ctx.author.name,
|
||||
author_text=CONST.STRINGS["mod_banned_author"],
|
||||
description=CONST.STRINGS["mod_banned_user"].format(target.id),
|
||||
footer_text=CONST.STRINGS["mod_dm_sent"] if dm_sent else CONST.STRINGS["mod_dm_not_sent"],
|
||||
),
|
||||
)
|
||||
create_case_task = create_case(ctx, target, "BAN", reason)
|
||||
await asyncio.gather(respond_task, create_case_task, return_exceptions=True)
|
||||
|
||||
# not a member in this guild, so ban right away
|
||||
else:
|
||||
await ctx.guild.ban(
|
||||
target,
|
||||
reason=CONST.STRINGS["mod_reason"].format(
|
||||
ctx.author.name,
|
||||
lib.format.shorten(output_reason, 200),
|
||||
),
|
||||
)
|
||||
|
||||
respond_task = ctx.send(
|
||||
embed=Builder.create_embed(
|
||||
theme="success",
|
||||
user_name=ctx.author.name,
|
||||
author_text=CONST.STRINGS["mod_banned_author"],
|
||||
description=CONST.STRINGS["mod_banned_user"].format(target.id),
|
||||
),
|
||||
)
|
||||
create_case_task = create_case(ctx, target, "BAN", reason)
|
||||
await asyncio.gather(respond_task, create_case_task)
|
||||
await asyncio.gather(
|
||||
ctx.send(embed=embed),
|
||||
create_case(ctx, cast(discord.User, target), "BAN", reason),
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
@commands.hybrid_command(name="unban")
|
||||
@commands.has_permissions(ban_members=True)
|
||||
@commands.guild_only()
|
||||
async def unban(
|
||||
self,
|
||||
ctx: commands.Context[commands.Bot],
|
||||
|
@ -139,7 +124,7 @@ class Ban(commands.Cog):
|
|||
theme="success",
|
||||
user_name=ctx.author.name,
|
||||
author_text=CONST.STRINGS["mod_unbanned_author"],
|
||||
description=CONST.STRINGS["mod_unbanned"].format(target.id),
|
||||
description=CONST.STRINGS["mod_unbanned"].format(target.name),
|
||||
),
|
||||
)
|
||||
create_case_task = create_case(ctx, target, "UNBAN", reason)
|
||||
|
|
|
@ -50,6 +50,7 @@ class Cases(commands.Cog):
|
|||
|
||||
@commands.hybrid_command(name="case", aliases=["c", "ca"], description="View a specific case by number")
|
||||
@commands.has_permissions(manage_messages=True)
|
||||
@commands.guild_only()
|
||||
async def view_case_by_number(self, ctx: commands.Context[commands.Bot], case_number: int) -> None:
|
||||
guild_id = ctx.guild.id if ctx.guild else 0
|
||||
case = case_service.fetch_case_by_guild_and_number(guild_id, case_number)
|
||||
|
@ -77,6 +78,7 @@ class Cases(commands.Cog):
|
|||
|
||||
@commands.hybrid_command(name="cases", description="View all cases in the guild")
|
||||
@commands.has_permissions(manage_messages=True)
|
||||
@commands.guild_only()
|
||||
async def view_all_cases_in_guild(self, ctx: commands.Context[commands.Bot]) -> None:
|
||||
if not ctx.guild:
|
||||
raise LumiException(CONST.STRINGS["error_not_in_guild"])
|
||||
|
@ -108,6 +110,7 @@ class Cases(commands.Cog):
|
|||
|
||||
@commands.hybrid_command(name="modcases", aliases=["mc", "modc"], description="View all cases in the guild")
|
||||
@commands.has_permissions(manage_messages=True)
|
||||
@commands.guild_only()
|
||||
async def view_all_cases_by_mod(self, ctx: commands.Context[commands.Bot], moderator: discord.Member) -> None:
|
||||
if not ctx.guild:
|
||||
raise LumiException(CONST.STRINGS["error_not_in_guild"])
|
||||
|
@ -139,6 +142,7 @@ class Cases(commands.Cog):
|
|||
|
||||
@commands.hybrid_command(name="editcase", aliases=["ec"], description="Edit the reason for a case")
|
||||
@commands.has_permissions(manage_messages=True)
|
||||
@commands.guild_only()
|
||||
async def edit_case_reason(self, ctx: commands.Context[commands.Bot], case_number: int, *, new_reason: str):
|
||||
if not ctx.guild:
|
||||
raise LumiException(CONST.STRINGS["error_not_in_guild"])
|
||||
|
|
|
@ -17,6 +17,7 @@ class Warn(commands.Cog):
|
|||
|
||||
@commands.hybrid_command(name="warn", description="Warn a user")
|
||||
@commands.has_permissions(manage_messages=True)
|
||||
@commands.guild_only()
|
||||
async def warn(self, ctx: commands.Context[commands.Bot], target: discord.Member, *, reason: str | None = None):
|
||||
if not ctx.guild or not ctx.author or not ctx.bot.user:
|
||||
raise LumiException
|
||||
|
|
Loading…
Reference in a new issue