Skip to content

Commit

Permalink
fix(policies): interval & policies (#15)
Browse files Browse the repository at this point in the history
* fix(policies): interval & policies
* fix(logs): policies logs
  • Loading branch information
Ealenn authored Apr 4, 2021
1 parent 5ab6eda commit ba360bd
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 54 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Completely customizable Twitch Bot.
- [x] Execute commands
- [x] Simple answer
- [ ] Custom command
- [ ] Role-Based Access Control
- [x] Role-Based Access Control
- [ ] Interaction with custom APIs
- [ ] Interaction with twitch APIs
- [ ] Change Title
Expand Down Expand Up @@ -68,19 +68,22 @@ tools:
# !schedulers off
- type: schedulers
name: '!schedulers'
policies:
onlyMods: true # IMPORTANT
policies:
mod: true
admin: true
argOn: 'on'
argOff: 'off'
argStatus: 'status'
commands:
- name: '!facebook' # Command to write
random: true # Takes a random message from the list rather than following the order of the list
random: false # Takes a random message from the list rather than following the order of the list
policies:
onlyMods: true # Only moderators can run this command
others: true # All
messages:
- 'My Facebook is https://facebook.com/example'
- name: '!twitter'
policies:
others: true # All
messages:
- 'My Twitter is https://twitter.com/example'
schedulers:
Expand Down Expand Up @@ -118,6 +121,18 @@ events:
- 'I know someone from sub, but, I say anything, alright {{ Username }} ?'
```
### Policies
``` yaml
# DEFAULT POLICIES VALUES
policies:
mod: false
admin: false
vip: false
sub: false
others: false
```
## Docker Compose
```yaml
Expand Down
13 changes: 0 additions & 13 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,18 @@ export class TwitchConfiguration {
public Channel: string;
}

/**
* Schedulers Configuration
*/
export class SchedulersConfiguration {
public Enabled: boolean;
}

/**
* Provide Configuration
*/
export interface IConfiguration {
App: AppConfiguration;
Twitch: TwitchConfiguration;
Schedulers: SchedulersConfiguration;
}

@singleton()
export class Configuration implements IConfiguration {
public App: AppConfiguration;
public Twitch: TwitchConfiguration;
public Schedulers: SchedulersConfiguration;

constructor() {
// Application
Expand All @@ -54,9 +45,5 @@ export class Configuration implements IConfiguration {
Password: process.env.LARBIN_TWITCH_PASSWORD as string || '',
Channel: process.env.LARBIN_TWITCH_CHANNEL as string || ''
};

this.Schedulers = {
Enabled: true
};
}
}
2 changes: 1 addition & 1 deletion src/LarbinBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LarbinBot implements ILarbinBot {
const commands = this._yamlService.getCommands();
commands.forEach((command: ICommand) => {
this._twitchService.AddCommand(command);
this._loggerService.Debug(`Command ${command.Trigger} Added.`);
this._loggerService.Debug(`Command ${command.Trigger} Added.`, command.Policies);
});

// Schedulers
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Commands/Tools/SchedulersToolsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ export class SchedulersToolsCommand extends BaseCommand {
}

protected _statusAction(twitchService: ITwitchService): void {
const status = this._configuration.Schedulers.Enabled;
const status = twitchService.StatusSchedulers();
twitchService.Write(`The schedulers is ${status ? 'ON' : 'OFF'}`);
}

protected _onAction(twitchService: ITwitchService): void {
this._configuration.Schedulers.Enabled = true;
twitchService.StartSchedulers();
this._statusAction(twitchService);
}

protected _offAction(twitchService: ITwitchService): void {
this._configuration.Schedulers.Enabled = false;
twitchService.StopSchedulers();
this._statusAction(twitchService);
}
}
27 changes: 22 additions & 5 deletions src/lib/Commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ChatUserstate, Userstate } from 'tmi.js';
import { Configuration } from '../../Configuration';
import { ITwitchService } from '../../services/TwitchService';

/**
* Policies Command
*/
export class CommandPolicies {
OnlyMods = false;
public Admin = false;
public Mod = false;
public Vip = false;
public Sub = false;
public Others = false;
}

