diff options
| author | 2019-12-20 02:28:34 -0300 | |
|---|---|---|
| committer | 2019-12-20 02:28:34 -0300 | |
| commit | 98096a4ada2d4d27edac24a606c04e74fb4dc45b (patch) | |
| tree | 08927034b89c07794250deddbff33273cc265bc1 | |
| parent | Refactor minor details in `post_infraction` (diff) | |
Give `post_user` default values for `payload` if absent in `user`
Now `post_user(...)` expects either a `discord.User` or a
`discord.Object` object as `user`. Either way, it will try to take the relevant
attributes from `user` to fill the DB columns. If it can't be done,
`.avatar_hash`, `.discriminator`, and `name` will take default values.
| -rw-r--r-- | bot/cogs/moderation/utils.py | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/bot/cogs/moderation/utils.py b/bot/cogs/moderation/utils.py index 1b683b4a3..0674e8d45 100644 --- a/bot/cogs/moderation/utils.py +++ b/bot/cogs/moderation/utils.py @@ -31,38 +31,45 @@ Infraction = t.Dict[str, t.Union[str, int, bool]] Expiry = t.Union[Duration, ISODateTime] -async def post_user(ctx: Context, user: discord.User) -> t.Optional[dict]: +async def post_user(ctx: Context, user: t.Union[discord.User, discord.Object]) -> t.Optional[dict]: """ Create a new user in the database. Used when an infraction needs to be applied on a user absent in the guild. """ - log.warn("Attempting to add user to the database.") + log.trace("Attempting to add user to the database.") - payload = { - 'avatar_hash': user.avatar, - 'discriminator': int(user.discriminator), - 'id': user.id, - 'in_guild': False, - 'name': user.name, - 'roles': [] - } + try: + payload = { + 'avatar_hash': user.avatar, + 'discriminator': int(user.discriminator), + 'id': user.id, + 'in_guild': False, + 'name': user.name, + 'roles': [] + } + except AttributeError: + log.trace("Couldn't take all the attributes for the user payload, taking just its ID.") + # XXX: Not sure if these default values are ideal. + payload = { + 'avatar_hash': 0, + 'discriminator': 0, + 'id': user.id, + 'in_guild': False, + 'name': 'Some name', + 'roles': [] + } try: response = await ctx.bot.api_client.post('bot/users', json=payload) - except ResponseCodeError: + return response + except ResponseCodeError as e: # TODO: Add details, and specific information per possible situation. # Potential race condition if someone joins and the bot syncs before the API replies! log.info("Couldn't post user.") - # TODO: Rewrite message. - await ctx.send("Tried to post user to the DB, couldn't be done.") - - return - - return response - + # NOTE: `e.status` is probably not enough for a good message + await ctx.send(f"The attempt to add the user to the DB failed: {e.status}") -# TODO: maybe delete proxy_user def proxy_user(user_id: str) -> discord.Object: """ |