From ab8786b68b019ea5ea8b06d13b4d67a7f7fe6bc6 Mon Sep 17 00:00:00 2001 From: alexbcberio Date: Thu, 6 Jan 2022 02:49:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Handle=20chat=20message=20commands?= =?UTF-8?q?=20(only=20for=20broadcaster)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/chatClient/index.ts | 33 +++++++++++++++++++++++++++++++++ src/enums/ChatCommands.ts | 3 +++ 2 files changed, 36 insertions(+) create mode 100644 src/enums/ChatCommands.ts diff --git a/src/backend/chatClient/index.ts b/src/backend/chatClient/index.ts index 15aeeb7..a029ef7 100644 --- a/src/backend/chatClient/index.ts +++ b/src/backend/chatClient/index.ts @@ -4,6 +4,8 @@ import { getAuthProvider, getUsernameFromId } from "../helpers/twitch"; import { Action } from "../../interfaces/actions/Action"; import { ActionType } from "../../enums/ActionType"; import { ChatClient } from "@twurple/chat"; +import { ChatCommands } from "../../enums/ChatCommands"; +import { TwitchPrivateMessage } from "@twurple/chat/lib/commands/TwitchPrivateMessage"; import { broadcast } from "../helpers/webServer"; import { start } from "../helpers/miniDb"; @@ -35,6 +37,8 @@ async function connect(channels: Array): Promise { console.log(`${LOG_PREFIX}No permission on ${channel}: ${message}`); }); + chatClient.onMessage(onMessage); + await chatClient.connect(); } @@ -79,3 +83,32 @@ async function handleClientAction(action: Action): Promise { console.log(`${[LOG_PREFIX]}Couldn't handle action:`, action); } } + +const commandPrefix = "!"; + +async function onMessage( + channel: string, + user: string, + message: string, + msg: TwitchPrivateMessage +): Promise { + if (msg.userInfo.isBroadcaster && message.startsWith(commandPrefix)) { + message = message.substring(commandPrefix.length); + + const args = message.split(" "); + const commandName = args.shift(); + + switch (commandName) { + case ChatCommands.Commands: + await say( + channel, + `Comandos disponibles: "${Object.values(ChatCommands).join('", "')}"` + ); + break; + default: + console.log( + `${LOG_PREFIX}Command ${commandPrefix}${commandName} not handled` + ); + } + } +} diff --git a/src/enums/ChatCommands.ts b/src/enums/ChatCommands.ts new file mode 100644 index 0000000..21bec92 --- /dev/null +++ b/src/enums/ChatCommands.ts @@ -0,0 +1,3 @@ +export enum ChatCommands { + Commands = "commands", +}