/**
Expand All @@ -15,7 +20,7 @@ export interface ICommand {
Trigger: string;
Policies: CommandPolicies;
Action(twitchService: ITwitchService, fullMessage: string, state: ChatUserstate): void;
CanAction(userState: Userstate): boolean;
CanAction(userState: Userstate, configuration: Configuration): boolean;
}

export abstract class BaseCommand implements ICommand {
Expand All @@ -35,10 +40,22 @@ export abstract class BaseCommand implements ICommand {

abstract Action(twitchService: ITwitchService, fullMessage: string, userState: ChatUserstate): void;
public CanAction(userState: Userstate): boolean {
if (this._policies.OnlyMods && !userState.mod) {
return false;
if (this._policies.Sub && userState.subscriber) {
return true;
}
return true;
if (this._policies.Vip && userState.badges?.vip) {
return true;
}
if (this._policies.Mod && userState.mod) {
return true;
}
if (this._policies.Admin && (userState.badges?.admin != null || userState.badges?.broadcaster != null)) {
return true;
}
if (this.Policies.Others) {
return this.Policies.Others;
}
return false;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/LoggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IConfiguration } from '../Configuration';
export interface ILoggerService {
Ascii(write: string): void;
Information(write: string): void;
Debug(write: string): void;
Debug(write: string, ...args: any): void;
Warning(write: string): void;
Error(write: string): void;
}
Expand All @@ -34,9 +34,9 @@ export class LoggerService implements ILoggerService {
console.info(`[INFO] ${write}`);
}

public Debug(write: string): void {
public Debug(write: string, ...args: any): void {
if (this._configuration.App.Debug){
console.debug(`[DEBUG] ${write}`);
console.debug(`[DEBUG] ${write}`, args);
}
}

Expand Down
35 changes: 29 additions & 6 deletions src/services/TwitchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IScheduler } from '../lib/Schedulers';
import { EventTypeParamsMapper } from '../mappers/EventTypeParamsMapper';
import { ILoggerService } from '.';
import { ITmiFactory } from '../factory/TmiFactory';
import { setInterval, clearInterval } from 'timers';

/**
* Provides all twitch tools
Expand All @@ -15,13 +16,19 @@ export interface ITwitchService {
Write(message: string): void;
AddCommand(command: ICommand): ITwitchService;
AddScheduler(scheduler: IScheduler): ITwitchService;
StartSchedulers(): void;
StopSchedulers(): void;
StatusSchedulers(): boolean;
AddEvent<T extends IEventParams>(event: IEvent<T>): ITwitchService;
Listen(): void;
}

@singleton()
export class TwitchService implements ITwitchService {
private _commands: Array<ICommand> = new Array<ICommand>();
private _schedulers: Array<IScheduler> = new Array<IScheduler>();
private _schedulersIntervals: Array<NodeJS.Timeout> = new Array<NodeJS.Timeout>();

private _loggerService: ILoggerService;
private _configuration: IConfiguration;
private _client: Client;
Expand All @@ -37,10 +44,11 @@ export class TwitchService implements ITwitchService {
}

public Listen(): void {
this.StartSchedulers();
this._client.on('message', (channels, userstate, message) => {
const command = this._commands.find((x) => message.startsWith(x.Trigger));
if (command != undefined) {
if (command.CanAction(userstate)) {
if (command.CanAction(userstate, this._configuration)) {
command.Action(this, message, userstate);
}
}
Expand All @@ -57,14 +65,29 @@ export class TwitchService implements ITwitchService {
}

public AddScheduler(scheduler: IScheduler): ITwitchService {
setInterval((s: IScheduler) => {
if (this._configuration.Schedulers.Enabled) {
s.Action(this);
}
}, scheduler.Minutes * 60000, scheduler);
this._schedulers.push(scheduler);
return this;
}

public StartSchedulers(): void {
this.StopSchedulers();
this._schedulers.forEach((scheduler) => {
const interval = setInterval((twitchService) => scheduler.Action(twitchService), scheduler.Minutes * 60000, this);
this._schedulersIntervals.push(interval);
})
}

public StopSchedulers(): void {
this._schedulersIntervals.forEach((intervalId: NodeJS.Timeout) => {
clearInterval(intervalId);
});
this._schedulersIntervals = new Array<NodeJS.Timeout>();
}

public StatusSchedulers(): boolean {
return this._schedulersIntervals.length > 0;
}

public AddEvent<T extends IEventParams>(event: IEvent<T>): ITwitchService {
this._client.on(event.Type, (...args: any[]) => {
event.Action(this, EventTypeParamsMapper(event.Type, args));
Expand Down
21 changes: 14 additions & 7 deletions src/services/YamlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ export class YamlService implements IYamlService {
}

protected _getCommandPolicies(policies: any): CommandPolicies {
const commandPolicies = new CommandPolicies();
if (policies && policies.onlyMods) {
commandPolicies.OnlyMods = policies.onlyMods === 'true';
const defaultPolicies = new CommandPolicies();
if (!policies) {
return defaultPolicies;
}
return commandPolicies;

return {
Admin: policies.admin ?? defaultPolicies.Admin,
Mod: policies.mod ?? defaultPolicies.Mod,
Vip: policies.vip ?? defaultPolicies.Vip,
Sub: policies.sub ?? defaultPolicies.Sub,
Others: policies.others ?? defaultPolicies.Others
} as CommandPolicies;
}
public getCommands(): Array<ICommand> {
const yamlContent = this.getYamlContent();
Expand All @@ -69,11 +76,11 @@ export class YamlService implements IYamlService {
}

yamlContent.commands?.forEach((element: any) => {
if (element.name && element.message) {
if (element.name && element.messages) {
if (element.random) {
commands.push(new RandomMessageCommand(element.name, this._getCommandPolicies(element.policies), element.message));
commands.push(new RandomMessageCommand(element.name, this._getCommandPolicies(element.policies), element.messages));
} else {
commands.push(new RoundRobinMessageCommand(element.name, this._getCommandPolicies(element.policies), element.message));
commands.push(new RoundRobinMessageCommand(element.name, this._getCommandPolicies(element.policies), element.messages));
}
}
});
Expand Down
Loading

0 comments on commit ba360bd

Please sign in to comment.