Add the ability to get the counter value without changing it via nightbot command

This commit is contained in:
Oliver Akins 2022-08-18 11:55:49 -06:00
parent 1ea19ebe8c
commit b301cefcae
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99
3 changed files with 24 additions and 0 deletions

View file

@ -59,6 +59,24 @@ const subcommands: {[index: string]: any} = {
};
},
},
"get": {
argc: 1,
async handler(request: Request, meta: subcommandMetadata, args: string[]) {
const { channel } = meta;
let counter = args[1].toLowerCase();
if (!await database.counterExists(channel, counter)) {
return {
message: `That counter doesn't exist.`,
value: 0
};
};
return {
message: await database.getMessage(channel, counter),
value: await database.getValue(channel, counter),
};
},
},
"default": {
argc: 0,
async handler(request: Request, meta: subcommandMetadata, args: string[]) {

View file

@ -9,6 +9,8 @@ export abstract class DatabaseHandler {
public abstract getMessage(channel: string, counter: string): Promise<string>;
public abstract getValue(channel: string, counter: string): Promise<number>;
public abstract addChannel(channel: string): Promise<void>;
public abstract addCounter(channel: string, counter: string): Promise<void>;

View file

@ -41,6 +41,10 @@ export class JSONDatabase extends DatabaseHandler {
return this.data[channel].counters[counter].message;
};
public async getValue(channel: string, counter: string): Promise<number> {
return this.data[channel].counters[counter].value;
};
public async addChannel(channel: string) {
if (await this.channelExists(channel)) {
throw new Error("Channel already exists");