Skip to content

Commit

Permalink
Add Discord home server user setting for DM place lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
synrg committed Jul 29, 2024
1 parent 83903ba commit 990b1b6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
96 changes: 94 additions & 2 deletions inatcog/commands/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,39 @@ async def user_remove(self, ctx, discord_user: str):

async def user_show_settings(self, ctx, config, setting: str = "all"):
"""iNat user settings."""
if setting not in ["all", "known", "home", "lang"]:
if setting not in ["all", "known", "home", "lang", "server"]:
await ctx.send(f"Unknown setting: {setting}")
return
if setting in ["all", "known"]:
known_all = await config.known_all()
await ctx.send(f"known: {known_all}")
guild = ctx.guild
if setting in ["all", "server"]:
server_id = await config.server()
home_server = None
home_server_name = "none"
if server_id:
if guild:
guilds = ctx.author.mutual_guilds
else:
guilds = ctx.bot.guilds
home_server = next(
(server for server in guilds if server.id == server_id), None
)
if not home_server:
home_server_name = f"Unknown Discord server #{server_id}"
elif not guild:
# If in a DM and the user has a home server set, use it to
# look up the `home` place setting below.
guild = home_server
if home_server:
home_server_name = home_server.name
await ctx.send(f"server: {home_server_name}")
if setting in ["all", "home"]:
home_id = await config.home()
if home_id:
try:
home = await self.place_table.get_place(ctx.guild, home_id)
home = await self.place_table.get_place(guild, home_id)
await ctx.send(f"home: {home.display_name} (<{home.url}>)")
except LookupError:
await ctx.send(f"Non-existent place ({home_id})")
Expand Down Expand Up @@ -328,6 +350,76 @@ async def user_set(self, ctx, arg: str = None):

await self.user_show_settings(ctx, config)

@user_set.command(name="server")
@known_inat_user()
async def user_set_server(self, ctx, *, value: str = None):
"""Set a Discord server as your home server.
`[p]user set server` show your home Discord server
`[p]user set server clear` clear your Discord home server
`[p]user set server this` set this server as your Discord home server
`[p]user set server [id]` set the Discord home server using its Server ID#
Where [id] is a Discord Server ID. See:
https://support.discord.com/hc/en-us/search?query=Find+my+server+ID
The abbreviations saved on your home server with `,place add` and
`,project add` can be used in commands that you DM to the bot so long
as both you and the bot are members of that server.
"""
try:
config = await get_valid_user_config(self, ctx.author, anywhere=True)
except LookupError as err:
await ctx.send(err)
return

if value is not None:
value = re.sub(DEQUOTE, r"\1", value)
bot = self.bot.user.name
if value.lower() in ["clear", "none", ""]:
await config.server.clear()
await ctx.send(
f"{bot} no longer has a home Discord server set for you."
)
else:
try:
home_server = None
if value.lower() == "this":
if ctx.guild:
home_server = ctx.guild
else:
await ctx.send(
f"You must type `{ctx.clean_prefix}user set server this` in a "
"server channel."
)
else:
if ctx.guild:
guilds = ctx.author.mutual_guilds
else:
guilds = ctx.bot.guilds
server_id = int(value)
home_server = next(
(server for server in guilds if server.id == server_id),
None,
)
if not home_server:
await ctx.send(
f"Specify an ID for a server where both you and {bot} are members. "
f"Alternatively, type `{ctx.clean_prefix}user set server this` in "
"a channel on that server."
)
if home_server:
await config.server.set(home_server.id)
await ctx.send(
f"{bot} will use {home_server.name} as your Discord home server."
)
except (LookupError, ValueError) as err:
await ctx.send(err)
return

await self.user_show_settings(ctx, config, "server")

@user_set.command(name="home")
@known_inat_user()
async def user_set_home(self, ctx, *, value: str = None):
Expand Down
23 changes: 19 additions & 4 deletions inatcog/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ async def get_place(
response = None
home_id = None

_guild = guild
if not _guild and user:
try:
user_config = await get_valid_user_config(self.cog, user, anywhere=True)
server_id = await user_config.server()
_guild = next(
(
server
for server in self.cog.bot.guilds
if server.id == server_id
),
None,
)
except LookupError:
pass
if isinstance(query, str):
abbrev = query.lower()
if abbrev == "home" and user:
Expand All @@ -33,16 +48,16 @@ async def get_place(
home_id = await user_config.home()
except LookupError:
pass
if not home_id and guild:
guild_config = self.cog.config.guild(guild)
if not home_id and _guild:
guild_config = self.cog.config.guild(_guild)
home_id = await guild_config.home()
if not home_id:
home_id = await self.cog.config.home()
if home_id or isinstance(query, int) or query.isnumeric():
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)
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])
Expand Down

0 comments on commit 990b1b6

Please sign in to comment.