Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(events): Twitch Events #2

Merged
merged 4 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Completely customizable Twitch Bot.
- [ ] Predictions
- [ ] Event-based Action
- [ ] Simple answer
- [ ] raided
- [x] join
- [x] raided
- [ ] resub
- [ ] submysterygift
- [ ] subgift
Expand Down Expand Up @@ -59,4 +60,24 @@ schedulers:
- 'Text rolling 1'
- 'Text rolling 2'
- 'Text rolling 3'
events:
- name: 'join'
messages:
- 'Less noise {{ Username }} is coming!'
- 'Ah! We are talking about you {{ Username }} !'
- name: 'raided'
messages:
- 'Thanks to @{{ Username }} for this raid of {{ Viewers }} viewers !'
- name: 'resub'
messages:
- 'Thanks {{ Username }} for your {{ Months }} with us ! -- {{ Username }} say: {{ Message }}'
- name: 'submysterygift'
messages:
- '{{ Username }} is rich and he just offered {{ OfferedSubs }} subscription! Thank him in the chat! (with a total of {{ GiftCount }} subscription offered)'
- name: 'subgift'
messages:
- 'Hey ! {{ Username }} is {{ GiftCount }}x more generous with {{ RecipientUsername }} !'
- name: 'subscription'
messages:
- 'I know someone from sub, but, I say anything, alright {{ Username }} ?'
```
34 changes: 30 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"repository": "https://github.com/Ealenn/LarbinBot",
"dependencies": {
"figlet": "^1.5.0",
"handlebars": "^4.7.7",
"reflect-metadata": "^0.1.13",
"tmi.js": "^1.5.0",
"tsyringe": "^4.5.0",
Expand Down
6 changes: 3 additions & 3 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class Configuration implements IConfiguration {

// Twitch
this.Twitch = {
Username: process.env.TWITCH_USERNAME as string || '',
Password: process.env.TWITCH_PASSWORD as string || '',
Channel: process.env.TWITCH_CHANNEL as string || ''
Username: process.env.LARBIN_TWITCH_USERNAME as string || '',
Password: process.env.LARBIN_TWITCH_PASSWORD as string || '',
Channel: process.env.LARBIN_TWITCH_CHANNEL as string || ''
};
}
}
14 changes: 11 additions & 3 deletions src/LarbinBot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { inject, injectable } from 'tsyringe';
import { IConfiguration } from './Configuration';
import { WriterCommand } from './lib/Commands/WriterCommand';
import { WriterScheduler } from './lib/Schedulers/WriterScheduler';
import { WriterCommand } from './lib/Commands';
import { IEvent, IEventParams } from './lib/Events';
import { IScheduler } from './lib/Schedulers';
import { ILoggerService } from './services/LoggerService';
import { ITwitchService } from './services/TwitchService';
import { IYamlService } from './services/YamlService';
Expand Down Expand Up @@ -44,11 +45,18 @@ export class LarbinBot implements ILarbinBot {

// Schedulers
const schedulers = this._yamlService.getSchedulers();
schedulers.forEach((scheduler: WriterScheduler) => {
schedulers.forEach((scheduler: IScheduler) => {
this._twitchService.AddScheduler(scheduler);
this._loggerService.Debug(`Scheduler ${scheduler.Id} every ${scheduler.Minutes} minutes Added.`);
});

// Events
const events = this._yamlService.getEvents();
events.forEach((event: IEvent<IEventParams>) => {
this._twitchService.AddEvent(event);
this._loggerService.Debug(`Event ${event.Type} Added.`);
});

// Start
this._twitchService.Listen();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Commands/WriterCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ICommand } from '.';
import { ITwitchService } from '../../services/TwitchService';

/**
* Simple Writer Command
* Writer Command
*/
export class WriterCommand implements ICommand {
private _trigger: string;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { ChatUserstate } from 'tmi.js';
import { ITwitchService } from '../../services/TwitchService';

/**
* Base Command
*/
export interface ICommand {
Trigger: string;
Action(twitchService: ITwitchService, state: ChatUserstate): void;
}

/**
* Commands
*/
export * from './WriterCommand'
26 changes: 26 additions & 0 deletions src/lib/Events/RandomMessageEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EventType, IEvent, IEventParams } from '.';
import Handlebars from 'handlebars';
import { ITwitchService } from '../../services/TwitchService';

export class RandomMessageEvent<T extends IEventParams> implements IEvent<T> {
public Type: EventType;
private _messages: Array<string>;

constructor(type: EventType, messages: Array<string>)
{
this.Type = type;
this._messages = messages;
}

protected getMessage() : string {
return this._messages[Math.floor(Math.random() * this._messages.length)];
}

public Action(twitchService: ITwitchService, params: T): void {
const message = this.getMessage();
const Template = Handlebars.compile(message);
twitchService.Write(
Template(params)
);
}
}
72 changes: 72 additions & 0 deletions src/lib/Events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ITwitchService } from '../../services/TwitchService';

/**
* Event Type
*/
export enum EventType {
RAIDED = 'raided',
JOIN = 'join',
RESUB = 'resub',
SUBMYSTERYGIFT = 'submysterygift',
SUBGIFT = 'subgift',
SUBSCRIPTION = 'subscription'
}

/**
* Base Event
*/
export interface IEvent<T extends IEventParams> {
Type: EventType;
Action(twitchService: ITwitchService, params: T): void;
}

/**
* Event Params
*/
export interface IEventParams {
Channel: string;
}

export class RaidedEventParams implements IEventParams {
Channel: string;
Username: string;
Viewers: number;
}

export class JoinEventParams implements IEventParams {
Channel: string;
Username: string;
}

export class ResubEventParams implements IEventParams {
Channel: string;
Username: string;
Months: number;
Message: string;
}

export class SubGiftEventParams implements IEventParams {
Channel: string;
Username: string;
StreakMonths: number;
RecipientUsername: string;
GiftCount: number;
}

export class SubMysteryGiftEventParams implements IEventParams {
Channel: string;
Username: string;
OfferedSubs: number;
GiftCount: number;
}

export class SubscriptionEventParams implements IEventParams {
Channel: string;
Username: string;
Message: string;
}

/**
* Events
*/
export * from './RandomMessageEvent';
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { IScheduler } from '.';
import { ITwitchService } from '../../services/TwitchService';

/**
* Simple Writer Scheduler
* Round Robin Scheduler
*/
export class WriterScheduler implements IScheduler {
export class RoundRobinScheduler implements IScheduler {
private _id: string;
public get Id(): string {
return this._id;
Expand All @@ -26,7 +26,7 @@ export class WriterScheduler implements IScheduler {
this._messages = messages;
}

private getMessage(): string {
protected getMessage(): string {
const message = this._messages[this._messageIndex];
this._messageIndex = this._messageIndex + 1;
if (this._messageIndex >= this._messages.length) {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Schedulers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { ITwitchService } from '../../services/TwitchService';

/**
* Base Scheduler
*/
export interface IScheduler {
Id: string;
Minutes: number;
Action(twitchService: ITwitchService): void;
}

/**
* Schedulers
*/
export * from './RoundRobinScheduler';
59 changes: 59 additions & 0 deletions src/mappers/EventTypeParamsMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { EventType, IEventParams, ResubEventParams } from '../lib/Events';
import { JoinEventParams, RaidedEventParams, SubMysteryGiftEventParams, SubscriptionEventParams } from '../lib/Events';

/**
* Convert tmi.js args to EventTypeParams T
* https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md
* @param eventType
* @param args
* @returns T
*/
export function EventTypeParamsMapper<T extends IEventParams>(eventType: EventType, args: any[]): T {
let params = {
Channel: args[0] as string
};

switch (eventType) {
case EventType.JOIN:
params = Object.assign(params, {
Username: args[1] as string
} as JoinEventParams);
break;
case EventType.RAIDED:
params = Object.assign(params, {
Username: args[1] as string,
Viewers: args[2] as number
} as RaidedEventParams);
break;
case EventType.RESUB:
params = Object.assign(params, {
Username: args[1],
Months: args[2],
Message: args[3]
}) as ResubEventParams;
break;
case EventType.SUBGIFT:
params = Object.assign(params, {
Username: args[1],
StreakMonths: args[2],
RecipientUsername: args[3],
GiftCount: args[5]['msg-param-sender-count']
});
break;
case EventType.SUBMYSTERYGIFT:
params = Object.assign(params, {
Username: args[1],
OfferedSubs: args[2],
GiftCount: args[3]
}) as SubMysteryGiftEventParams;
break;
case EventType.SUBSCRIPTION:
params = Object.assign(params, {
Username: args[1],
Message: args[3]
} as SubscriptionEventParams);
break;
}

return params as T;
}