UNPKG

@twitchfy/chatbot

Version:

A powerful node module to make your own Twitch ChatBot

158 lines (157 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = void 0; const BaseMessage_1 = require("./BaseMessage"); const BaseUser_1 = require("./BaseUser"); const MessageReply_1 = require("./MessageReply"); const Collection_1 = require("./Collection"); const BaseEmote_1 = require("./BaseEmote"); const BaseCheermote_1 = require("./BaseCheermote"); /** * Represents a message in a chatroom. */ class Message extends BaseMessage_1.BaseMessage { /** * The content of the message. */ content; /** * The type of the message. */ type; /** * The chatroom where the message was sent. */ chatroom; /** * The mentions in the message. */ mentions; /** * The emotes in the message. */ emotes; /** * The cheermotes in the message. */ cheermotes; /** * The bits cheered in the message. */ bits; /** * The reward Id of the message. Null if the message doesn't have a reward redemption. */ rewardId; /** * The message reply of the message. Null if the message doesn't have a reply. */ messageReply; /** * The data of the message. */ commandData; /** * Creates a new instance of the message. * @param chatbot The current instance of the chatbot. * @param data The data of the message. * @param chatroom The chatroom where the message was sent. */ constructor(chatbot, data, chatroom) { super(chatbot, { ...data.message, user_id: data.chatter.id, user_name: data.chatter.displayName, user_login: data.chatter.login, chatroom_id: chatroom.id }); this.commandData = data; this.type = data.message.type; this.chatroom = chatroom; this.content = this.__parseContent(); this.mentions = this.__parseMentions(); this.emotes = this.__parseEmotes(); this.cheermotes = this.__parseCheermotes(); this.bits = data.message.bits; this.messageReply = data.message.reply ? new MessageReply_1.MessageReply(chatbot, data.message.reply, chatroom) : null; this.rewardId = data.message.channelRewardId; } /** * The Id of the author of the message. */ get authorId() { return this.author.id; } /** * Whether the message is a normal text message. */ get isText() { return this.commandData.message.type === 'text'; } /** * Whether the message was highlighted by the highlight message reward. */ get isHighlighted() { return this.commandData.message.type === 'channel_points_highlighted'; } /** * Whether the message was sent by the send in subscriber mode reward. */ get isChannelPointsSubOnly() { return this.commandData.message.type === 'channel_points_sub_only'; } /** * Whether the message is an user intro message. */ get isUserIntro() { return this.commandData.message.type === 'user_intro'; } /** * The message which was replied by this message. Null if the message doesn't have a reply. */ get parentReply() { return this.messageReply?.parent ?? null; } /** * The start message of the thread where this message is part of. Null if the message is not part of a thread. */ get threadReply() { return this.messageReply?.thread ?? null; } /** * Whether the message has a reward reedemption. */ get hasRewardRedeemption() { return !!this.rewardId; } /** * Parse the content of the message if the message is a command to remove the prefix and the command name. * @returns The parsed content. * @internal */ __parseContent() { return this.commandData.prefix ? this.commandData.message.content.slice(this.commandData.prefix.length + this.commandData.commandName.length).trim() : this.commandData.message.content; } /** * Parse the mentions of the message. * @returns A collection of the mentions. * @internal */ __parseMentions() { const data = this.commandData.message.fragments.filter((x) => x.type === 'mention').map((x) => new BaseUser_1.BaseUser(this.chatbot, { ...x.mention.user, display_name: x.mention.user.displayName })); return new Collection_1.Collection(data.map((x) => [x.id, x])); } /** * Parse the emotes of the message. * @returns A collection of the emotes. * @internal */ __parseEmotes() { const data = this.commandData.message.fragments.filter((x) => x.type === 'emote').map((x) => new BaseEmote_1.BaseEmote(this.chatbot, { ...x.emote, emote_set_id: x.emote.setId, owner_id: x.emote.ownerId, name: x.content })); return new Collection_1.Collection(data.map((x) => [x.id, x])); } /** * Parse the cheermotes of the message. * @returns A collection of the cheermotes. * @internal */ __parseCheermotes() { const data = this.commandData.message.fragments.filter((x) => x.type === 'cheermote').map((x) => new BaseCheermote_1.BaseCheermote(this.chatbot, { ...x.cheermote, broadcaster_id: this.chatroomId })); return new Collection_1.Collection(data.map((x) => [x.prefix, x])); } } exports.Message = Message;