aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Manuel Ignacio Pérez Alcolea <[email protected]>2019-12-16 02:11:55 -0300
committerGravatar Manuel Ignacio Pérez Alcolea <[email protected]>2019-12-16 02:11:55 -0300
commit447de16b5c62a6d473abb4bf1c61b34f8d5e0f26 (patch)
tree89cb0ebad2d1d0c8ee932cc21dbd27a3386bd242
parentAdd `post_user` function to POST a new user to the DB (diff)
Make post_infraction try to `post_user` if user doesn't exist
Try twice to apply the infraction. If the user is not in the database, try to add it, then try to apply the infraction again. This allows any moderation function that uses `FetchedUser` as a converter to apply the infraction even when the user is absent in the local database.
-rw-r--r--bot/cogs/moderation/infractions.py4
-rw-r--r--bot/cogs/moderation/utils.py33
2 files changed, 17 insertions, 20 deletions
diff --git a/bot/cogs/moderation/infractions.py b/bot/cogs/moderation/infractions.py
index 3536a3d38..253a8db5b 100644
--- a/bot/cogs/moderation/infractions.py
+++ b/bot/cogs/moderation/infractions.py
@@ -13,12 +13,10 @@ from bot.decorators import respect_role_hierarchy
from bot.utils.checks import with_role_check
from . import utils
from .scheduler import InfractionScheduler
-from .utils import MemberObject
+from .utils import MemberObject, UserTypes as MemberConverter
log = logging.getLogger(__name__)
-MemberConverter = t.Union[utils.UserTypes, utils.proxy_user]
-
class Infractions(InfractionScheduler, commands.Cog):
"""Apply and pardon infractions on users for moderation purposes."""
diff --git a/bot/cogs/moderation/utils.py b/bot/cogs/moderation/utils.py
index 81a975ba4..994bdeaa3 100644
--- a/bot/cogs/moderation/utils.py
+++ b/bot/cogs/moderation/utils.py
@@ -9,7 +9,7 @@ from discord.ext.commands import Context
from bot.api import ResponseCodeError
from bot.constants import Colours, Icons
-from bot.converters import Duration, ISODateTime
+from bot.converters import Duration, FetchedUser, ISODateTime
log = logging.getLogger(__name__)
@@ -25,7 +25,7 @@ INFRACTION_ICONS = {
RULES_URL = "https://pythondiscord.com/pages/rules"
APPEALABLE_INFRACTIONS = ("ban", "mute")
-UserTypes = t.Union[discord.Member, discord.User]
+UserTypes = t.Union[discord.Member, discord.User, FetchedUser]
MemberObject = t.Union[UserTypes, discord.Object]
Infraction = t.Dict[str, t.Union[str, int, bool]]
Expiry = t.Union[Duration, ISODateTime]
@@ -107,22 +107,21 @@ async def post_infraction(
if expires_at:
payload['expires_at'] = expires_at.isoformat()
- try:
- response = await ctx.bot.api_client.post('bot/infractions', json=payload)
- except ResponseCodeError as exp:
- if exp.status == 400 and 'user' in exp.response_json:
- log.info(
- f"{ctx.author} tried to add a {infr_type} infraction to `{user.id}`, "
- "but that user id was not found in the database."
- )
- await ctx.send(
- f":x: Cannot add infraction, the specified user is not known to the database."
- )
- return
+ # Try to apply the infraction. If it fails because the user doesn't exist, try to add it.
+ for attempt in range(2):
+ try:
+ response = await ctx.bot.api_client.post('bot/infractions', json=payload)
+ except ResponseCodeError as exp:
+ if exp.status == 400 and 'user'in exp.response_json:
+ # Only once attempt to try to add the user to the database, not two:
+ if attempt > 0 or await post_user(ctx, user) is None:
+ return
+ else:
+ log.exception("An unexpected ResponseCodeError occurred while adding an infraction:")
+ await ctx.send(":x: There was an error adding the infraction.")
+ return
else:
- log.exception("An unexpected ResponseCodeError occurred while adding an infraction:")
- await ctx.send(":x: There was an error adding the infraction.")
- return
+ break
return response