Inform about commands not found
This commit is contained in:
parent
7e080f2c99
commit
41d6099d89
9 changed files with 68 additions and 55 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue