Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Merge branch 'ioni' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Talbot committed Jun 5, 2018
2 parents 9de2563 + 1ed3eb9 commit 3bbab61
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 3,354 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ Cache avec Redis ? (ex : liste des 1000 derniers utilisateurs)
* decr le nb morts ("deathcounterdecr")
* set le nb morts ("deathcounterset X")
* demander le nb morts ("deathcounter")
* "!last" pour avoir les dernières connexions viewers
* ~~"!last" pour avoir les dernières connexions viewers~~
* ~~"!game" pour avoir le nom du jeu actuel~~
* ~~"!title" pour le titre du stream + nom du jeu si en live~~
* "!playtime" pour le uptime du jeu actuel en live
* "!uptime" pour le uptime total du live
* ~~"!uptime" pour le uptime total du live~~
* "!pause" pour ne plus répondre aux commandes
* "!resume" pour enlever "pause"
* "!mute" pour ne pas envoyer de réponse
Expand Down
10 changes: 8 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
const TitleCommand = require('./lib/Command/TitleCommand');
const GameCommand = require('./lib/Command/GameCommand');
const LastCommand = require('./lib/Command/LastCommand');
const UptimeCommand = require('./lib/Command/UptimeCommand');
const UsersCommand = require('./lib/Command/UsersCommand');
const ModsCommand = require('./lib/Command/ModsCommand');
const TwitchConnectorIO = require('./lib/Connector/TwitchConnectorIO');
const StaticCommandRepository = require('./lib/Database/Repository/StaticCommandRepository');
const UserRepository = require('./lib/Database/Repository/UserRepository');
Expand All @@ -36,14 +39,14 @@
await dbManager.init();
const staticCommandRepo = new StaticCommandRepository(dbManager);
const userRepo = new UserRepository(dbManager);
const streamInfoRepository = new StreamInfoRepository(dbManager);
const streamInfoRepo = new StreamInfoRepository(dbManager);

const cacheManager = new CacheManager(logger);
await cacheManager.init();

const webhookServer = new WebhookServer(logger, 3000);
webhookServer.init();
const twitchWebhook = new TwitchWebhook(logger, webhookServer, cacheManager, streamInfoRepository);
const twitchWebhook = new TwitchWebhook(logger, webhookServer, cacheManager, streamInfoRepo);
await twitchWebhook.init();

const connectorManager = new ConnectorManager();
Expand All @@ -66,6 +69,9 @@
commandHandler.registerCommand(new TitleCommand(logger));
commandHandler.registerCommand(new GameCommand(logger));
commandHandler.registerCommand(new LastCommand(logger, userRepo));
commandHandler.registerCommand(new UptimeCommand(logger));
commandHandler.registerCommand(new UsersCommand(logger));
commandHandler.registerCommand(new ModsCommand(logger));

console.log(`Nozomibot is running... command "${scio.exitCommand}" for quit.`);

Expand Down
6 changes: 3 additions & 3 deletions lib/Command/Commands/ListCommandsCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = class StaticCommandsCommand extends Command {
}

if (!thereAreCommands) {
responseMessage = 'There are no commands.';
responseMessage = "Il n'y a aucune commande.";
}

commandExchange.connector.write(
Expand All @@ -64,7 +64,7 @@ module.exports = class StaticCommandsCommand extends Command {
if (command.isFake()) {
continue;
}
res += `${command.getFullName()}\r\n`;
res += `!${command.getFullName()}\n`;
}

return res;
Expand All @@ -77,7 +77,7 @@ module.exports = class StaticCommandsCommand extends Command {
});
if (commands) {
for (let command of commands) {
res += `${command.name}\r\n`;
res += `!${command.name}\n`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Commands/ListStaticCommandsCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = class ListStaticCommandsCommand extends Command {
responseMessage = 'There are no static commands.';
} else {
for (let command of commands) {
responseMessage += `${command.name}\r\n`;
responseMessage += `!${command.name}\n`;
}
}

Expand Down
45 changes: 45 additions & 0 deletions lib/Command/ModsCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Command = require('./Command');
const CommandResponse = require('./CommandResponse');
const TwitchConnectorIO = require('../Connector/TwitchConnectorIO');

module.exports = class UsersCommand extends Command {
constructor (logger) {
super('mods', logger);
}

usage () {
return `${this.name}`;
}

validate (commandExchange) {
return commandExchange.args._.length === 0;
}

getAvailableConnectors () {
return [TwitchConnectorIO];
}

async handle (commandExchange) {
this.logger.info('received');

const chatters = await commandExchange.connector.getChatters().catch(err => {
this.logger.error(`failed to get chatters : ${err}`);
});

let response = '';
if (chatters && chatters.moderators) {
response = chatters.moderators.join('\n');
} else {
response = "La liste des modos n'est pas disponible.";
}

commandExchange.connector.write(
new CommandResponse(
response,
'whisper',
commandExchange.context.userstate.username,
commandExchange.context
)
);
}
};
47 changes: 47 additions & 0 deletions lib/Command/UptimeCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const Command = require('./Command');
const CommandResponse = require('./CommandResponse');
const TwitchConnectorIO = require('../Connector/TwitchConnectorIO');
const moment = require('moment');

module.exports = class UptimeCommand extends Command {
constructor (logger) {
super('uptime', logger);
}

usage () {
return `${this.name}`;
}

validate (commandExchange) {
return commandExchange.args._.length === 0;
}

getAvailableConnectors () {
return [TwitchConnectorIO];
}

async handle (commandExchange) {
this.logger.info('received');

const streamInfo = await commandExchange.connector.getStreamInfo(process.env.TWITCH_CHANNEL_ID).catch(err => {
this.logger.error(`failed to get stream info : ${err}`);
});

let response = '';
if (streamInfo && streamInfo.started_at) {
const milliseconds = moment(new Date()).valueOf() - moment(streamInfo.started_at).valueOf();
response = `${Math.round(milliseconds / 1000 / 60)} minutes`;
} else {
response = "L'uptime n'est pas disponible.";
}

commandExchange.connector.write(
new CommandResponse(
response,
commandExchange.sourceType,
commandExchange.recipient,
commandExchange.context
)
);
}
};
45 changes: 45 additions & 0 deletions lib/Command/UsersCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Command = require('./Command');
const CommandResponse = require('./CommandResponse');
const TwitchConnectorIO = require('../Connector/TwitchConnectorIO');

module.exports = class UsersCommand extends Command {
constructor (logger) {
super('users', logger);
}

usage () {
return `${this.name}`;
}

validate (commandExchange) {
return commandExchange.args._.length === 0;
}

getAvailableConnectors () {
return [TwitchConnectorIO];
}

async handle (commandExchange) {
this.logger.info('received');

const chatters = await commandExchange.connector.getChatters().catch(err => {
this.logger.error(`failed to get chatters : ${err}`);
});

let response = '';
if (chatters && chatters.viewers) {
response = chatters.viewers.join('\n');
} else {
response = "La liste des viewers n'est pas disponible.";
}

commandExchange.connector.write(
new CommandResponse(
response,
'whisper',
commandExchange.context.userstate.username,
commandExchange.context
)
);
}
};
2 changes: 1 addition & 1 deletion lib/Connector/StandardConnectorIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class StandardConnectorIO extends ConnectorIO {
constructor (exitCommand, dbManager, cacheManager, webhookServer, logger) {
super();

this.logger = logger.child({subject: 'StandardConnectorIO'});
this.logger = logger.child({subject: this.constructor.name});
this.dbManager = dbManager;
this.cacheManager = cacheManager;
this.webhookServer = webhookServer;
Expand Down
18 changes: 17 additions & 1 deletion lib/Connector/TwitchConnectorIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module.exports = class TwitchConnectorIO extends ConnectorIO {
});
this.client.on('message', (channel, userstate, message, self) => {
if (self) return;
console.log(userstate);
this.onMessage(channel, userstate, message);
});
try {
Expand Down Expand Up @@ -229,8 +228,25 @@ module.exports = class TwitchConnectorIO extends ConnectorIO {
return gameInfo;
}

async getChatters () {
let chatters = await this.cacheManager.getObject('chatters');
if (!chatters) {
const body = await this._fetch(`https://tmi.twitch.tv/group/user/${process.env.TWITCH_CHANNEL.substr(1)}/chatters`);
if (!body.chatters) {
throw new Error(`chatters do not exist`);
}

chatters = body.chatters;
this.cacheManager.setObject('chatters', chatters, 60); // 1min
}

return chatters;
}

async write (commandResponse) {
let message = `${commandResponse.message}`;
// change "\n" in ", " because IRC does not interpret \n.
message = message.replace(/\n/g, ', ');
while (message.length > 0) {
// split into 499char-parts because IRC maxlength message
const subMessage = message.substr(0, 499);
Expand Down
2 changes: 0 additions & 2 deletions lib/Webhook/WebhookServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ module.exports = class WebhookServer {
if (this._handleRoute(parsedUrl, request, response, body)) {
return;
}
console.log(body, parsedUrl, headers);

response.statusCode = 404;
return response.end();
Expand All @@ -94,7 +93,6 @@ module.exports = class WebhookServer {
return;
}

console.log(body, parsedUrl, headers);
response.statusCode = 404;
return response.end();
});
Expand Down

0 comments on commit 3bbab61

Please sign in to comment.