Implement the advanced Nightbot command
This commit is contained in:
parent
10983cec6f
commit
78200a680d
1 changed files with 141 additions and 0 deletions
141
src/endpoints/nightbot/advanced.ts
Normal file
141
src/endpoints/nightbot/advanced.ts
Normal file
|
|
@ -0,0 +1,141 @@
|
||||||
|
import { nightbotCustomHeadersSchema } from "$/schemas/nightbot";
|
||||||
|
import { ServerRoute, Request } from "@hapi/hapi";
|
||||||
|
import { database } from "$/main";
|
||||||
|
import boom from "@hapi/boom";
|
||||||
|
import Joi from "joi";
|
||||||
|
|
||||||
|
function subtractHandler(request: Request, meta: subcommandMetadata, args: string[]) {
|
||||||
|
// args: !cmd sub <counter:string> [delta:integer]
|
||||||
|
// args: !cmd remove <counter:string> [delta:integer]
|
||||||
|
let { channel } = meta;
|
||||||
|
let counter = args[1].toLowerCase();
|
||||||
|
let delta = args[2] ? parseInt(args[2]) : 1;
|
||||||
|
|
||||||
|
if (!database.counterExists(channel, counter)) {
|
||||||
|
throw new Error(`No counter exists with that name`);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: database.changeCount(channel, counter, -delta),
|
||||||
|
message: `value=$(count)`
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
interface subcommandMetadata {
|
||||||
|
channel: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const subcommands: {[index: string]: any} = {
|
||||||
|
"add": {
|
||||||
|
argc: 1,
|
||||||
|
handler(request: Request, meta: subcommandMetadata, args: string[]) {
|
||||||
|
// args: !cmd add <counter:string> [delta:integer]
|
||||||
|
let { channel } = meta;
|
||||||
|
let counter = args[1].toLowerCase();
|
||||||
|
let delta = args[2] ? parseInt(args[2]) : 1;
|
||||||
|
|
||||||
|
if (!database.counterExists(channel, counter)) {
|
||||||
|
throw new Error(`No counter exists with that name`);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: database.changeCount(channel, counter, delta),
|
||||||
|
message: `value=$(count)`
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"sub": { argc: 1, handler: subtractHandler, },
|
||||||
|
"remove": { argc: 1, handler: subtractHandler, },
|
||||||
|
"new": {
|
||||||
|
argc: 1,
|
||||||
|
handler(request: Request, meta: subcommandMetadata, args: string[]) {
|
||||||
|
// args: !cmd new <counter:string>
|
||||||
|
let { channel } = meta;
|
||||||
|
let counter = args[1].toLowerCase();
|
||||||
|
database.addCounter(channel, counter);
|
||||||
|
return {
|
||||||
|
message: `Counter ${counter} created successfully.`,
|
||||||
|
value: 0
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
argc: 0,
|
||||||
|
handler(request: Request, meta: subcommandMetadata, args: string[]) {
|
||||||
|
// args: !cmd <counter:string> [delta:number]
|
||||||
|
let { channel } = meta;
|
||||||
|
let counter = args[0].toLowerCase();
|
||||||
|
let delta = args[1] ? parseInt(args[1]) : 1;
|
||||||
|
|
||||||
|
if (!database.counterExists(channel, counter)) {
|
||||||
|
throw new Error(`No counter exists with that name`);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: database.changeCount(channel, counter, delta),
|
||||||
|
message: `value=$(count)`
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const route: ServerRoute = {
|
||||||
|
method: `GET`, path: `/nightbot.advanced`,
|
||||||
|
options: {
|
||||||
|
validate: {
|
||||||
|
query: Joi.object({
|
||||||
|
args: Joi.string().allow("").required(),
|
||||||
|
msg: Joi.string(),
|
||||||
|
}),
|
||||||
|
headers: nightbotCustomHeadersSchema.unknown(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async handler(request) {
|
||||||
|
let args = (<string>request.query.args)
|
||||||
|
.split(` `)
|
||||||
|
.filter(x => x.length > 0);
|
||||||
|
|
||||||
|
let sc = subcommands[args[0]] ?? subcommands.default;
|
||||||
|
|
||||||
|
if (args.length < sc.argc + 1) {
|
||||||
|
return `Not enough arguments`;
|
||||||
|
};
|
||||||
|
|
||||||
|
let channelData = new URLSearchParams(request.headers["nightbot-channel"]);
|
||||||
|
let channel = channelData.get(`name`);
|
||||||
|
let userData = new URLSearchParams(request.headers["nightbot-user"]);
|
||||||
|
let user = userData.get(`name`);
|
||||||
|
|
||||||
|
|
||||||
|
if (!channel) { throw boom.badData(`Missing channel name`) };
|
||||||
|
if (!user) { throw boom.badData(`Missing user name`) };
|
||||||
|
|
||||||
|
|
||||||
|
if (!database.channelExists(channel)) {
|
||||||
|
return `The channel isn't setup to use the counter system`;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (database.isUserIgnored(channel, user)) { return `Invalid permissions`; };
|
||||||
|
|
||||||
|
console.log(`[${channel}] ${user} is running command with args: ${args}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
var { message, value } = sc.handler(
|
||||||
|
request,
|
||||||
|
{ channel, user },
|
||||||
|
args
|
||||||
|
);
|
||||||
|
} catch (e: any) {
|
||||||
|
return e.message;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (message) {
|
||||||
|
return message.replace(/\$\(count\)/g, value);
|
||||||
|
} else if (value) {
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
return `An unknown error occured.`;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
export default route;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue