From 1445b4f2c32bf0e483438de4d4cc71e5d53218df Mon Sep 17 00:00:00 2001 From: grouvie <20428462+grouvie@users.noreply.github.com> Date: Thu, 12 Jun 2025 00:01:21 +0200 Subject: [PATCH] Initial commit --- .env.example | 8 ++++++++ assets/configs/commands.json.example | 16 ++++++++++++++++ src/CBWebSocket.ts | 12 ++++++------ src/ChatBot.ts | 6 +++--- src/Messages/CommandMessage.ts | 10 +++++----- src/Messages/MessageFactory.ts | 8 ++++---- src/Messages/PrivateMessage.ts | 2 +- src/Messages/VoteMessage.ts | 4 ++-- test/Messages/CommandMessage.spec.ts | 10 +++++----- test/Messages/PrivateMessage.spec.ts | 4 ++-- test/Messages/VoteMessage.spec.ts | 6 +++--- 11 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 .env.example create mode 100644 assets/configs/commands.json.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f7e820b --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# Define the bot's username used for identification in Twitch chat +TWITCH_BOT_USERNAME = "some_bot_name" + +# Set the bot's OAuth token required for authenticating with the Twitch API +TWITCH_BOT_OAUTH_TOKEN = "some_bot_oauth_token" + +# Specify the name of the Twitch channel that the bot will join and interact with +TWITCH_CHANNEL_NAME = "some_twitch_channel" diff --git a/assets/configs/commands.json.example b/assets/configs/commands.json.example new file mode 100644 index 0000000..e04bacc --- /dev/null +++ b/assets/configs/commands.json.example @@ -0,0 +1,16 @@ +{ + "commands": [ + { + "name": "!hello", + "response": "Hello, world!", + "cooldownInSec": 5, + "scope": "user" + }, + { + "name": "!greet", + "response": "Hey there, ${sender}! Thanks for stopping by!", + "cooldownInSec": 10, + "scope": "global" + } + ] +} diff --git a/src/CBWebSocket.ts b/src/CBWebSocket.ts index 76688e2..e380239 100644 --- a/src/CBWebSocket.ts +++ b/src/CBWebSocket.ts @@ -1,7 +1,7 @@ -import WebSocket, {RawData} from "ws" -import {MessageFactory} from "./Messages/MessageFactory"; -import {VotingService} from "./Voting/VotingService"; -import {PrivateMessage} from "./Messages/PrivateMessage"; +import WebSocket, { RawData } from "ws"; +import { MessageFactory } from "./Messages/MessageFactory"; +import { PrivateMessage } from "./Messages/PrivateMessage"; +import { VotingService } from "./Voting/VotingService"; export class CBWebSocket { @@ -30,8 +30,8 @@ export class CBWebSocket { private registerListener() { this.client.on("open", () => { this.client.send(`CAP REQ :twitch.tv/commands`); - this.client.send(`PASS oauth:${process.env.TOKEN}`); - this.client.send(`NICK ${process.env.NICKNAME}`); + this.client.send(`PASS oauth:${process.env.TWITCH_BOT_OAUTH_TOKEN}`); + this.client.send(`NICK ${process.env.TWITCH_BOT_USERNAME}`); this.client.send(`JOIN #${this.channel}`); }); diff --git a/src/ChatBot.ts b/src/ChatBot.ts index 093c0f1..f968eae 100644 --- a/src/ChatBot.ts +++ b/src/ChatBot.ts @@ -1,5 +1,5 @@ -import {CBWebSocket} from "./CBWebSocket"; -import {ConfigStorage} from "./Config/ConfigStorage"; +import { CBWebSocket } from "./CBWebSocket"; +import { ConfigStorage } from "./Config/ConfigStorage"; export class ChatBot { private socket: CBWebSocket; @@ -11,6 +11,6 @@ export class ChatBot { console.error(error.message); process.exit(-1); } - this.socket = new CBWebSocket("thecodingbuddies"); + this.socket = new CBWebSocket(`${process.env.TWITCH_CHANNEL_NAME}`); } } diff --git a/src/Messages/CommandMessage.ts b/src/Messages/CommandMessage.ts index efdc6b4..c58dc13 100644 --- a/src/Messages/CommandMessage.ts +++ b/src/Messages/CommandMessage.ts @@ -1,7 +1,7 @@ -import {ConfigStorage} from "../Config/ConfigStorage"; -import {PlaceHolderTransformer} from "./PlaceHolderTransformer"; -import {Command} from "../Config/CommandParser"; -import {PrivateMessage} from "./PrivateMessage"; +import { Command } from "../Config/CommandParser"; +import { ConfigStorage } from "../Config/ConfigStorage"; +import { PlaceHolderTransformer } from "./PlaceHolderTransformer"; +import { PrivateMessage } from "./PrivateMessage"; export class CommandMessage implements Message { username: string; @@ -18,7 +18,7 @@ export class CommandMessage implements Message { this.author = this.username; this.channel = parts[2].slice(1); this.content = content.slice(1); - this.botName = process.env.NICKNAME; + this.botName = process.env.TWITCH_BOT_USERNAME; } catch (e) { throw new Error("Message incomplete"); } diff --git a/src/Messages/MessageFactory.ts b/src/Messages/MessageFactory.ts index cf39a0e..a945cad 100644 --- a/src/Messages/MessageFactory.ts +++ b/src/Messages/MessageFactory.ts @@ -1,7 +1,7 @@ -import {CommandMessage} from "./CommandMessage"; -import {PingMessage} from "./PingMessage"; -import {UnknownMessage} from "./UnknownMessage"; -import {VoteMessage} from "./VoteMessage"; +import { CommandMessage } from "./CommandMessage"; +import { PingMessage } from "./PingMessage"; +import { UnknownMessage } from "./UnknownMessage"; +import { VoteMessage } from "./VoteMessage"; const voteCommandIdentifier: string[] = [":!vote", ":!vote-start"]; diff --git a/src/Messages/PrivateMessage.ts b/src/Messages/PrivateMessage.ts index 3582a23..97a35e6 100644 --- a/src/Messages/PrivateMessage.ts +++ b/src/Messages/PrivateMessage.ts @@ -2,6 +2,6 @@ export class PrivateMessage { readonly content: string; constructor(message: string) { - this.content = `:${process.env.NICKNAME} PRIVMSG #${process.env.CHANNEL} :${message}`; + this.content = `:${process.env.TWITCH_BOT_USERNAME} PRIVMSG #${process.env.TWITCH_CHANNEL_NAME} :${message}`; } } diff --git a/src/Messages/VoteMessage.ts b/src/Messages/VoteMessage.ts index de082ba..36a7808 100644 --- a/src/Messages/VoteMessage.ts +++ b/src/Messages/VoteMessage.ts @@ -1,4 +1,4 @@ -import {VotingService} from "../Voting/VotingService"; +import { VotingService } from "../Voting/VotingService"; enum VoteType { START_DEFAULT_VOTE, @@ -58,7 +58,7 @@ export class VoteMessage implements Message { } const chatBotResponse: string = (this.sessionName !== "default") ? `${this.sessionName} started` : "started"; return (this.isStartVoteType()) - ? `:${process.env.NICKNAME} PRIVMSG #${process.env.CHANNEL} :Voting ${chatBotResponse}! Options are ${this.options}` + ? `:${process.env.TWITCH_BOT_USERNAME} PRIVMSG #${process.env.TWITCH_CHANNEL_NAME} :Voting ${chatBotResponse}! Options are ${this.options}` : ""; } diff --git a/test/Messages/CommandMessage.spec.ts b/test/Messages/CommandMessage.spec.ts index c1c2f37..d39aa49 100644 --- a/test/Messages/CommandMessage.spec.ts +++ b/test/Messages/CommandMessage.spec.ts @@ -1,7 +1,7 @@ -import {CommandMessage} from "../../src/Messages/CommandMessage"; -import {Command, CommandScope} from "../../src/Config/CommandParser"; -import {ConfigStorage} from "../../src/Config/ConfigStorage"; -import {CommandTimeoutList} from "../../src/Config/CommandTimeoutList"; +import { Command, CommandScope } from "../../src/Config/CommandParser"; +import { CommandTimeoutList } from "../../src/Config/CommandTimeoutList"; +import { ConfigStorage } from "../../src/Config/ConfigStorage"; +import { CommandMessage } from "../../src/Messages/CommandMessage"; const testCommandTimoutInSec: number = 2; const mockGetCommand = (): Command[] => { @@ -38,7 +38,7 @@ describe('CommandMessageTest', () => { let timeoutList: CommandTimeoutList; beforeEach(() => { - process.env.NICKNAME = "nickname"; + process.env.TWITCH_BOT_USERNAME = "nickname"; process.env.CHANNEL = "channel"; ConfigStorage.getCommands = mockGetCommand; timeoutList = new CommandTimeoutList(); diff --git a/test/Messages/PrivateMessage.spec.ts b/test/Messages/PrivateMessage.spec.ts index 58f28d7..595fe52 100644 --- a/test/Messages/PrivateMessage.spec.ts +++ b/test/Messages/PrivateMessage.spec.ts @@ -1,8 +1,8 @@ -import {PrivateMessage} from "../../src/Messages/PrivateMessage"; +import { PrivateMessage } from "../../src/Messages/PrivateMessage"; describe('PrivateMessage', () => { beforeEach(() => { - process.env.NICKNAME = 'botName'; + process.env.TWITCH_BOT_USERNAME = 'botName'; process.env.CHANNEL = 'ourChannel'; }) diff --git a/test/Messages/VoteMessage.spec.ts b/test/Messages/VoteMessage.spec.ts index 63d0589..8658ba7 100644 --- a/test/Messages/VoteMessage.spec.ts +++ b/test/Messages/VoteMessage.spec.ts @@ -1,10 +1,10 @@ -import {VoteMessage} from "../../src/Messages/VoteMessage"; -import {VotingService} from "../../src/Voting/VotingService"; +import { VoteMessage } from "../../src/Messages/VoteMessage"; +import { VotingService } from "../../src/Voting/VotingService"; describe('VoteMessage', () => { beforeEach(() => { - process.env.NICKNAME = "nickname"; + process.env.TWITCH_BOT_USERNAME = "nickname"; process.env.CHANNEL = "thecodingbuddies"; })