Skip to content

Commit

Permalink
Lookup place abbrev for hub server if set.
Browse files Browse the repository at this point in the history
  • Loading branch information
synrg committed Aug 3, 2024
1 parent d83540e commit 39f4258
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions inatcog/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
"""Module for constants."""
COG_NAME = "iNat"
# ToDo: make this configurable:
# - currently only contains iNaturalist and Dronefly server ids
HUB_SERVERS = [525711945270296587, 615263302485803019]
20 changes: 15 additions & 5 deletions inatcog/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pyinaturalist.models import Place

from .converters.base import QuotedContextMemberConverter
from .utils import get_home_server, get_valid_user_config
from .constants import HUB_SERVERS
from .utils import get_home_server, get_hub_server, get_valid_user_config

RESERVED_PLACES = ["home", "none", "clear", "all", "any"]

Expand All @@ -23,6 +24,14 @@ async def get_place(
response = None
home_id = None

async def _get_place(guild, abbrev):
response = None
guild_config = self.cog.config.guild(guild)
places = await guild_config.places()
if abbrev in places:
response = await self.cog.api.get_places(places[abbrev])
return response

_guild = guild or await get_home_server(self.cog, user)
if isinstance(query, str):
abbrev = query.lower()
Expand All @@ -43,10 +52,11 @@ async def get_place(
place_id = home_id or query
response = await self.cog.api.get_places(int(place_id))
elif _guild:
guild_config = self.cog.config.guild(_guild)
places = await guild_config.places()
if abbrev in places:
response = await self.cog.api.get_places(places[abbrev])
response = await _get_place(_guild, abbrev)
if not response and _guild.id not in HUB_SERVERS:
hub_server = await get_hub_server(self.cog, _guild)
if hub_server:
response = await _get_place(hub_server, abbrev)

if not response:
response = await self.cog.api.get_places(
Expand Down
15 changes: 15 additions & 0 deletions inatcog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,18 @@ async def get_home_server(
except LookupError:
pass
return guild


async def get_hub_server(
cog: commands.Cog,
guild: discord.Guild,
) -> discord.Guild:
hub_server = None
guild_config = cog.config.guild(guild)
if guild_config:
server_id = await guild_config.server()
hub_server = next(
(server for server in cog.bot.guilds if server.id == server_id),
None,
)
return hub_server

0 comments on commit 39f4258

Please sign in to comment.