Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing naming counters #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 43 additions & 7 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const helpMsg = `Command reference:
/incx - Increment counter x (replace x with any number)
/dec - Decrement counter
/decx - Decrement counter x
/namex - Name a counter for the overview
/reset - Reset counter back to 0
/resetx - Reset counter x back to 0
/set y - Set counter to y [/set y]
Expand Down Expand Up @@ -71,6 +72,10 @@ function logOutMsg(ctx, text) {
}, text);
}

function nameForCounter(counters, counterId) {
return (counters[counterId].name === undefined) ? counterId : counters[counterId].name;
}

bot.command('broadcast', ctx => {
if(ctx.from.id == config.adminChatId) {
var words = ctx.message.text.split(' ');
Expand Down Expand Up @@ -107,7 +112,7 @@ bot.command('stop', ctx => {
ctx.reply(m);
});

bot.command(['incx', 'decx', 'getx', 'setx', 'resetx'], ctx => {
bot.command(['incx', 'decx', 'getx', 'setx', 'resetx', 'namex'], ctx => {
logMsg(ctx);
logOutMsg(ctx, incNMsg);
ctx.reply(incNMsg);
Expand All @@ -130,7 +135,12 @@ bot.command('getall', ctx => {
counters = dataService.getAllCounters(ctx.chat.id);
msg = "";
Object.keys(counters).forEach(counterId => {
msg += '[' + counterId + '] ' + counters[counterId].value + "\n";
var name = nameForCounter(counters, counterId);
if (name !== counterId) {
msg += '[' + counterId + ']';
}
msg += '[' + name + '] ';
msg += counters[counterId].value + "\n";
});
logOutMsg(ctx, msg);
ctx.reply(msg);
Expand All @@ -152,7 +162,8 @@ bot.hears(getRegExp('inc'), ctx => {
val += delta;
dataService.setCounter(ctx.chat.id, counterId, val);

var printCounterId = counterId ? "[" + counterId + "] " : "";
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
Expand All @@ -174,7 +185,8 @@ bot.hears(getRegExp('dec'), ctx => {
val -= delta;
dataService.setCounter(ctx.chat.id, counterId, val);

var printCounterId = counterId ? "[" + counterId + "] " : "";
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
Expand All @@ -189,7 +201,8 @@ bot.hears(getRegExp('reset'), ctx => {
var val = 0;
dataService.setCounter(ctx.chat.id, counterId, val);

var printCounterId = counterId ? "[" + counterId + "] " : "";
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
Expand All @@ -203,7 +216,8 @@ bot.hears(getRegExp('get'), ctx => {

var val = +dataService.getCounter(ctx.chat.id, counterId);

var printCounterId = counterId ? "[" + counterId + "] " : "";
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + val;
logOutMsg(ctx, val);
ctx.reply(val);
Expand All @@ -219,7 +233,8 @@ bot.hears(getRegExp('set'), ctx => {
if (params.length == 2 && !isNaN(params[1])) {
var val = Math.floor(params[1]);
dataService.setCounter(ctx.chat.id, counterId, val);
var printCounterId = counterId ? "[" + counterId + "] " : "";
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + val;
} else {
val = inputErrMsg;
Expand All @@ -229,6 +244,27 @@ bot.hears(getRegExp('set'), ctx => {
ctx.reply(val);
});

bot.hears(getRegExp('name'), ctx => {
logMsg(ctx);
currentCommand = 'name';
var m = ctx.message.text.match(getRegExp(currentCommand))[0]; //filter command
var counterId = m.substring(m.indexOf(currentCommand) + currentCommand.length) || 0; //get id of command, return 0 if not found

params = ctx.message.text.split(" ");
console.log(params);
if (params.length == 2) {
var name = params[1];
dataService.setName(ctx.chat.id, counterId, name);
var name = nameForCounter(dataService.getAllCounters(ctx.chat.id), counterId);
var printCounterId = counterId ? "[" + name + "] " : "";
val = printCounterId + name;
} else {
val = inputErrMsg;
}

logOutMsg(ctx, val);
ctx.reply(val);
});

bot.startPolling();

Expand Down
9 changes: 8 additions & 1 deletion dataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ function getAllCounters(uid) {
return users[uid].counter;
}

function setName(uid, id, name) {
assertCounter(uid, id);
users[uid].counter[id].name = name;
saveUsers();
}

module.exports = {
loadUsers,
registerUser,
Expand All @@ -132,5 +138,6 @@ module.exports = {
getMetaData,
setCounter,
getCounter,
getAllCounters
getAllCounters,
setName
};
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "2"
services:
node:
image: "node:12"
image: "node:18"
user: "node"
working_dir: /home/node/app
environment:
Expand Down
1 change: 1 addition & 0 deletions further bot settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ inc3 - Increment counter 3
incx - Increment counter x (replace x with any number)
dec - Decrement counter
decx - Decrement counter x
namex - Name a specific counter
reset - Reset counter back to 0
resetx - Reset counter x back to 0
set - Set counter to y [/set y]
Expand Down