diff --git a/src/rabbitmq/RabbitMQHandler.ts b/src/rabbitmq/RabbitMQHandler.ts index f13f18d1..f75c76cc 100644 --- a/src/rabbitmq/RabbitMQHandler.ts +++ b/src/rabbitmq/RabbitMQHandler.ts @@ -6,10 +6,12 @@ import { handleInteractionResponseCreate } from './events/interactionResponseCre import { handleInteractionResponseEdit } from './events/interactionResponseEdit'; import { handleInteractionResponseDelete } from './events/interactionResponseDelete'; import { handleFollowUpMessageCreate } from './events/FollowUpMessageCreate'; +import { handleSendMessageToChannel } from './events/sendMessageToChannel'; export function setupRabbitMQHandlers() { RabbitMQ.onEvent(Event.DISCORD_BOT.FETCH, handleFetchEvent); RabbitMQ.onEvent(Event.DISCORD_BOT.SEND_MESSAGE, handleSendMessageEvent); + RabbitMQ.onEvent(Event.DISCORD_BOT.SEND_MESSAGE_TO_CHANNEL, handleSendMessageToChannel); RabbitMQ.onEvent(Event.DISCORD_BOT.FETCH_MEMBERS, handleFetchMembersEvent); RabbitMQ.onEvent(Event.DISCORD_BOT.INTERACTION_RESPONSE.CREATE, handleInteractionResponseCreate); RabbitMQ.onEvent(Event.DISCORD_BOT.INTERACTION_RESPONSE.EDIT, handleInteractionResponseEdit); diff --git a/src/rabbitmq/events/sendMessageToChannel.ts b/src/rabbitmq/events/sendMessageToChannel.ts new file mode 100644 index 00000000..e76ce21e --- /dev/null +++ b/src/rabbitmq/events/sendMessageToChannel.ts @@ -0,0 +1,26 @@ +import { Event, MBConnection } from '@togethercrew.dev/tc-messagebroker'; +import parentLogger from '../../config/logger'; +import { channelService } from '../../services'; +const logger = parentLogger.child({ module: `${Event.DISCORD_BOT.SEND_MESSAGE}` }); + +export async function handleSendMessageToChannel(msg: any) { + try { + logger.info({ msg, event: Event.DISCORD_BOT.SEND_MESSAGE_TO_CHANNEL }, 'is running'); + if (!msg) return; + + const { content } = msg; + const saga = await MBConnection.models.Saga.findOne({ sagaId: content.uuid }); + + const channels = saga.data['channels']; + const message = saga.data['message']; + + for (let i = 0; i < channels.length; i++) { + await channelService.sendChannelMessage(channels[i], message) + } + + logger.info({ msg, event: Event.DISCORD_BOT.SEND_MESSAGE_TO_CHANNEL }, 'is done'); + } catch (error) { + logger.error({ msg, event: Event.DISCORD_BOT.SEND_MESSAGE_TO_CHANNEL, error }, 'is failed'); + + } +} \ No newline at end of file