-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add channel id for thread message #21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import { GatewayMessageCreateDispatchData } from 'discord-api-types/v10'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { DatabaseManager, makeThreadRepository } from '@togethercrew.dev/db'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export async function getChannelIdFromThread( | ||||||||||||||||||||||||||||||||||||||||||||||
| data: GatewayMessageCreateDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||
| let channelId: string; | ||||||||||||||||||||||||||||||||||||||||||||||
| const dbConnection = await DatabaseManager.getInstance().getGuildDb( | ||||||||||||||||||||||||||||||||||||||||||||||
| data.guild_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| const threadRepository = makeThreadRepository(dbConnection); | ||||||||||||||||||||||||||||||||||||||||||||||
| channelId = (await threadRepository.findOne({ id: data.channel_id })) | ||||||||||||||||||||||||||||||||||||||||||||||
| .parent_id; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential null pointer exception and improve error handling. The code doesn't handle the case where the thread is not found in the database, which will cause a runtime error when trying to access - try {
- let channelId: string;
- const dbConnection = await DatabaseManager.getInstance().getGuildDb(
- data.guild_id,
- );
- const threadRepository = makeThreadRepository(dbConnection);
- channelId = (await threadRepository.findOne({ id: data.channel_id }))
- .parent_id;
- return channelId;
+ try {
+ const dbConnection = await DatabaseManager.getInstance().getGuildDb(
+ data.guild_id,
+ );
+ const threadRepository = makeThreadRepository(dbConnection);
+ const thread = await threadRepository.findOne({ id: data.channel_id });
+
+ if (!thread) {
+ throw new Error(`Thread not found: ${data.channel_id}`);
+ }
+
+ if (!thread.parent_id) {
+ throw new Error(`Thread ${data.channel_id} has no parent channel`);
+ }
+
+ return thread.parent_id;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
| return channelId; | ||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.error('Error in getChannelIdFromThread:', error); | ||||||||||||||||||||||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use proper logging instead of console.error. Consistent with other parts of the codebase, use a proper logger instead of console.error. +import parentLogger from '../../../config/logger.config';
+
+const logger = parentLogger.child({ activity: 'discord:gateway:add-channelId' });
} catch (error) {
- console.error('Error in getChannelIdFromThread:', error);
+ logger.error({ error, guildId: data.guild_id, channelId: data.channel_id }, 'Failed to get channel ID from thread');
throw error;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,22 @@ import { Snowflake } from 'discord.js'; | |||||||
| import { FilterQuery } from 'mongoose'; | ||||||||
|
|
||||||||
| import { | ||||||||
| DatabaseManager, IChannel, IChannelUpdateBody, IGuildMember, IGuildMemberUpdateBody, IRawInfo, | ||||||||
| IRawInfoUpdateBody, IRole, IRoleUpdateBody, IThread, makeChannelRepository, | ||||||||
| makeGuildMemberRepository, makeRawInfoRepository, makeRoleRepository, makeThreadRepository, | ||||||||
| ThreadUpdateBody | ||||||||
| DatabaseManager, | ||||||||
| IChannel, | ||||||||
| IChannelUpdateBody, | ||||||||
| IGuildMember, | ||||||||
| IGuildMemberUpdateBody, | ||||||||
| IRawInfo, | ||||||||
| IRawInfoUpdateBody, | ||||||||
| IRole, | ||||||||
| IRoleUpdateBody, | ||||||||
| IThread, | ||||||||
| makeChannelRepository, | ||||||||
| makeGuildMemberRepository, | ||||||||
| makeRawInfoRepository, | ||||||||
| makeRoleRepository, | ||||||||
| makeThreadRepository, | ||||||||
| ThreadUpdateBody, | ||||||||
| } from '@togethercrew.dev/db'; | ||||||||
|
|
||||||||
| import parentLogger from '../../../config/logger.config'; | ||||||||
|
|
@@ -138,6 +150,7 @@ export async function createRawInfo( | |||||||
| data: IRawInfo, | ||||||||
| ): Promise<void> { | ||||||||
| try { | ||||||||
| console.log('Creating raw info', data); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace console.log with proper logging. Using - console.log('Creating raw info', data);
+ logger.debug({ guildId, data }, 'Creating raw info');📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
| const dbConnection = | ||||||||
| await DatabaseManager.getInstance().getGuildDb(guildId); | ||||||||
| const repository = makeRawInfoRepository(dbConnection); | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,23 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageCreateDispatchData, GatewayMessageDeleteBulkDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageDeleteDispatchData, GatewayMessageReactionAddDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionRemoveAllDispatchData, GatewayMessageReactionRemoveDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionRemoveEmojiDispatchData, GatewayMessageUpdateDispatchData | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageCreateDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageDeleteBulkDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageDeleteDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionAddDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionRemoveAllDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionRemoveDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageReactionRemoveEmojiDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GatewayMessageUpdateDispatchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from 'discord-api-types/v10'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { proxyActivities } from '@temporalio/workflow'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type * as Activities from '../../../../activities'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DatabaseManager, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeChannelRepository, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeRawInfoRepository, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeThreadRepository, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@togethercrew.dev/db'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove unused imports. These database-related imports are not used anywhere in the file and should be removed to avoid confusion. -import {
- DatabaseManager,
- makeChannelRepository,
- makeRawInfoRepository,
- makeThreadRepository,
-} from '@togethercrew.dev/db';📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const activities = proxyActivities<typeof Activities>({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| startToCloseTimeout: '1 minute', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -28,8 +38,18 @@ export class MessageHandler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static async create(data: GatewayMessageCreateDispatchData) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(await guardMessage(data.guild_id, data.channel_id, data.author.id))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const mapped = await activities.mapMessageCreate(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Need to remove this section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const tempData: any = data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isThreadMessage = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tempData.channel_type === 10 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tempData.channel_type === 11 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tempData.channel_type === 12; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isThreadMessage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const channelId = await activities.getChannelIdFromThread(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mapped.channelId = channelId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Until Here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve type safety and use constants for channel types. The current implementation has several issues: type casting to +// Discord channel types constants
+const DISCORD_CHANNEL_TYPES = {
+ GUILD_PUBLIC_THREAD: 11,
+ GUILD_PRIVATE_THREAD: 12,
+ GUILD_NEWS_THREAD: 10,
+} as const;
- // TODO: Need to remove this section
- const tempData: any = data;
- const isThreadMessage =
- tempData.channel_type === 10 ||
- tempData.channel_type === 11 ||
- tempData.channel_type === 12;
- if (isThreadMessage) {
- const channelId = await activities.getChannelIdFromThread(data);
- mapped.channelId = channelId;
- }
- // TODO: Until Here
+ // TODO: Need to remove this section
+ const isThreadMessage =
+ 'channel_type' in data && (
+ data.channel_type === DISCORD_CHANNEL_TYPES.GUILD_NEWS_THREAD ||
+ data.channel_type === DISCORD_CHANNEL_TYPES.GUILD_PUBLIC_THREAD ||
+ data.channel_type === DISCORD_CHANNEL_TYPES.GUILD_PRIVATE_THREAD
+ );
+ if (isThreadMessage) {
+ try {
+ const channelId = await activities.getChannelIdFromThread(data);
+ mapped.channelId = channelId;
+ } catch (error) {
+ // Log error but don't fail the entire operation
+ console.error('Failed to get channel ID from thread:', error);
+ }
+ }
+ // TODO: Until Here📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await activities.createRawInfo(data.guild_id, mapped); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,6 +58,7 @@ export class MessageHandler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const mapped = await activities.mapMessageUpdate(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await activities.updateRawInfo( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.guild_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { messageId: data.id }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add explicit return type and parameter validation.
The function should have an explicit return type and validate required parameters.
📝 Committable suggestion
🤖 Prompt for AI Agents