From 90c0f76ae4fced4f0feae02953171ef6ba8fc0ef Mon Sep 17 00:00:00 2001 From: Dav <57032623+Dav-Git@users.noreply.github.com> Date: Mon, 13 Jan 2020 23:57:39 +0100 Subject: [PATCH] [Warnings] Make [p]warnings usable on base of permissions (#3327) * new code Added the admin check to warnings and removed the part where the user could check themselves. Added new mywarns which replaces part of the old behaviour of warn * Update warnings.py * Create 2900.enhance.rst * Fixed command name Because appearently I can't remember a command for 10 seconds * Commands in backticks Put command names in changelog in double backticks after being advised to do so in discord * made user not optional, and the other thing sinbad requested * switched parts. magic resolves #2900 --- changelog.d/warnings/2900.enhance.rst | 2 + redbot/cogs/warnings/warnings.py | 62 ++++++++++++++++----------- 2 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 changelog.d/warnings/2900.enhance.rst diff --git a/changelog.d/warnings/2900.enhance.rst b/changelog.d/warnings/2900.enhance.rst new file mode 100644 index 000000000..1c6f7599c --- /dev/null +++ b/changelog.d/warnings/2900.enhance.rst @@ -0,0 +1,2 @@ +``[p]warnings`` can now be used by users that have the permission to use it from the Permissions cog, in order to check another user's warnings. +``[p]mywarnings`` can now be used by any user (instead of ``[p]warnings`` previously) to check their own warnings. diff --git a/redbot/cogs/warnings/warnings.py b/redbot/cogs/warnings/warnings.py index 515c1c13c..458f43ff9 100644 --- a/redbot/cogs/warnings/warnings.py +++ b/redbot/cogs/warnings/warnings.py @@ -12,7 +12,6 @@ from redbot.cogs.warnings.helpers import ( from redbot.core import Config, checks, commands, modlog from redbot.core.bot import Red from redbot.core.i18n import Translator, cog_i18n -from redbot.core.utils.mod import is_admin_or_superior from redbot.core.utils.chat_formatting import warning, pagify from redbot.core.utils.menus import menu, DEFAULT_CONTROLS @@ -342,30 +341,16 @@ class Warnings(commands.Cog): @commands.command() @commands.guild_only() - async def warnings( - self, ctx: commands.Context, user: Optional[Union[discord.Member, int]] = None - ): - """List the warnings for the specified user. + @checks.admin() + async def warnings(self, ctx: commands.Context, user: Union[discord.Member, int]): + """List the warnings for the specified user.""" - Omit `` to see your own warnings. - - Note that showing warnings for users other than yourself requires - appropriate permissions. - """ - if user is None: - user = ctx.author - else: - if not await is_admin_or_superior(self.bot, ctx.author): - return await ctx.send( - warning(_("You are not allowed to check warnings for other users!")) - ) - - try: - userid: int = user.id - except AttributeError: - userid: int = user - user = ctx.guild.get_member(userid) - user = user or namedtuple("Member", "id guild")(userid, ctx.guild) + try: + userid: int = user.id + except AttributeError: + userid: int = user + user = ctx.guild.get_member(userid) + user = user or namedtuple("Member", "id guild")(userid, ctx.guild) msg = "" member_settings = self.config.member(user) @@ -389,6 +374,35 @@ class Warnings(commands.Cog): pagify(msg, shorten_by=58), box_lang=_("Warnings for {user}").format(user=user) ) + @commands.command() + @commands.guild_only() + async def mywarnings(self, ctx: commands.Context): + """List warnings for yourself.""" + + user = ctx.author + + msg = "" + member_settings = self.config.member(user) + async with member_settings.warnings() as user_warnings: + if not user_warnings.keys(): # no warnings for the user + await ctx.send(_("You have no warnings!")) + else: + for key in user_warnings.keys(): + mod_id = user_warnings[key]["mod"] + mod = ctx.bot.get_user(mod_id) or _("Unknown Moderator ({})").format(mod_id) + msg += _( + "{num_points} point warning {reason_name} issued by {user} for " + "{description}\n" + ).format( + num_points=user_warnings[key]["points"], + reason_name=key, + user=mod, + description=user_warnings[key]["description"], + ) + await ctx.send_interactive( + pagify(msg, shorten_by=58), box_lang=_("Warnings for {user}").format(user=user) + ) + @commands.command() @commands.guild_only() @checks.admin_or_permissions(ban_members=True)