errbotio / err-backend-discord

Backend for Discord for Errbot
GNU General Public License v3.0
23 stars 22 forks source link

Errbot tries to process webhooks as if they are users, can't find a username, and raises ValueError. #67

Closed greenaar closed 4 months ago

greenaar commented 4 months ago

This took me the better part of an afternoon to identify, mostly because I thought it was something else. (I thought it was related to actions I was taking as a user).

ValueError: Failed to get the user 1231072357033185361

This occurs when curl webhook posts into a discord channel Errbot is monitoring.

In this case, fail2ban, as shown here:

actionstop = curl -X POST "" \ -H "Content-Type: application/json" \ -d '{"username": "Fail2Ban", "content":":no_entry: The [] jail has been stopped"}'

(with or without "username":"Fail2Ban", doesn't seem to matter)

It tries to use on_message() against it (from err-backend-discord.py) -- the first thing it does is

err_msg.frm = DiscordRoomOccupant(msg.author.id, msg.channel.id)

which immediately does

super().init(user_id)

And fails here:

    self.discord_user = DiscordPerson.client.get_user(self._user_id)
    if self.discord_user is None:
        raise ValueError(f"Failed to get the user {self._user_id}")

Unsurprisingly, because it's not a real user, so you can't reference a username. discord_user will always return 'None' in this case.

My workaround was to switch the raise in room.py (~line 79) to:

log.info("Ignoring message from id %s, username is %s, which means it is likely a webhook.", self._user_id, username)

and to add this around line 100 of err-backend-discord.py

    if msg.author.bot:
        return

I.e. this is not a person, so do not continue to process them.

I'm not convinced this is anything like an ideal result, but in the short term it silences this error. I'm open to alternatives, and I'd happily try my hand at writing a patch that is better than this hack.

Hopefully this issue helps someone else who sees this in their logs.

nzlosh commented 4 months ago

Can you create a PR for this please?

greenaar commented 4 months ago

https://github.com/errbotio/err-backend-discord/pull/68 here you go. I'm using this successfully - though I'll reiterate, I'm not entirely sure it's the correct path. It's a simple patch at least.

nzlosh commented 4 months ago

Thanks for your contribution. If others encounter a problem with the patch, they're most welcome to provide fixes.