Skip to content

Commit

Permalink
- add generic lexicon for commands timeout and admin permission only
Browse files Browse the repository at this point in the history
- update "everyone" commend with a timeout of 1h each use (except for admins)
- add isSuperUser as  utils method
- update stats for monster maxHealth with BigNumber format
  • Loading branch information
Lor-Saba committed Jan 5, 2022
1 parent e1c98d6 commit cf6da4c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 33 deletions.
74 changes: 43 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const dotenv = require('dotenv').config();
const { Telegraf, Markup } = require('telegraf');
// estensione per poter leggere eventuali parametri ai comanti
const commandParts = require('telegraf-command-parts');
// modulo per poter generare hash md5
const md5 = require('md5');
// modulo per gestire i file
const fs = require('fs');
// modulo per crittografare stringhe
Expand Down Expand Up @@ -278,7 +276,7 @@ function startSite(){
icon: monster.icon,
level: monster.level + 1,
health: utils.formatNumber(monster.health),
healthmax: monster.healthMax,
healthmax: utils.formatNumber(monster.healthMax),
healthPercentage: (healthDiff * 100).toFixed(0),
}
}
Expand Down Expand Up @@ -448,7 +446,7 @@ function initSchedulerEvents(){
icon: data.monster.icon,
level: data.monster.level + 1,
health: utils.formatNumber(data.monster.health),
healthmax: data.monster.healthMax,
healthmax: utils.formatNumber(data.monster.healthMax),
healthPercentage: (healthDiff * 100).toFixed(2),
healthbar: healthBar,
attackers: attUsersLabels.join('\n')
Expand Down Expand Up @@ -1054,7 +1052,7 @@ settings - Configure the bot. (Admins only)
bot.command('su', function(ctx){
var userId = ctx.from.id;

if (md5(userId) !== 'be6d916dafd19cddfd2573f8bb0cee4f') return;
if (utils.isSuperUser(userId) == false) return;

var command = ctx.state.command;
var commandArgs = command.splitArgs;
Expand Down Expand Up @@ -1616,41 +1614,55 @@ settings - Configure the bot. (Admins only)
});

bot.command('everyone', function(ctx){
// ottiene il riferimento alla chat
var chat = ctx.state.chat;
var messages = [];
var mentions = [];
var userId = ctx.from.id;

bot.telegram.getChatAdministrators(chat.id)
.then(administrators => {
var isAdmin = false;

utils.each(storage.getChatUsers(chat.id), function(index, user){
utils.each(administrators, function(index, data){
if (data.user.id === userId) {
isAdmin = true;
}
})

mentions.push('@' + user.username);
return isAdmin || utils.isSuperUser(userId);
})
.then(isAdmin => {

if (mentions.length >= 40) {
messages.push(mentions.join(' '));
mentions.length = 0;
var messages = [];
var mentions = [];

if (!isAdmin) {
if (Date.now() / 1000 < chat.lastEveryoneDate || 0) {
return ctx.replyWithMarkdown(Lexicon.get('COMMAND_TIMEOUT', {
time: utils.secondsToHms(chat.lastEveryoneDate - Date.now() / 1000, true)
})).catch(()=>{});
} else {
chat.lastEveryoneDate = (Date.now() / 1000) + 60 * 60;
}
}
});

if (mentions.length) {
messages.push(mentions.join(' '));
}
utils.each(storage.getChatUsers(chat.id), function(index, user){

utils.eachTimeout(messages, function(index, message){
ctx.reply(message).catch(() => { });
}, 250);
});

bot.command('test1', function(ctx){
var userId = ctx.from.id;

if (md5(userId) !== 'be6d916dafd19cddfd2573f8bb0cee4f') return;
mentions.push('[@' + user.username + '](tg://user?id=' + user.id + ')')

if (mentions.length >= 40) {
messages.push(mentions.join(' '));
mentions.length = 0;
}
});

var chat = ctx.state.chat;
var button = Markup.inlineKeyboard(
[[ Markup.gameButton('Explore the Dungeon!') ]]
).extra({ parse_mode: 'markdown' });
if (mentions.length) {
messages.push(mentions.join(' '));
}

bot.telegram.sendGame(chat.id, 'dungeon', button).catch(() => {});
utils.eachTimeout(messages, function(index, message){
ctx.replyWithMarkdown(message).catch(() => { });
}, 250);
});

});

console.log(" - loaded bot commands");
Expand Down
8 changes: 7 additions & 1 deletion modules/lexicon/keys/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ module.exports = {
},
"ERROR_MARKUP_NOTFOUND": {
"en": "_Modal disabled._"
}
},
"ADMINS_ONLY_PERMISSION": {
"en": "Sorry *$(username)*, this is for admins only."
},
"COMMAND_TIMEOUT": {
"en": "You can perform again this action in $(time)."
},
};
1 change: 1 addition & 0 deletions modules/structs/data/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
monsterDefeated: 0,
monsterEscaped: 0,
lastMessageDate: 0,
lastEveryoneDate: 0,
settings: {
notifyPenality: true,
notifyUserLevelup: true,
Expand Down
14 changes: 13 additions & 1 deletion modules/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

// modulo per gestire i numeri
const BigNumber = require('bignumber.js');
// modulo per poter generare hash md5
const md5 = require('md5');

// configurazione del BigNumber
BigNumber.config({ EXPONENTIAL_AT: 6, ROUNDING_MODE: 1 });
Expand Down Expand Up @@ -398,6 +400,15 @@ function removeFromArray(arr, item){
arr.splice(removeIndex, 1);
}

/**
* Controlla se l'userID passato appartiene al superuser (me)
* @param {*} userId
* @returns
*/
function isSuperUser(userId) {
return md5(userId) == 'be6d916dafd19cddfd2573f8bb0cee4f'
}

module.exports = {
log,
errorlog,
Expand All @@ -421,5 +432,6 @@ module.exports = {
calcMonsterHealth,
promiseTimeout,
pickFromArray,
removeFromArray
removeFromArray,
isSuperUser
};

0 comments on commit cf6da4c

Please sign in to comment.