UNPKG

twitch-core

Version:
252 lines (251 loc) 6.53 kB
/// <reference types="node" /> import winston from 'winston'; import EventEmitter from 'events'; import { ChatUserstate } from 'tmi.js'; import { EmotesManager } from '../emotes/EmotesManager'; import { CommandConstants } from './CommandConstants'; import { CommandArguments } from '../commands/CommandParser'; import { TwitchChatUser } from '../users/TwitchChatUser'; import { TwitchChatMessage } from '../messages/TwitchChatMessage'; import { TwitchChatCommand } from '../commands/TwitchChatCommand'; import { SettingsProvider } from '../settings/SettingsProvider'; declare type MessageLimits = keyof typeof CommandConstants.MESSAGE_LIMITS; interface TwitchOptions { /** * Twitch OAuth token */ oauthToken: string; /** * https://dev.twitch.tv/docs/authentication#refreshing-access-tokens */ refreshToken: string; /** * https://dev.twitch.tv/console/apps */ clientId: string; secretToken: string; /** * https://dev.twitch.tv/docs/authentication#scopes */ scopes: string[]; } interface ClientOptions { /** * Bot username */ username: string; /** * Bot oauth password (without oauth:) */ oauth: string; /** * Initials channels to join (default: empty array) */ channels: string[]; /** * List of bot owners username (default: empty array) */ botOwners?: string[]; /** * Server port (default: 8080) */ serverPort?: number; /** * Enable server (default: false) */ enableServer?: boolean; /** * Default command prefix (default: !) */ prefix?: string; /** * Denotes if the bot must send a message when join a channel (default: false) */ greetOnJoin?: boolean; /** * On Join message (sent if greetOnJoin = true) */ onJoinMessage?: string; /** * Denotes if the bot must autojoin its own channel (default: false) */ autoJoinBotChannel?: boolean; /** * Enable verbose logging (default: false) */ verboseLogging?: boolean; /** * Define the bot type, will be used for message limits control. * See CommandConstants for available bot type values (default: 'BOT_TYPE_NORMAL') */ botType?: MessageLimits; /** * Enable Rate Limiting control (default: true) */ enableRateLimitingControl?: boolean; } declare type ChatterState = ChatUserstate & { message: string; }; declare class TwitchCommandClient extends EventEmitter { readonly logger: winston.Logger; private tmi; private channelsWithMod; private parser; private server; options: ClientOptions; commands: TwitchChatCommand[]; emotesManager: EmotesManager; provider: SettingsProvider; messagesCounterInterval: NodeJS.Timeout; messagesCount: number; constructor(options: ClientOptions); private checkOptions; /** * Connect the bot to Twitch Chat */ connect(): Promise<void>; /** * Send a text message in the channel * * @param channel * @param message * @param addRandomEmote */ say(channel: string, message: string, addRandomEmote?: boolean): Promise<[string]>; /** * Send an action message in the channel * * @param channel * @param message * @param addRandomEmote */ action(channel: string, message: string, addRandomEmote?: boolean): Promise<[string]>; /** * Send a private message to the user with given text * * @param username * @param message */ whisper(username: string, message: string): Promise<[string, string]>; /** * Register commands in given path (recursive) * * @param path * @param options */ registerCommandsIn(path: string): void; /** * Register text commands */ registerTextCommands(): void; /** * Register default commands */ registerDefaultCommands(): void; findCommand(parserResult: CommandArguments): TwitchChatCommand; /** * Execute command method * * @param command * @param msg */ executeCommandMethod(command: string, msg: TwitchChatMessage): void; /** * Bot connected */ onConnect(): void; /** * Channel joined or someone join the channel * * @param channel * @param username */ onJoin(channel: string, username: string): void; /** * Bot disconnects */ onDisconnect(): void; /** * Command executed * * @param channel * @param userstate * @param messageText * @param self */ private onMessage; /** * Connection timeout * * @param channel * @param username * @param reason * @param duration */ onTimeout(channel: string, username: string, reason: string, duration: string): void; /** * Reconnection */ onReconnect(): void; /** * Request the bot to join a channel * * @param channel */ join(channel: string): Promise<[string]>; /** * Request the bot to leave a channel * * @param channel */ part(channel: string): Promise<[string]>; /** * Gets the bot username */ getUsername(): string; /** * Gets the bot channels */ getChannels(): string[]; /** * Checks if the message author is one of bot owners * * @param author */ isOwner(author: TwitchChatUser): boolean; /** * Received mod role * * @param channel * @param username */ onMod(channel: string, username: string): void; /** * Emit error * * @param error */ onError(error: unknown): void; /** * Unmod bot * * @param channel * @param username */ onUnmod(channel: string, username: string): void; /** * Start messages counting */ private startMessagesCounterInterval; /** * Reset message counter */ private resetMessageCounter; /** * Check if the bot sent too many messages in timespan limit */ private checkRateLimit; private toLowerArray; } export { TwitchCommandClient, ClientOptions, TwitchOptions, ChatterState };