Skip to content
Permalink
Browse files

[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
  • Loading branch information
Dav-Git authored and mikeshardmind committed Jan 13, 2020
1 parent 3c53b89 commit 90c0f76ae4fced4f0feae02953171ef6ba8fc0ef
Showing with 38 additions and 22 deletions.
  1. +2 −0 changelog.d/warnings/2900.enhance.rst
  2. +36 −22 redbot/cogs/warnings/warnings.py
@@ -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.
@@ -12,7 +12,6 @@
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,36 +341,51 @@ def __init__(self, bot: Red):

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

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!"))
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(_("That user has 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)
)

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)
@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(_("That user has no warnings!"))
await ctx.send(_("You have no warnings!"))
else:
for key in user_warnings.keys():
mod_id = user_warnings[key]["mod"]

0 comments on commit 90c0f76

Please sign in to comment.
You can’t perform that action at this time.