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

Make mutelinks auto disable when destiny goes offline. #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ services
logger.error('Config property: "chatToConnectTo" not set to one of "dgg" or "twitch"');
process.exit(1);
}
if(config.hasOwnProperty('scheduledCommands')){
config.scheduledCommands.forEach(commandToSchedule => services.fakeScheduler.createMessage(commandToSchedule));
if (config.hasOwnProperty('scheduledCommands')) {
config.scheduledCommands.forEach((commandToSchedule) =>
services.fakeScheduler.createMessage(commandToSchedule),
);
}

const chatServiceRouter = new ChatServiceRouter(
Expand All @@ -66,10 +68,7 @@ services
messageRouter,
commandRouter,
logger,
services.punishmentStream,
services.scheduledCommands,
services.fakeScheduler,
services.messageRelay,
services,
);
chatServiceRouter.create();
})
Expand Down
11 changes: 9 additions & 2 deletions lib/configuration/sample.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"cookieToken": "yourCookieToken",
"botNick": "yourBotNick"
},
"scheduledCommands": ["!youtube", "!schedule"],
"destinyLive": {
"url": "ws://localhost:6969",
"origin": "www.obamna.gg"
},
"scheduledCommands": [
"!youtube",
"!schedule"
],
"logger": {
"level": "debug"
},
Expand Down Expand Up @@ -134,4 +141,4 @@
"threadFilePath": "",
"stateStoreFilePath": ""
}
}
}
121 changes: 63 additions & 58 deletions lib/message-routing/chat-service-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
const _ = require('lodash');

