import { api, Room } from '@rocket.chat/core-services'; import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings'; import { Rooms, Subscriptions, Users } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; import { i18n } from '../../lib/i18n'; import { slashCommands } from '../../lib/utils/slashCommand'; import { settings } from '../../settings'; slashCommands.add({ command: 'join', callback: async ({ params, message, userId }: SlashCommandCallbackParams<'join'>): Promise => { let channel = params.trim(); if (channel === '') { return; } if (!userId) { return; } channel = channel.replace('#', ''); const room = await Rooms.findOneByNameAndType(channel, 'c'); if (!room) { void api.broadcast('notify.ephemeralMessage', userId, message.rid, { msg: i18n.t('Channel_doesnt_exist', { interpolation: { escapeValue: false }, channelName: channel, lng: settings.get('Language') || 'en', }), }); return; } const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, userId, { projection: { _id: 1 }, }); if (subscription) { throw new Meteor.Error('error-user-already-in-room', 'You are already in the channel', { method: 'slashCommands', }); } const user = await Users.findOneById(userId); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'slashCommands', }); } await Room.join({ room, user }); }, options: { description: 'Join_the_given_channel', params: '#channel', permission: 'view-c-room', }, });