# Discord Webhook Alerts

## Objective

Push product and operational alerts into a Discord channel without exposing the webhook.

An outbound Discord destination that receives selected app events as formatted channel messages.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find the events already worth announcing internally — signups, failed payments, error spikes, support requests — and let an operator choose which of them route to a Discord destination. Do not fan out every event the app emits.
2. Store the webhook target as a server-side secret. It must never reach the browser, a client bundle, a log line, or an error report, because anyone holding it can post to the channel indefinitely.
3. Send through the app's existing background-job system so a slow or unreachable Discord never blocks the request that triggered the event.
4. Treat every message as carrying untrusted content. Neutralise mention syntax, role and channel references, and link markup in any user-supplied text before it is included.
5. Do not build a second notification pipeline. If the app already routes notifications to email or another channel, add Discord as one more destination on that pipeline so preferences, throttling, and audit stay in one place.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A webhook target must be validated when it is saved, by confirming it accepts a test message, and the validation result must be reported without ever echoing the target itself back to the screen or into logs.
- User-supplied text that contains mention syntax must not be able to notify everyone in the channel. Escape or strip mentions rather than trusting that the content looks harmless.
- Discord enforces limits on message length and on the number and size of structured fields. Truncate on a sensible boundary and link back to the app for the full record rather than letting the send fail.
- A webhook deleted on the Discord side will keep rejecting sends forever. Detect the permanent rejection, mark the destination as broken, stop retrying, and tell an operator in the app.
- A send that times out may still have been delivered. Attach a stable identifier per event and keep a record of what was sent, so a retry after a timeout does not post the same alert twice.
- Discord rate-limits aggressively when a burst arrives. Honour the throttling signal, back off, and queue rather than dropping alerts on the floor.
- A busy hour must not turn into hundreds of near-identical messages. Collapse repeated events of the same kind into a single summary above a threshold.
- When Discord is down entirely, the triggering action must still succeed and the alert must be retried later or visibly abandoned, never silently lost.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Operators choose which event types route to Discord, and the default is nothing.
- [ ] The webhook target is stored server-side and appears in no log, response, or client asset.
- [ ] Alerts are delivered from a background job and never delay the originating request.
- [ ] User-supplied content cannot trigger a channel-wide or role mention.
- [ ] Retries after a timeout do not produce duplicate messages in the channel.
- [ ] A deleted or rejected webhook is marked broken, stops retrying, and is surfaced to an operator.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
