Add argument to specify the counter's message when creating it.

This commit is contained in:
Oliver Akins 2022-08-20 14:17:00 -06:00
parent 2a79b41ad0
commit 3f5a122bae
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99
2 changed files with 12 additions and 4 deletions

View file

@ -51,10 +51,18 @@ const subcommands: {[index: string]: any} = {
allowIgnored: false,
argc: 1,
async handler(request: Request, meta: subcommandMetadata, args: string[]) {
// args: !cmd new <counter:string>
// args: !cmd new <counter:string> [message:string...]
let { channel } = meta;
let counter = args[1].toLowerCase();
await database.addCounter(channel, counter);
let message = args.length > 2 ? args.slice(2).join(` `) : undefined;
if (!message?.includes(`$(count)`)) {
return {
message: `Can't create a counter when the message doesn't have $(count) in it somewhere`,
};
};
await database.addCounter(channel, counter, message);
return {
message: `Counter ${counter} created successfully.`,
value: 0

View file

@ -55,13 +55,13 @@ export class JSONDatabase extends DatabaseHandler {
};
};
public async addCounter(channel: string, counter: string) {
public async addCounter(channel: string, counter: string, message?: string) {
if (await this.counterExists(channel, counter)) {
throw new Error(`Counter already exists`);
};
this.data[channel].counters[counter] = {
value: 0,
message: `$(value)`,
message: message ?? `$(value)`,
};
};