Move clientCommands to client

This approach automatically imports the command names.
This commit is contained in:
JeDaYoshi 2021-07-04 02:15:33 +00:00
parent 521426bb05
commit bbda392c3d
No known key found for this signature in database
GPG key ID: 8060B288C274219D
4 changed files with 20 additions and 35 deletions

View file

@ -6,6 +6,7 @@ import Mousetrap from "mousetrap";
import {Textcomplete, Textarea} from "textcomplete"; import {Textcomplete, Textarea} from "textcomplete";
import fuzzy from "fuzzy"; import fuzzy from "fuzzy";
import commands from "./commands/index";
import emojiMap from "./helpers/simplemap.json"; import emojiMap from "./helpers/simplemap.json";
import store from "./store"; import store from "./store";
@ -311,8 +312,19 @@ function completeNicks(word, isFuzzy) {
return users.filter((w) => !w.toLowerCase().indexOf(word)); return users.filter((w) => !w.toLowerCase().indexOf(word));
} }
function getCommands() {
const clientCommands = Object.keys(commands).map((cmd) => `/${cmd}`);
const cmds = [...new Set(Array.from(constants.commands).concat(clientCommands))];
if (!store.state.settings.searchEnabled) {
cmds.pop("/search");
}
return cmds.sort();
}
function completeCommands(word) { function completeCommands(word) {
const words = constants.commands.slice(); const words = getCommands().slice();
return fuzzyGrep(word, words); return fuzzyGrep(word, words);
} }

View file

@ -1,5 +1,3 @@
const clientSideCommands = ["/collapse", "/expand"];
const passThroughCommands = [ const passThroughCommands = [
"/as", "/as",
"/bs", "/bs",
@ -43,19 +41,12 @@ const userInputs = [
const pluginCommands = new Map(); const pluginCommands = new Map();
const getCommands = (client) => { const getCommands = () =>
const commands = Array.from(userInputs.keys()) Array.from(userInputs.keys())
.concat(Array.from(pluginCommands.keys())) .concat(Array.from(pluginCommands.keys()))
.map((command) => `/${command}`) .map((command) => `/${command}`)
.concat(clientSideCommands) .concat(passThroughCommands)
.concat(passThroughCommands); .sort();
if (client.messageProvider !== undefined) {
commands.push("/search");
}
return commands.sort();
};
const addPluginCommand = (packageInfo, command, func) => { const addPluginCommand = (packageInfo, command, func) => {
func.packageInfo = packageInfo; func.packageInfo = packageInfo;

View file

@ -692,7 +692,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
), ),
token: tokenToSend, token: tokenToSend,
}); });
socket.emit("commands", inputs.getCommands(client)); socket.emit("commands", inputs.getCommands());
}; };
if (Helper.config.public) { if (Helper.config.public) {

View file

@ -4,34 +4,16 @@ const expect = require("chai").expect;
const inputs = require("../../../src/plugins/inputs"); const inputs = require("../../../src/plugins/inputs");
describe("inputs", function () { describe("inputs", function () {
const client = {
messageProvider: undefined,
};
const clientWithProvider = {
...client,
messageProvider: true,
};
describe(".getCommands", function () { describe(".getCommands", function () {
it("should return a non-empty array", function () { it("should return a non-empty array", function () {
expect(inputs.getCommands(client)).to.be.an("array").that.is.not.empty; expect(inputs.getCommands()).to.be.an("array").that.is.not.empty;
}); });
it("should only return strings with no whitespaces and starting with /", function () { it("should only return strings with no whitespaces and starting with /", function () {
inputs.getCommands(client).forEach((command) => { inputs.getCommands().forEach((command) => {
expect(command).to.be.a("string").that.does.not.match(/\s/); expect(command).to.be.a("string").that.does.not.match(/\s/);
expect(command[0]).to.equal("/"); expect(command[0]).to.equal("/");
}); });
}); });
it("should not include /search without a message provider", function () {
expect(inputs.getCommands(client)).to.be.an("array").that.does.not.contains("/search");
});
it("should include /search with a message provider", function () {
expect(inputs.getCommands(clientWithProvider))
.to.be.an("array")
.that.contains("/search");
});
}); });
}); });