Files
dc-nogi/bot/discovery.py
2026-08-01 15:09:47 +02:00

53 lines
2.0 KiB
Python

import logging
from dataclasses import dataclass
from typing import List, Optional
import discord
log = logging.getLogger(__name__)
@dataclass
class Target:
target_id: int
target_type: str # "guild" | "group_dm"
name: str
async def discover_targets(client: discord.Client, friend_id: int, manual_guild_ids: Optional[List[int]] = None) -> List[Target]:
targets: List[Target] = []
guild_ids_seen = set()
try:
friend = client.get_user(friend_id) or await client.fetch_user(friend_id)
profile = await friend.profile(with_mutual_guilds=True, with_mutual_friends=False, with_mutual_friends_count=False)
for mutual in (profile.mutual_guilds or []):
guild = client.get_guild(mutual.id)
name = guild.name if guild else f"Unknown Guild {mutual.id}"
targets.append(Target(mutual.id, "guild", name))
guild_ids_seen.add(mutual.id)
except discord.NotFound:
log.warning(
"No mutual guild/friend relationship visible for user %s "
"(profile lookup requires already being friends or sharing a server). "
"Relying on manual_guild_ids and group DMs only.",
friend_id,
)
except discord.HTTPException:
log.warning("Mutual-guilds profile lookup failed; falling back to manual_guild_ids only", exc_info=True)
for guild_id in (manual_guild_ids or []):
if guild_id in guild_ids_seen:
continue
guild = client.get_guild(guild_id)
name = guild.name if guild else f"Manual Guild {guild_id}"
targets.append(Target(guild_id, "guild", name))
guild_ids_seen.add(guild_id)
for channel in client.private_channels:
if isinstance(channel, discord.GroupChannel) and any(u.id == friend_id for u in channel.recipients):
name = channel.name or ", ".join(u.name for u in channel.recipients)
targets.append(Target(channel.id, "group_dm", name))
return targets