1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 18:23:12 +00:00

chore: Refactor actionable function to use LumiException for error handling

This commit is contained in:
wlinator 2024-08-02 05:45:47 -04:00
parent a2733db532
commit 54449e0af2
6 changed files with 20 additions and 27 deletions

View file

@ -50,6 +50,9 @@
"error_private_message_only_description": "this command can only be used in private messages.",
"error_unknown_error_author": "Unknown Error",
"error_unknown_error_description": "an unknown error occurred. Please try again later.",
"error_actionable_self": "you can't perform this action on yourself.",
"error_actionable_hierarchy_user": "you don't have permission to perform this action on this user due to role hierarchy.",
"error_actionable_hierarchy_bot": "I don't have permission to perform this action on this user due to role hierarchy.",
"help_use_prefix": "Please use Lumi's prefix to get help. Type `{0}help`",
"info_api_version": "**API:** v{0}\n",
"info_database_records": "**Database:** {0} records",

View file

@ -106,14 +106,10 @@ class ErrorListener(Cog):
f"{ctx.author.name} executed {command_type}{ctx.command.qualified_name}"
)
if ctx.guild is not None:
log_msg += f" | guild: {ctx.guild.name} "
else:
log_msg += " in DMs"
if len(str(error)) > 80:
error = str(error)[:80] + "..."
logger.warning(f"{log_msg} | FAILED: {error}")
log_msg += " in DMs" if ctx.guild is None else f" | guild: {ctx.guild.name} "
# if len(str(error)) > 80:
# error = f"{str(error)[:80]}..."
logger.exception(f"{log_msg} | FAILED: {error}")
@Cog.listener()
async def on_command_error(self, ctx, error) -> None:

View file

@ -1,4 +1,3 @@
import discord
from discord.ext import commands
@ -27,11 +26,3 @@ class LumiException(commands.CommandError):
def __init__(self, message="An error occurred."):
self.message = message
super().__init__(message)
class UserHierarchy(discord.DiscordException):
pass
class BotHierarchy(discord.DiscordException):
pass

View file

@ -18,7 +18,7 @@ async def ban_user(cog, ctx, target: discord.User, reason: Optional[str] = None)
# member -> user is in the guild, check role hierarchy
if member:
bot_member = await cog.client.get_or_fetch_member(ctx.guild, ctx.bot.user.id)
actionable.actionable(member, ctx.author, bot_member)
await actionable.actionable(member, ctx.author, bot_member)
try:
await member.send(

View file

@ -1,10 +1,9 @@
import discord
from discord.ext.commands import BadArgument
from lib.exceptions import LumiExceptions
from lib.exceptions.LumiExceptions import LumiException
from lib.constants import CONST
def actionable(
async def actionable(
target: discord.Member,
invoker: discord.Member,
bot_user: discord.Member,
@ -21,10 +20,10 @@ def actionable(
True if the client's highest role AND the invoker's highest role are higher than the target.
"""
if target == invoker:
raise BadArgument("you can't ban yourself.")
raise LumiException(CONST.STRINGS["error_actionable_self"])
if target.top_role >= invoker.top_role and invoker != invoker.guild.owner:
raise LumiExceptions.UserHierarchy
raise LumiException(CONST.STRINGS["error_actionable_hierarchy_user"])
elif target.top_role >= bot_user.top_role:
raise LumiExceptions.BotHierarchy
if target.top_role >= bot_user.top_role:
raise LumiException(CONST.STRINGS["error_actionable_hierarchy_bot"])

View file

@ -1,13 +1,17 @@
import discord
from typing import Optional
from discord.ext.commands import UserConverter
from discord.ext.commands import UserConverter, MemberConverter
import asyncio
from lib.embed_builder import EmbedBuilder
from lib.constants import CONST
from modules.moderation.utils.case_handler import create_case
from modules.moderation.utils.actionable import actionable
async def warn_user(ctx, target: discord.Member, reason: Optional[str]):
bot_member = await MemberConverter().convert(ctx, str(ctx.bot.user.id))
await actionable(target, ctx.author, bot_member)
output_reason = reason or CONST.STRINGS["mod_no_reason"]
dm_task = target.send(