Skip to content

Discord Integration #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Huijiro
Copy link

@Huijiro Huijiro commented Jun 10, 2025

Summary by CodeRabbit

  • New Features
    • Added support for structured parsing and validation of Discord message data, enabling reliable extraction of message details from raw input.
    • Enabled handling of Discord messages within the data processing system, including validation of content type and parsing.
    • Added capability to send replies to Discord messages through a new service interface and API implementation.
    • Integrated Discord messaging support into the server context and API exports for seamless use across the platform.

Copy link
Contributor

coderabbitai bot commented Jun 10, 2025

Walkthrough

A new module for handling Discord message data has been introduced. It defines a TypeScript interface for message structure, a type guard for validation, a class to encapsulate and validate messages, and an asynchronous function to parse messages from raw buffers. The module provides structured access and validation for Discord messages. Additionally, support for Discord message parsing and replying is integrated into the data handling, API, server context, and type definitions. The main entry point exports the new Discord API alongside the existing APIs.

Changes

File(s) Change Summary
src/io/discord.ts Added interface for Discord message, type guard, class for encapsulation/validation, and async parser function.
src/router/data.ts Added discordMessage() async method in DataHandler to parse Discord messages from JSON content buffers.
src/types.ts Extended Data interface with discordMessage() method; added DiscordService interface with sendReply method; added discord property to AgentContext; imported DiscordMessage.
src/apis/discord.ts Added DiscordApi class implementing DiscordService with sendReply method using traced HTTP POST requests.
src/index.ts Added named exports for EmailAPI and DiscordAPI.
src/server/server.ts Imported and instantiated DiscordAPI, added it to server context's API set.

Sequence Diagram(s)

sequenceDiagram
    participant DataHandler
    participant Buffer
    participant DiscordMessage
    participant TypeGuard

    DataHandler->>Buffer: Receive raw data (Buffer)
    DataHandler->>DiscordMessage: Call parseDiscordMessage with Buffer
    DiscordMessage->>TypeGuard: Validate parsed object
    TypeGuard-->>DiscordMessage: Validation result
    DiscordMessage-->>DataHandler: Return DiscordMessage instance or throw error
Loading
sequenceDiagram
    participant DiscordApi
    participant BackendServer
    participant OpenTelemetry

    DiscordApi->>OpenTelemetry: Start child span "agentuity.discord.reply"
    DiscordApi->>BackendServer: POST /discord/{agentId}/reply with message data
    BackendServer-->>DiscordApi: Response (200 OK or error)
    DiscordApi->>OpenTelemetry: Set span status based on response
    DiscordApi-->>OpenTelemetry: End span
Loading

Poem

🐇
A message hops from Buffer's nest,
Parsed and checked, it passes the test.
With guards and classes, neat and bright,
Discord chats come into light.
Replies can now be sent with cheer—
A rabbit’s code, both swift and clear!
🌿✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 645d6af and 59ff135.

📒 Files selected for processing (2)
  • src/apis/discord.ts (1 hunks)
  • src/io/discord.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/apis/discord.ts
  • src/io/discord.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/io/discord.ts (1)

97-107: Remove unnecessary async keyword.

The function doesn't perform any asynchronous operations, so the async keyword is unnecessary and misleading.

Apply this diff to remove the unnecessary async:

-export async function parseDiscordMessage(
+export function parseDiscordMessage(
	data: Buffer
-): Promise<DiscordMessage> {
+): DiscordMessage {
	try {
		return new DiscordMessage(data.toString());
	} catch (error) {
		throw new Error(
			`Failed to parse discord: ${error instanceof Error ? error.message : 'Unknown error'}`
		);
	}
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 21e5b97 and 265d8ed.

📒 Files selected for processing (1)
  • src/io/discord.ts (1 hunks)
🔇 Additional comments (2)
src/io/discord.ts (2)

1-22: Well-structured interface with clear documentation.

The interface definition is clean and well-documented. The optional guildId for DM messages is appropriately handled.


46-95: Clean class implementation with proper encapsulation.

The class follows good TypeScript practices with private readonly fields, proper validation, and clean getters. The isDM() method correctly identifies direct messages.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/router/data.ts (1)

217-223: LGTM! Consider improving the error message for clarity.

The implementation correctly follows the same pattern as the existing email() method with proper content type validation.

Consider making the error message more specific about the requirement:

-		throw new Error('The content type is not a valid discord message');
+		throw new Error('Discord messages require application/json content type');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 265d8ed and 899c627.

📒 Files selected for processing (3)
  • src/io/discord.ts (1 hunks)
  • src/router/data.ts (2 hunks)
  • src/types.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/io/discord.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/types.ts (1)
src/io/discord.ts (4)
  • DiscordMessage (81-130)
  • messageId (112-114)
  • guildId (104-106)
  • content (123-125)
🪛 Biome (1.9.4)
src/router/data.ts

[error] 5-5: Some named imports are only used as types.

This import is only used as a type.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Add inline type keywords.

(lint/style/useImportType)

src/types.ts

[error] 5-5: All these imports are only used as types.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Use import type.

(lint/style/useImportType)

🔇 Additional comments (1)
src/types.ts (1)

78-81: LGTM! Consistent with existing patterns.

The method signature and documentation follow the same pattern as the existing email() method.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/apis/discord.ts (2)

10-14: Fix JSDoc parameter mismatch (guildIdchannelId).

The JSDoc block still references guildId, but the actual argument is channelId. This can mislead generated docs and consumers.

- * @param guildId - the guild id of the discord message (undefined for DMs)
+ * @param channelId - the channel or DM id of the discord message

24-30: Capture channelId in tracing span.

For troubleshooting you’ll almost always need the channel/DM identifier. Recording it as an attribute keeps the trace self-contained.

 span.setAttribute('@agentuity/agentId', agentId);
 span.setAttribute('@agentuity/discordMessageId', messageId);
+span.setAttribute('@agentuity/discordChannelId', channelId);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 30d294a and af710d0.

📒 Files selected for processing (4)
  • src/apis/discord.ts (1 hunks)
  • src/index.ts (1 hunks)
  • src/server/server.ts (3 hunks)
  • src/types.ts (4 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/index.ts
  • src/server/server.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/types.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (1)
src/apis/discord.ts (1)

54-56: Guard against missing response object in error branch.

resp.response is referenced only in the error path; make sure it is always present, or default to resp.statusText.

-`error sending discord reply: ${resp.response.statusText} (${resp.response.status})`
+`error sending discord reply: ${resp.response?.statusText ?? 'unknown'} (${resp.status})`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant