diff options
| author | 2019-12-16 01:59:50 -0300 | |
|---|---|---|
| committer | 2019-12-16 01:59:50 -0300 | |
| commit | 2798e2db4e37f538b94bd84fee473d4b31504f17 (patch) | |
| tree | 1f735b8a34991e30dfe8f2f71a00bf40b4e125ca | |
| parent | Remove pointless comma after last argument (diff) | |
Add `post_user` function to POST a new user to the DB
As it is now, this function is planned to be used a big-helper in
`post_infraction`. Its interface is partially similar: it will return
a "JSON" dictionary if everything went well, or `None` if it
failed. If it fails, it will send a message to the channel and
register the issue in the `log`.
| -rw-r--r-- | bot/cogs/moderation/utils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/bot/cogs/moderation/utils.py b/bot/cogs/moderation/utils.py index 044c6f8d1..81a975ba4 100644 --- a/bot/cogs/moderation/utils.py +++ b/bot/cogs/moderation/utils.py @@ -31,6 +31,39 @@ 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]: + """ + 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.") + + payload = { + 'avatar_hash': user.avatar, + 'discriminator': int(user.discriminator), + 'id': user.id, + 'in_guild': False, + 'name': user.name, + 'roles': [] + } + + try: + response = await ctx.bot.api_client.post('bot/users', json=payload) + except ResponseCodeError: + # 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 + + +# TODO: maybe delete proxy_user + def proxy_user(user_id: str) -> discord.Object: """ Create a proxy user object from the given id. |