dronefly-garden / dronefly

Red Discord Bot V3 cogs for naturalists.
Other
16 stars 3 forks source link

ebird hybrids: handle howMany = X #72

Closed synrg closed 4 years ago

synrg commented 4 years ago

When a species count is indicated as 'X', howMany is not set in any transactions returned by the API. This caused `ebird hybrids CA-NS 7' to fail yesterday when the command attempted to format the output assuming howMany was set.

Exception in command 'ebird hybrids'
Traceback (most recent call last):
  File "/home/synrg/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/synrg/.local/share/Appledore/cogs/CogManager/cogs/ebirdcog/ebirdcog.py", line 133, in hybrids
    value=("· {obsDt}: {howMany} at {locName}").format_map(rec),
  File "/home/synrg/.local/share/Appledore/cogs/CogManager/cogs/ebirdcog/ebirdcog.py", line 26, in __getitem__
    val = super().__getitem__(key)
KeyError: 'howMany'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/synrg/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "/home/synrg/.local/lib/python3.8/site-packages/redbot/core/commands/commands.py", line 631, in invoke
    await super().invoke(ctx)
  File "/home/synrg/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1158, in invoke
    await ctx.invoked_subcommand.invoke(ctx)
  File "/home/synrg/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/synrg/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'howMany'

A quick fix would be to set rec["howMany"] = "uncounted" prior to formatting the string.

A more thorough approach would be to wrap the API results in dataclasses-json as we do for inatcog so that each record always has appropriate values even when not returned by the API call.

synrg commented 4 years ago

a959f3c74c8051d0f68e55bb811326c4f2a2b723 should fix this issue.