Add DM support, friend identification, and target pruning
React in 1:1 DMs alongside servers/groups, surface the resolved friend username at startup and in the stats panel, verify reactions actually stick after adding them (with jump links for manual spot-checking), and prune stale targets from stats on each run so only the currently mutual set is shown.
This commit is contained in:
@@ -10,16 +10,27 @@ log = logging.getLogger(__name__)
|
||||
@dataclass
|
||||
class Target:
|
||||
target_id: int
|
||||
target_type: str # "guild" | "group_dm"
|
||||
target_type: str # "guild" | "group_dm" | "dm"
|
||||
name: str
|
||||
|
||||
|
||||
async def discover_targets(client: discord.Client, friend_id: int, manual_guild_ids: Optional[List[int]] = None) -> List[Target]:
|
||||
async def resolve_friend(client: discord.Client, friend_id: int) -> discord.User:
|
||||
"""Fetch the target user object so we can display who the script is
|
||||
actually reacting to (helps catch a wrong/typo'd friend_id early)."""
|
||||
user = client.get_user(friend_id)
|
||||
if user is None:
|
||||
user = await client.fetch_user(friend_id)
|
||||
return user
|
||||
|
||||
|
||||
async def discover_targets(
|
||||
client: discord.Client, friend: discord.User, manual_guild_ids: Optional[List[int]] = None
|
||||
) -> List[Target]:
|
||||
friend_id = friend.id
|
||||
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)
|
||||
@@ -44,6 +55,12 @@ async def discover_targets(client: discord.Client, friend_id: int, manual_guild_
|
||||
targets.append(Target(guild_id, "guild", name))
|
||||
guild_ids_seen.add(guild_id)
|
||||
|
||||
try:
|
||||
dm_channel = friend.dm_channel or await friend.create_dm()
|
||||
targets.append(Target(dm_channel.id, "dm", f"DM with {friend}"))
|
||||
except discord.HTTPException:
|
||||
log.warning("Failed to open the 1:1 DM channel with %s", friend, exc_info=True)
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user