[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
This commit is contained in:
Dav 2020-01-13 23:57:39 +01:00 committed by Michael H
parent 3c53b89040
commit 90c0f76ae4
2 changed files with 40 additions and 24 deletions

View File

@ -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.

View File

@ -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 `<user>` 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)