Skip to content

Commit

Permalink
Merge branch 'fix/User-Not-Found' into 'master'
Browse files Browse the repository at this point in the history
[Fix] Fix an Error if a User Doesn't Exist

See merge request hull-seals/code/irc/halpybot!156
  • Loading branch information
Rixxan committed Nov 28, 2022
2 parents 3d9f239 + 07e0cfb commit 1826c14
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions halpybot/commands/caseutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import List
from ..packages.command import Commands
from ..packages.facts import Facts
from ..packages.models import Context, User
from ..packages.models import Context, User, NoUserFound


@Commands.command("go")
Expand All @@ -33,7 +33,7 @@ async def cmd_go(ctx: Context, args: List[str]):
try:
whois = await User.get_info(ctx.bot, str(seal))
vhost = User.process_vhost(whois.hostname)
except AttributeError:
except (AttributeError, NoUserFound):
vhost = "notUser"
# There is no hard set "not a seal" vhost level.
if vhost is None:
Expand Down
12 changes: 10 additions & 2 deletions halpybot/packages/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
"""

from .edsm_classes import Coordinates, Location, SystemInfo
from .user import User
from .user import User, UserError, NoUserFound
from .context import Context

__all__ = ["Context", "User", "Coordinates", "Location", "SystemInfo"]
__all__ = [
"Context",
"User",
"Coordinates",
"Location",
"SystemInfo",
"UserError",
"NoUserFound",
]
17 changes: 16 additions & 1 deletion halpybot/packages/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@
from typing import Optional, Set
from attr import dataclass
import pydle

import cattr


class UserError(Exception):
"""
Base class for User Class errors
"""


class NoUserFound(UserError):
"""
An exception occurred while sending a Discord Webhook
"""


@dataclass(frozen=True)
class User:
"""IRC User info
Expand Down Expand Up @@ -66,7 +77,11 @@ async def get_info(cls, bot: pydle.Client, nickname: str) -> Optional[User]:
"""
# fetch the user object from pydle
if nickname.endswith(",") or nickname.endswith(":"):
nickname = nickname[:-1]
data = await bot.whois(nickname)
if data is None:
raise NoUserFound
if "nickname" not in data:
data["nickname"] = nickname
return cattr.structure(data, Optional[User])
Expand Down

0 comments on commit 1826c14

Please sign in to comment.