Merged
Conversation
Contributor
Reviewer's GuideIntroduces a safety-focused internal direct messaging service with policy-based audience allowlists, a small test /notify cog, and documentation describing the DM safety model, along with unit tests and minor formatting cleanup. Sequence diagram for /notify self-test DM flowsequenceDiagram
actor User
participant DiscordClient
participant Bot
participant NotifyCog
participant Policies as policies_module
participant DmFacade as dm_module
participant Messenger as DirectMessenger
participant DiscordAPI
User->>DiscordClient: invoke slash command /notify message
DiscordClient->>Bot: interaction create
Bot->>NotifyCog: notify(interaction, message)
NotifyCog->>NotifyCog: validate guild not None
NotifyCog->>Policies: allow_users(interaction.user.id, max_recipients=1)
Policies-->>NotifyCog: Policy
NotifyCog->>DmFacade: compose_to_user(guild, interaction.user.id, message, policy)
DmFacade->>Messenger: compose_to_user(guild, user_id, content, policy)
Messenger->>Messenger: _resolve_policy(policy)
Messenger->>Messenger: _normalize_content(content)
Messenger->>Messenger: _validate_requested_audience(user_ids, role_ids, policy, default_role_id)
Messenger->>Messenger: _resolve_audience(guild, user_ids, role_ids)
Messenger->>Messenger: _validate_send_policy(policy, preview)
Messenger-->>DmFacade: Draft
DmFacade-->>NotifyCog: Draft
NotifyCog->>DmFacade: render_preview(Draft)
DmFacade->>Messenger: render_preview(Draft)
Messenger-->>DmFacade: str preview
DmFacade-->>NotifyCog: str preview
NotifyCog->>NotifyCog: log preview
NotifyCog->>DmFacade: send(guild, Draft)
DmFacade->>Messenger: send(guild, Draft)
Messenger->>Messenger: _validate_send_policy(policy, preview)
loop for each recipient
Messenger->>DiscordAPI: create DM message
alt success
DiscordAPI-->>Messenger: message created
else failure
DiscordAPI-->>Messenger: error
Messenger->>Messenger: record failed_id
end
end
Messenger-->>DmFacade: SendResult
DmFacade-->>NotifyCog: SendResult
alt sent_count == 1
NotifyCog->>DiscordClient: interaction.response.send_message("Sent you a DM.", ephemeral)
else
NotifyCog->>NotifyCog: raise UserFriendlyError
NotifyCog->>DiscordClient: error response
end
DiscordClient-->>User: ephemeral confirmation or error
Class diagram for internal DM service and policiesclassDiagram
class Policy {
+frozenset~int~ allowed_user_ids
+frozenset~int~ allowed_role_ids
+int max_recipients
+__post_init__()
}
class MessagePayload {
+str content
}
class AudiencePreview {
+list~discord.Member~ recipients
+list~int~ skipped_ids
+tuple~int,...~ source_user_ids
+tuple~int,...~ source_role_ids
+int recipient_count
}
class Draft {
+int guild_id
+AudiencePreview preview
+MessagePayload payload
+Policy policy
+datetime created_at
}
class SendResult {
+int sent_count
+list~int~ failed_ids
}
class DirectMessenger {
-logging.Logger log
+DirectMessenger()
+compose(guild, content, user_ids, role_ids, policy) Draft
+compose_to_user(guild, user_id, content, policy) Draft
+compose_to_users(guild, user_ids, content, policy) Draft
+compose_to_role(guild, role_id, content, policy) Draft
+compose_to_roles(guild, role_ids, content, policy) Draft
+send(guild, draft) SendResult
+render_preview(draft) str
-_compose(guild, content, user_ids, role_ids, policy) Draft
-_resolve_policy(policy) Policy
-_normalize_content(content) str
-_validate_requested_audience(user_ids, role_ids, policy, default_role_id) void
-_resolve_audience(guild, user_ids, role_ids) AudiencePreview
-_validate_send_policy(policy, preview) void
-_resolve_member(guild, user_id) discord.Member
}
class PoliciesModule {
+Policy DENY_ALL
+allow_users(user_ids, max_recipients) Policy
+allow_roles(role_ids, max_recipients) Policy
+allow_targets(user_ids, role_ids, max_recipients) Policy
}
class DmFacade {
+compose(guild, content, user_ids, role_ids, policy) Draft
+compose_to_user(guild, user_id, content, policy) Draft
+compose_to_users(guild, user_ids, content, policy) Draft
+compose_to_role(guild, role_id, content, policy) Draft
+compose_to_roles(guild, role_ids, content, policy) Draft
+send(guild, draft) SendResult
+render_preview(draft) str
}
DirectMessenger --> Policy
DirectMessenger --> MessagePayload
DirectMessenger --> AudiencePreview
DirectMessenger --> Draft
DirectMessenger --> SendResult
Draft --> AudiencePreview
Draft --> MessagePayload
Draft --> Policy
PoliciesModule --> Policy
DmFacade --> DirectMessenger
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
DirectMessenger._resolve_policy, consider returning the sharedpolicies.DENY_ALLinstance instead of constructing a newPolicy()so there is a single canonical default and you can rely on identity checks or shared configuration in one place. - In
AudiencePreview,skipped_idscurrently mixes both user and role IDs; if you plan to inspect this for debugging or metrics, it may be clearer to track skipped user IDs and role IDs separately (or annotate their type) to make diagnostics more precise.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `DirectMessenger._resolve_policy`, consider returning the shared `policies.DENY_ALL` instance instead of constructing a new `Policy()` so there is a single canonical default and you can rely on identity checks or shared configuration in one place.
- In `AudiencePreview`, `skipped_ids` currently mixes both user and role IDs; if you plan to inspect this for debugging or metrics, it may be clearer to track skipped user IDs and role IDs separately (or annotate their type) to make diagnostics more precise.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Introduce a safety-focused internal direct messaging service with explicit policies and a narrow self-test command surface, and update documentation to codify its usage model.
New Features:
/notifycommand that lets users send themselves a test DM using the internal DM service.Enhancements:
Tests: