Add getActiveChannel

Simplifies the command logic
This commit is contained in:
Simon Ser 2021-06-03 10:18:34 +02:00
parent 9067b5a168
commit a2dc045683

View file

@ -9,16 +9,21 @@ function getActiveClient(app) {
return app.clients.get(buf.network); return app.clients.get(buf.network);
} }
function getActiveChannel(app) {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
throw new Error("Not in a channel");
}
return activeBuffer.name;
}
function ban(app, args) { function ban(app, args) {
var nick = args[0]; var nick = args[0];
if (!nick) { if (!nick) {
throw new Error("Missing nick"); throw new Error("Missing nick");
} }
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { var params = [activeChannel, nick];
throw new Error("Not in a channel");
}
var params = [activeBuffer.name, nick];
if (args.length > 1) { if (args.length > 1) {
params.push(args.slice(1).join(" ")); params.push(args.slice(1).join(" "));
} }
@ -28,7 +33,7 @@ function ban(app, args) {
const user = info[2]; const user = info[2];
const host = info[3]; const host = info[3];
client.send({ command: "MODE", params: [ client.send({ command: "MODE", params: [
activeBuffer.name, activeChannel,
"+b", "+b",
`*!${user}@${host}` `*!${user}@${host}`
]}); ]});
@ -52,11 +57,8 @@ const kick = {
description: "Remove a user from the channel", description: "Remove a user from the channel",
execute: (app, args) => { execute: (app, args) => {
var nick = args[0]; var nick = args[0];
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { var params = [activeChannel, nick];
throw new Error("Not in a channel");
}
var params = [activeBuffer.name, nick];
if (args.length > 1) { if (args.length > 1) {
params.push(args.slice(1).join(" ")); params.push(args.slice(1).join(" "));
} }
@ -70,13 +72,11 @@ function givemode(app, args, mode) {
if (!nick) { if (!nick) {
throw new Error("Missing nick"); throw new Error("Missing nick");
} }
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { getActiveClient(app).send({
throw new Error("Not in a channel"); command: "MODE",
} params: [activeChannel, mode, nick],
getActiveClient(app).send({ command: "MODE", params: [ });
activeBuffer.name, mode, nick,
]});
} }
export default { export default {
@ -85,13 +85,10 @@ export default {
description: "Bans a user from the channel, or displays the current ban list", description: "Bans a user from the channel, or displays the current ban list",
execute: (app, args) => { execute: (app, args) => {
if (args.length == 0) { if (args.length == 0) {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
throw new Error("Not in a channel");
}
getActiveClient(app).send({ getActiveClient(app).send({
command: "MODE", command: "MODE",
params: [activeBuffer.name, "+b"], params: [activeChannel, "+b"],
}); });
} else { } else {
return ban(app, args); return ban(app, args);
@ -152,12 +149,9 @@ export default {
if (!nick) { if (!nick) {
throw new Error("Missing nick"); throw new Error("Missing nick");
} }
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
throw new Error("Not in a channel");
}
getActiveClient(app).send({ command: "INVITE", params: [ getActiveClient(app).send({ command: "INVITE", params: [
nick, activeBuffer.name, nick, activeChannel,
]}); ]});
}, },
}, },
@ -184,12 +178,9 @@ export default {
description: "Send an action message to the current buffer", description: "Send an action message to the current buffer",
execute: (app, args) => { execute: (app, args) => {
var action = args.join(" "); var action = args.join(" ");
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer) {
throw new Error("Not in a buffer");
}
var text = `\x01ACTION ${action}\x01`; var text = `\x01ACTION ${action}\x01`;
app.privmsg(activeBuffer.name, text); app.privmsg(activeChannel, text);
}, },
}, },
"mode": { "mode": {
@ -198,11 +189,8 @@ export default {
execute: (app, args) => { execute: (app, args) => {
var target = args[0]; var target = args[0];
if (target.startsWith("+") || target.startsWith("-")) { if (target.startsWith("+") || target.startsWith("-")) {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { args = [activeChannel, ...args];
throw new Error("Not in a channel");
}
args = [activeBuffer.name, ...args];
} }
getActiveClient(app).send({ command: "MODE", params: args }); getActiveClient(app).send({ command: "MODE", params: args });
}, },
@ -250,11 +238,8 @@ export default {
description: "Leave a channel", description: "Leave a channel",
execute: (app, args) => { execute: (app, args) => {
var reason = args.join(" "); var reason = args.join(" ");
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { var params = [activeChannel];
throw new Error("Not in a channel");
}
var params = [activeBuffer.name];
if (reason) { if (reason) {
params.push(reason); params.push(reason);
} }
@ -328,11 +313,8 @@ export default {
usage: "<topic>", usage: "<topic>",
description: "Change the topic of the current channel", description: "Change the topic of the current channel",
execute: (app, args) => { execute: (app, args) => {
var activeBuffer = app.state.buffers.get(app.state.activeBuffer); var activeChannel = getActiveChannel(app);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) { var params = [activeChannel];
throw new Error("Not in a channel");
}
var params = [activeBuffer.name];
if (args.length > 0) { if (args.length > 0) {
params.push(args.join(" ")); params.push(args.join(" "));
} }