class ChatServiceRouter {
constructor(
chatToConnectTo,
bot,
messageRouter,
commandRouter,
logger,
punishmentStream,
messageSchedulerStream,
fakeScheduler,
messageRelay,
) {
constructor(chatToConnectTo, bot, messageRouter, commandRouter, logger, services) {
this.messageRouter = messageRouter;
this.commandRouter = commandRouter;
this.chatToConnectTo = chatToConnectTo;
this.logger = logger;
this.bot = bot;
this.punishmentStream = punishmentStream;
this.messageSchedulerStream = messageSchedulerStream;
this.fakeScheduler = fakeScheduler;
this.punishmentStream = services.punishmentStream;
this.messageSchedulerStream = services.scheduledCommands;
this.fakeScheduler = services.fakeScheduler;
// TODO just refactor other things to use the message relay
this.messageRelay = messageRelay;
this.messageRelay = services.messageRelay;
this.destinyLive = services.destinylive;
}

create() {
Expand Down Expand Up @@ -50,49 +41,22 @@ class ChatServiceRouter {
this.bot.on('command', (commandObject) => {
this.commandRouter
.routeIncomingCommandMessage(commandObject)
.then((outputObject) => {
if (_.isEmpty(outputObject.output)) {
return;
}

if (outputObject.err) {
this.logger.error(
'Purposeful error thrown by command',
commandObject,
outputObject.err,
);
if (_.isString(outputObject.output)) {
if (outputObject.isWhisper) {
this.bot.sendWhisper(outputObject.user, outputObject.output);
} else {
this.bot.sendMessage(outputObject.output);
}
}
return;
}

if (
this.chatToConnectTo === 'dgg' &&
outputObject.isMultiLine &&
outputObject.isWhisper === false
) {
this.bot.sendMultiLine(outputObject.output);
} else if (
this.chatToConnectTo === 'twitch' &&
outputObject.isMultiLine &&
outputObject.isWhisper === false
) {
// There's no good way to do multilines in twitch atm? :C
this.bot.sendMessage(outputObject.output.join(' '));
} else if (outputObject.isWhisper === true) {
this.bot.sendWhisper(outputObject.user, outputObject.output);
} else {
this.bot.sendMessage(outputObject.output);
}
})
.catch((err) => {
this.logger.error('Got an error while parsing command: ', err);
});
.then((outputObject) => this.handleCommandReturn(outputObject, commandObject))
.catch(this.handleCommandError);
});

this.destinyLive.connect();
this.destinyLive.on('offline', () => {
// Turn mutelinks off.
const commandObject = {
command: '!mutelinks',
input: 'off',
parsedMessage: null,
};
this.commandRouter
.backgroundRunCommand(commandObject)
.then((outputObject) => this.handleCommandReturn(outputObject, commandObject))
.catch(this.handleCommandError);
});

this.messageSchedulerStream.on('command', (commandObject) => {
Expand Down Expand Up @@ -142,6 +106,47 @@ class ChatServiceRouter {
}
});
}

handleCommandReturn(outputObject, commandObject) {
if (_.isEmpty(outputObject.output)) {
return;
}

if (outputObject.err) {
this.logger.error('Purposeful error thrown by command', commandObject, outputObject.err);
if (_.isString(outputObject.output)) {
if (outputObject.isWhisper) {
this.bot.sendWhisper(outputObject.user, outputObject.output);
} else {
this.bot.sendMessage(outputObject.output);
}
}
return;
}

if (
this.chatToConnectTo === 'dgg' &&
outputObject.isMultiLine &&
outputObject.isWhisper === false
) {
this.bot.sendMultiLine(outputObject.output);
} else if (
this.chatToConnectTo === 'twitch' &&
outputObject.isMultiLine &&
outputObject.isWhisper === false
) {
// There's no good way to do multilines in twitch atm? :C
this.bot.sendMessage(outputObject.output.join(' '));
} else if (outputObject.isWhisper === true) {
this.bot.sendWhisper(outputObject.user, outputObject.output);
} else {
this.bot.sendMessage(outputObject.output);
}
}

handleCommandError(err) {
this.logger.error('Got an error while parsing command: ', err);
}
}

module.exports = ChatServiceRouter;
25 changes: 22 additions & 3 deletions lib/message-routing/command-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ class CommandRouter {
return false;
}

backgroundRunCommand(commandObject) {
const commandFunction = this.commandRegister.findCommand(commandObject.command);
if (commandFunction === false) return Promise.resolve(false);

if (
_.isObject(commandFunction.inputValidator) &&
!commandFunction.inputValidator.test(commandObject.input)
) {
return Promise.resolve(false);
}

return this.runCommand(
commandFunction,
commandObject.input,
commandObject.parsedMessage,
false,
);
}

runCommand(command, input, parsedMessage, isWhisper) {
return new Promise((accept, reject) => {
if (command.isPromiseOutput) {
Expand All @@ -101,14 +120,14 @@ class CommandRouter {
.then((outputObject) => {
if (outputObject.err !== null) {
return accept({
user: parsedMessage.user,
user: parsedMessage?.user,
isWhisper,
err: outputObject.err,
output: outputObject.output,
});
}
return accept({
user: parsedMessage.user,
user: parsedMessage?.user,
isWhisper,
output: outputObject.output,
isMultiLine: command.multiLineCommand,
Expand All @@ -125,7 +144,7 @@ class CommandRouter {
try {
const staticOutputObject = command.work(input, this.services, parsedMessage);
return accept({
user: parsedMessage.user,
user: parsedMessage?.user,
output: staticOutputObject.output,
isMultiLine: command.multiLineCommand,
isWhisper,
Expand Down
61 changes: 61 additions & 0 deletions lib/services/destinylive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const EventEmitter = require('events');
const WebSocket = require('ws');

class DestinyLive extends EventEmitter {
constructor(config, services) {
super();
this.logger = services.logger;
this.commandRegistry = services.commandRegistry;
this.url = config.url;
this.origin = config.origin;
this.cache = {};
}

connect() {
this.ws = new WebSocket(this.url, {
origin: this.origin,
});
this.ws.onopen = this.onOpen.bind(this);
this.ws.onclose = this.onClose.bind(this);
this.ws.onmessage = this.parseMessages.bind(this);
this.ws.onerror = this.handleError.bind(this);
}

onOpen() {
this.logger.info('Destiny Live socket opened.');
}

onClose() {
this.logger.info('Destiny Live socket closed. Attempting to reconnect....');
setTimeout(() => {
this.connect();
}, 5000);
}

parseMessages(message) {
try {
const event = JSON.parse(message.data);

if (event.type === 'dggApi:streamInfo' && this.cache[event.type]) {
const wasLive = this.cache[event.type].some((s) => s?.live);
const isLive = event.data.some((s) => s?.live);
if (wasLive && !isLive) {
this.emit('offline');
} else if (!wasLive && isLive) {
this.emit('online');
}
}

// cache message
this.cache[event.type] = event.data;
} catch (error) {
this.logger.info('Destiny Live socket message parse error: ', error);
}
}

handleError(error) {
this.logger.info('Destiny Live socket recieved error: ', error.message);
}
}

module.exports = DestinyLive;
8 changes: 5 additions & 3 deletions lib/services/service-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ const SpamDetection = require('./spam-detection');
const ScheduledCommands = require('./message-scheduler');
const gulagService = require('./gulag');
const LastFm = require('./lastfm');
const YouTube = require('./youtube.js');
const GoogleCal = require('./schedule.js');
const RoleCache = require('./role-cache.js');
const YouTube = require('./youtube');
const GoogleCal = require('./schedule');
const RoleCache = require('./role-cache');
const DggApi = require('./dgg-api');
const TwitterApi = require('./twitter-api');
const FakeScheduler = require('./fake-command-scheduler');
const RedditVote = require('./reddit-vote');
const MessageRelay = require('./message-relay');
const messageMatchingService = require('./message-matching');
const HTMLMetadata = require('./html-metadata');
const DestinyLive = require('./destinylive');

class Services {
constructor(serviceConfigurations, chatConnectedTo) {
Expand All @@ -42,6 +43,7 @@ class Services {
this.schedule = new GoogleCal(serviceConfigurations.googleCalendar);
this.fakeScheduler = new FakeScheduler(serviceConfigurations.schedule);
this.dggApi = new DggApi(serviceConfigurations.dggApi, this.logger);
this.destinylive = new DestinyLive(serviceConfigurations.destinyLive, this);
this.twitterApi = new TwitterApi(serviceConfigurations.twitter, this.logger);
this.messageRelay = new MessageRelay();
this.htmlMetadata = new HTMLMetadata();
Expand Down