Inform about commands not found

This commit is contained in:
Ken-Håvard Lieng 2017-05-22 03:49:37 +02:00
parent 7e080f2c99
commit 41d6099d89
9 changed files with 68 additions and 55 deletions

View file

@ -1,17 +1,28 @@
import { inform } from '../actions/message';
const notFound = 'commandNotFound';
function createContext({ dispatch, getState }, { server, channel }) {
return { dispatch, getState, server, channel };
}
export default function createCommandMiddleware(type, handlers) {
return ({ dispatch, getState }) => next => action => {
return store => next => action => {
if (action.type === type) {
const words = action.command.slice(1).split(' ');
const command = words[0];
const params = words.slice(1);
let result;
if (command in handlers) {
handlers[command]({
dispatch,
getState,
server: action.server,
channel: action.channel
}, ...params);
result = handlers[command](createContext(store, action), ...params);
} else if (notFound in handlers) {
result = handlers[notFound](createContext(store, action), command);
}
if (typeof result === 'string' || Array.isArray(result)) {
store.dispatch(inform(result, action.server, action.channel));
}
}