From 672fd562f322463d9734eb8980a7d472a6e8b660 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 16:34:23 +0330 Subject: [PATCH 01/17] refactoring, created messages folder and added context state --- handlers/commands/addCommand.js | 7 ++-- handlers/commands/admin.js | 11 ++--- handlers/commands/ban.js | 6 +-- handlers/commands/getwarns.js | 7 ++-- handlers/commands/leave.js | 11 +++-- handlers/commands/nowarns.js | 9 ++-- handlers/commands/removeCommand.js | 15 ++----- handlers/commands/unadmin.js | 9 ++-- handlers/commands/unban.js | 9 ++-- handlers/commands/unwarn.js | 11 ++--- handlers/commands/warn.js | 9 ++-- .../{middlewares => messages}/addCustomCmd.js | 17 ++++---- handlers/messages/addUser.js | 41 +++++++++++++++++++ .../checkUsername.js | 0 .../{middlewares => messages}/removeLinks.js | 18 ++++---- .../{middlewares => messages}/runCustomCmd.js | 15 ++----- handlers/middlewares/addUser.js | 33 --------------- index.js | 16 +++++--- 18 files changed, 120 insertions(+), 124 deletions(-) rename handlers/{middlewares => messages}/addCustomCmd.js (90%) create mode 100644 handlers/messages/addUser.js rename handlers/{middlewares => messages}/checkUsername.js (100%) rename handlers/{middlewares => messages}/removeLinks.js (76%) rename handlers/{middlewares => messages}/runCustomCmd.js (82%) delete mode 100644 handlers/middlewares/addUser.js diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index c7ea8f5..b2bd8ad 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -1,18 +1,17 @@ 'use strict'; // DB -const { isAdmin } = require('../../stores/user'); const { addCommand } = require('../../stores/command'); // Bot const { replyOptions } = require('../../bot/options'); -const addCommandHandler = async ({ chat, message, reply }) => { - const user = message.from; +const addCommandHandler = async ({ chat, reply, state }) => { + const { isAdmin, user } = state; if (chat.type !== 'private') { return null; } - if (!await isAdmin(user)) { + if (!isAdmin) { return reply('ℹ️ Sorry, only admins access this command.', replyOptions); } diff --git a/handlers/commands/admin.js b/handlers/commands/admin.js index bee579f..5bb45f3 100644 --- a/handlers/commands/admin.js +++ b/handlers/commands/admin.js @@ -1,13 +1,9 @@ 'use strict'; // Utils -const { loadJSON } = require('../../utils/json'); const { link } = require('../../utils/tg'); const { logError } = require('../../utils/log'); -// Config -const { masterID } = loadJSON('config.json'); - // Bot const { replyOptions } = require('../../bot/options'); @@ -20,8 +16,9 @@ const { nowarns } = require('../../stores/user'); -const adminHandler = async ({ message, reply }) => { - if (message.from.id !== masterID) { +const adminHandler = async ({ message, reply, state }) => { + const { isMaster, user } = state; + if (!isMaster) { return null; } @@ -29,7 +26,7 @@ const adminHandler = async ({ message, reply }) => { ? message.reply_to_message.from : message.commandMention ? message.commandMention - : message.from; + : user; if (await isBanned(userToAdmin)) { return reply('ℹ️ Can\'t admin banned user.', replyOptions); diff --git a/handlers/commands/ban.js b/handlers/commands/ban.js index 5d7f05a..66f7213 100644 --- a/handlers/commands/ban.js +++ b/handlers/commands/ban.js @@ -12,8 +12,8 @@ const { replyOptions } = require('../../bot/options'); const { listGroups } = require('../../stores/group'); const { isAdmin, isBanned, ban } = require('../../stores/user'); -const banHandler = async ({ chat, message, reply, telegram, me }) => { - if (!await isAdmin(message.from)) { +const banHandler = async ({ chat, message, reply, telegram, me, state }) => { + if (!state.isAdmin) { return null; } @@ -69,7 +69,7 @@ const banHandler = async ({ chat, message, reply, telegram, me }) => { logError(err); } - return reply(`🚫 ${link(message.from)} banned ${link(userToBan)} ` + + return reply(`🚫 ${link(state.user)} banned ${link(userToBan)} ` + `for:\n\n${reason}`, replyOptions); }; diff --git a/handlers/commands/getwarns.js b/handlers/commands/getwarns.js index d6c7a99..fc3b871 100644 --- a/handlers/commands/getwarns.js +++ b/handlers/commands/getwarns.js @@ -7,10 +7,11 @@ const { link } = require('../../utils/tg'); const { replyOptions } = require('../../bot/options'); // DB -const { isAdmin, getWarns } = require('../../stores/user'); +const { getWarns } = require('../../stores/user'); -const getWarnsHandler = async ({ message, reply }) => { - if (!await isAdmin(message.from)) { +const getWarnsHandler = async ({ message, reply, state }) => { + const { isAdmin } = state; + if (!isAdmin) { return null; } const theUser = message.reply_to_message diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js index f06f052..e7528dc 100644 --- a/handlers/commands/leave.js +++ b/handlers/commands/leave.js @@ -1,15 +1,14 @@ 'use strict'; -const { masterID } = require('../../config.json'); - const { removeGroup } = require('../../stores/group'); -const leaveCommandHandler = async ctx => { - if (ctx.from.id !== masterID) { +const leaveCommandHandler = async ({ chat, telegram, state }) => { + const { isMaster } = state; + if (!isMaster) { return null; } - await removeGroup(ctx.chat); - return ctx.telegram.leaveChat(ctx.chat.id); + await removeGroup(chat); + return telegram.leaveChat(chat.id); }; module.exports = leaveCommandHandler; diff --git a/handlers/commands/nowarns.js b/handlers/commands/nowarns.js index 5c398fc..fdee869 100644 --- a/handlers/commands/nowarns.js +++ b/handlers/commands/nowarns.js @@ -8,10 +8,11 @@ const { logError } = require('../../utils/log'); const { replyOptions } = require('../../bot/options'); // DB -const { isAdmin, getWarns, nowarns } = require('../../stores/user'); +const { getWarns, nowarns } = require('../../stores/user'); -const nowarnsHandler = async ({ message, reply }) => { - if (!await isAdmin(message.from)) { +const nowarnsHandler = async ({ message, reply, state }) => { + const { isAdmin, user } = state; + if (!isAdmin) { return null; } @@ -39,7 +40,7 @@ const nowarnsHandler = async ({ message, reply }) => { } return reply( - `♻️ ${link(message.from)} pardoned ${link(userToUnwarn)} ` + + `♻️ ${link(user)} pardoned ${link(userToUnwarn)} ` + 'for all of their warnings.', replyOptions); }; diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index 3a3e041..a029b48 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -1,25 +1,18 @@ 'use strict'; -// Utils -const { loadJSON } = require('../../utils/json'); - -// Config -const { masterID } = loadJSON('config.json'); - // DB -const { isAdmin } = require('../../stores/user'); const { getCommand, removeCommand } = require('../../stores/command'); // Bot const { replyOptions } = require('../../bot/options'); -const removeCommandHandler = async ({ chat, message, reply }) => { - const user = message.from; +const removeCommandHandler = async ({ chat, message, reply, state }) => { + const { isAdmin, isMaster } = state; const { text } = message; if (chat.type !== 'private') { return null; } - if (!await isAdmin(user)) { + if (!isAdmin) { return reply('ℹ️ Sorry, only admins access this command.', replyOptions); } @@ -37,7 +30,7 @@ const removeCommandHandler = async ({ chat, message, reply }) => { replyOptions); } - if (command.role === 'Master' && user.id !== masterID) { + if (command.role === 'Master' && !isMaster) { return reply('ℹ️ Sorry, only master can remove this command.', replyOptions); } diff --git a/handlers/commands/unadmin.js b/handlers/commands/unadmin.js index ee68b03..0174a10 100644 --- a/handlers/commands/unadmin.js +++ b/handlers/commands/unadmin.js @@ -1,21 +1,18 @@ 'use strict'; // Utils -const { loadJSON } = require('../../utils/json'); const { link } = require('../../utils/tg'); const { logError } = require('../../utils/log'); -// Config -const { masterID } = loadJSON('config.json'); - // Bot const { replyOptions } = require('../../bot/options'); // DB const { isAdmin, unadmin } = require('../../stores/user'); -const unAdminHandler = async ({ message, reply }) => { - if (message.from.id !== masterID) { +const unAdminHandler = async ({ message, reply, state }) => { + const { isMaster } = state; + if (!isMaster) { return null; } diff --git a/handlers/commands/unban.js b/handlers/commands/unban.js index 39713ac..37add2b 100644 --- a/handlers/commands/unban.js +++ b/handlers/commands/unban.js @@ -9,12 +9,13 @@ const { replyOptions } = require('../../bot/options'); // DB const { listGroups } = require('../../stores/group'); -const { isAdmin, isBanned, unban } = require('../../stores/user'); +const { isBanned, unban } = require('../../stores/user'); const noop = Function.prototype; -const unbanHandler = async ({ message, reply, telegram }) => { - if (!await isAdmin(message.from)) { +const unbanHandler = async ({ message, reply, telegram, state }) => { + const { isAdmin, user } = state; + if (!isAdmin) { return null; } @@ -60,7 +61,7 @@ const unbanHandler = async ({ message, reply, telegram }) => { // hance .catch(noop) // (it's an expected, non-critical failure) - return reply(`♻️ ${link(message.from)} unbanned ` + + return reply(`♻️ ${link(user)} unbanned ` + `${link(userToUnban)}.`, replyOptions); }; diff --git a/handlers/commands/unwarn.js b/handlers/commands/unwarn.js index cc65dac..42824c3 100644 --- a/handlers/commands/unwarn.js +++ b/handlers/commands/unwarn.js @@ -7,10 +7,11 @@ const { link } = require('../../utils/tg'); const { replyOptions } = require('../../bot/options'); // DB -const { isAdmin, getWarns, unwarn } = require('../../stores/user'); +const { getWarns, unwarn } = require('../../stores/user'); -const unwarnHandler = async ({ message, reply }) => { - if (!await isAdmin(message.from)) { +const unwarnHandler = async ({ message, reply, state }) => { + const { isAdmin, user } = state; + if (!isAdmin) { return null; } @@ -35,9 +36,9 @@ const unwarnHandler = async ({ message, reply }) => { await unwarn(userToUnwarn); return reply( - `❎ ${link(message.from)} pardoned ${link(userToUnwarn)} ` + + `❎ ${link(user)} pardoned ${link(userToUnwarn)} ` + `for:\n\n${allWarns[allWarns.length - 1]}` + - `(${allWarns.length - 1}/3)`, + ` (${allWarns.length - 1}/3)`, replyOptions); }; diff --git a/handlers/commands/warn.js b/handlers/commands/warn.js index b6afb0c..bc445cf 100644 --- a/handlers/commands/warn.js +++ b/handlers/commands/warn.js @@ -15,8 +15,9 @@ const { replyOptions } = require('../../bot/options'); // DB const { isAdmin, ban, getWarns, warn } = require('../../stores/user'); -const warnHandler = async ({ message, chat, reply, me }) => { - if (!await isAdmin(message.from)) { +const warnHandler = async ({ message, chat, reply, me, state }) => { + const { user } = state; + if (!state.isAdmin) { return null; } @@ -59,14 +60,14 @@ const warnHandler = async ({ message, chat, reply, me }) => { if (warnCount.length < numberOfWarnsToBan) { promises.push(reply( - `⚠️ ${link(message.from)} warned ${link(userToWarn)} ` + + `⚠️ ${link(user)} warned ${link(userToWarn)} ` + `for:\n\n ${reason} (${warnCount.length}/3)`, replyOptions)); } else { promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id)); promises.push(ban(userToWarn, 'Reached max number of warnings')); promises.push(reply( - `🚫 ${link(message.from)} banned ${link(userToWarn)} ` + + `🚫 ${link(user)} banned ${link(userToWarn)} ` + 'for:\n\nReached max number of warnings ' + `(${warnCount.length}/3)\n\n`, replyOptions)); diff --git a/handlers/middlewares/addCustomCmd.js b/handlers/messages/addCustomCmd.js similarity index 90% rename from handlers/middlewares/addCustomCmd.js rename to handlers/messages/addCustomCmd.js index 5f0f3d2..bad7526 100644 --- a/handlers/middlewares/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -6,17 +6,17 @@ const { Markup } = require('telegraf'); const { replyOptions } = require('../../bot/options'); // DB -const { isAdmin } = require('../../stores/user'); const { getCommand, removeCommand, updateCommand } = require('../../stores/command'); -const addCustomCmdHandler = async ({ chat, message, reply }, next) => { +const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { const { text, photo, document, video, audio } = message; - const { id } = message.from; - + const { isAdmin, user } = state; + const { id } = user; + console.log(state); if (text && /^\/\w+/.test(text)) { await removeCommand({ id, isActive: false }); return next(); @@ -24,14 +24,13 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => { const command = await getCommand({ id, isActive: false }); if (chat.type !== 'private' || - !await isAdmin(message.from) || + !isAdmin || !command || !command.state) { return next(); } - const { state } = command; - if (state === 'add') { + if (command.state === 'add') { if (!/^(?=\D)\w+$/.test(text)) { reply('Please send a valid command.'); return next(); @@ -56,7 +55,7 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => { return next(); } - if (state === 'role') { + if (command.state === 'role') { if (text !== 'Master' && text !== 'Admins' && text !== 'Everyone') { reply('Please send a valid role.', Markup.keyboard([ [ 'Master', 'Admins', 'Everyone' ] @@ -75,7 +74,7 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => { return next(); } - if (state === 'content') { + if (command.state === 'content') { let newCommand; if (text) { newCommand = { content: text, type: 'text' }; diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js new file mode 100644 index 0000000..a278b99 --- /dev/null +++ b/handlers/messages/addUser.js @@ -0,0 +1,41 @@ +'use strict'; + +// Utils +const { loadJSON } = require('../../utils/json'); + +// Config +const { masterID } = loadJSON('config.json'); + +// DB +const { addUser, isUser } = require('../../stores/user'); + +const addUserHandler = async (ctx, next) => { + const { message } = ctx; + const newUser = message.from; + const user = newUser && await isUser(message.from); + const usersToAdd = []; + + if (!user && newUser) { + usersToAdd.push(addUser(newUser)); + } + + ctx.state = { + isAdmin: user && user.status === 'admin', + isMaster: user.id === masterID, + user: newUser, + }; + + if ( + message.reply_to_message && + message.reply_to_message.from && + !await isUser(message.reply_to_message.from) + ) { + usersToAdd.push(addUser(message.reply_to_message.from)); + } + + await Promise.all(usersToAdd); + + return next(); +}; + +module.exports = addUserHandler; diff --git a/handlers/middlewares/checkUsername.js b/handlers/messages/checkUsername.js similarity index 100% rename from handlers/middlewares/checkUsername.js rename to handlers/messages/checkUsername.js diff --git a/handlers/middlewares/removeLinks.js b/handlers/messages/removeLinks.js similarity index 76% rename from handlers/middlewares/removeLinks.js rename to handlers/messages/removeLinks.js index dff99c3..36de64f 100644 --- a/handlers/middlewares/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -17,10 +17,11 @@ const bot = require('../../bot'); const { replyOptions } = require('../../bot/options'); // DB -const { isAdmin, ban, warn } = require('../../stores/user'); +const { ban, warn } = require('../../stores/user'); const { listGroups } = require('../../stores/group'); -const removeLinks = async ({ message, chat, reply }, next) => { +const removeLinks = async ({ message, chat, reply, state }, next) => { + const { isAdmin, user } = state; const groups = await listGroups(); const groupLinks = [ ...groups.map(group => group.link @@ -41,26 +42,25 @@ const removeLinks = async ({ message, chat, reply }, next) => { !(excludedChannels.includes(message.text) || groupLinks.includes(message.text.split('/joinchat/')[1])) ) { - const userToWarn = message.from; - if (await isAdmin(userToWarn)) { + if (isAdmin) { return next(); } const reason = 'Channel forward/link'; - const warnCount = await warn(userToWarn, reason); + const warnCount = await warn(user, reason); const promises = [ bot.telegram.deleteMessage(chat.id, message.message_id) ]; if (warnCount < numberOfWarnsToBan) { promises.push(reply( - `⚠️ ${link(userToWarn)} got warned! (${warnCount}/3)` + + `⚠️ ${link(user)} got warned! (${warnCount}/3)` + `\n\nReason: ${reason}`, replyOptions)); } else { - promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id)); - promises.push(ban(userToWarn, + promises.push(bot.telegram.kickChatMember(chat.id, user.id)); + promises.push(ban(user, 'Reached max number of warnings')); promises.push(reply( - `🚫 ${link(userToWarn)} got banned! (${warnCount}/3)` + + `🚫 ${link(user)} got banned! (${warnCount}/3)` + '\n\nReason: Reached max number of warnings', replyOptions)); } diff --git a/handlers/middlewares/runCustomCmd.js b/handlers/messages/runCustomCmd.js similarity index 82% rename from handlers/middlewares/runCustomCmd.js rename to handlers/messages/runCustomCmd.js index cf3ad8e..4a55b17 100644 --- a/handlers/middlewares/runCustomCmd.js +++ b/handlers/messages/runCustomCmd.js @@ -1,18 +1,11 @@ 'use strict'; -// Utils -const { loadJSON } = require('../../utils/json'); - -// Config -const { masterID } = loadJSON('config.json'); - // DB const { getCommand } = require('../../stores/command'); -const { isAdmin } = require('../../stores/user'); const runCustomCmdHandler = async (ctx, next) => { - const { message } = ctx; - const user = message.from; + const { message, state } = ctx; + const { isAdmin, isMaster } = state; const isCommand = message.entities && message.entities.filter(entity => entity.type === 'bot_command'); if (!isCommand || !isCommand.length) { @@ -33,9 +26,9 @@ const runCustomCmdHandler = async (ctx, next) => { const options = Object.assign(replyTo, caption ? { caption } : {}); if ( role === 'Master' && - user.id !== masterID || + !isMaster || role === 'Admins' && - !await isAdmin(user) + !isAdmin ) { return next(); } diff --git a/handlers/middlewares/addUser.js b/handlers/middlewares/addUser.js deleted file mode 100644 index 192d2a6..0000000 --- a/handlers/middlewares/addUser.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -// Utils -const { logError } = require('../../utils/log'); - -// DB -const { addUser, isUser } = require('../../stores/user'); - -const addUserHandler = async ({ message }, next) => { - const usersToAdd = []; - if (message.from && !await isUser(message.from)) { - usersToAdd.push(message.from); - } - if ( - message.reply_to_message && - message.reply_to_message.from && - !await isUser(message.reply_to_message.from) - ) { - usersToAdd.push(message.reply_to_message.from); - } - - usersToAdd.forEach(async user => { - try { - await addUser(user); - } catch (err) { - logError(err); - } - }); - - return next(); -}; - -module.exports = addUserHandler; diff --git a/index.js b/index.js index b2fb4d9..3302546 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ const delTimeout = 2 * 60 * 1000; * Path of middlewares and commands */ const middleware = './handlers/middlewares'; +const message = './handlers/messages'; const command = './handlers/commands'; /** @@ -35,14 +36,19 @@ const command = './handlers/commands'; const leaveUnmanagedHandler = require(`${middleware}/leaveUnmanaged`); const removeCommandsHandler = require(`${middleware}/removeCommands`); const kickBannedHandler = require(`${middleware}/kickBanned`); -const addUserHandler = require(`${middleware}/addUser`); -const removeLinksHandler = require(`${middleware}/removeLinks`); -const checkUsernameHandler = require(`${middleware}/checkUsername`); -const addCustomCmdHandler = require(`${middleware}/addCustomCmd`); -const runCustomCmdHandler = require(`${middleware}/runCustomCmd`); const antibotHandler = require(`${middleware}/antibot`); const addedToGroupHandler = require(`${middleware}/addedToGroup`); +/** + * @type {function} + * Messages Handlers + */ +const addUserHandler = require(`${message}/addUser`); +const removeLinksHandler = require(`${message}/removeLinks`); +const checkUsernameHandler = require(`${message}/checkUsername`); +const addCustomCmdHandler = require(`${message}/addCustomCmd`); +const runCustomCmdHandler = require(`${message}/runCustomCmd`); + /** * @type {function} * Commmands Handlers From 36535d4a6b435ecee4e9ff5747c9a7626dc0b8e3 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 16:40:49 +0330 Subject: [PATCH 02/17] refactoring on line if statements returns --- .eslintrc.json | 2 +- handlers/commands/addCommand.js | 5 ++--- handlers/commands/admin.js | 4 +--- handlers/commands/ban.js | 4 +--- handlers/commands/commands.js | 5 ++--- handlers/commands/getwarns.js | 5 ++--- handlers/commands/help.js | 5 ++--- handlers/commands/leave.js | 5 ++--- handlers/commands/nowarns.js | 4 +--- handlers/commands/removeCommand.js | 5 ++--- handlers/commands/unadmin.js | 4 +--- handlers/commands/unban.js | 4 +--- handlers/commands/unwarn.js | 4 +--- handlers/commands/warn.js | 4 +--- 14 files changed, 20 insertions(+), 40 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 33cc707..983799b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,7 @@ "consistent-return": "error", "curly": [ "error", - "all" + "multi-line" ], "default-case": "error", "dot-location": [ diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index b2bd8ad..85a2822 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -8,9 +8,8 @@ const { replyOptions } = require('../../bot/options'); const addCommandHandler = async ({ chat, reply, state }) => { const { isAdmin, user } = state; - if (chat.type !== 'private') { - return null; - } + if (chat.type !== 'private') return null; + if (!isAdmin) { return reply('ℹ️ Sorry, only admins access this command.', replyOptions); diff --git a/handlers/commands/admin.js b/handlers/commands/admin.js index 5bb45f3..556ea22 100644 --- a/handlers/commands/admin.js +++ b/handlers/commands/admin.js @@ -18,9 +18,7 @@ const { const adminHandler = async ({ message, reply, state }) => { const { isMaster, user } = state; - if (!isMaster) { - return null; - } + if (!isMaster) return null; const userToAdmin = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/ban.js b/handlers/commands/ban.js index 66f7213..57d53c6 100644 --- a/handlers/commands/ban.js +++ b/handlers/commands/ban.js @@ -13,9 +13,7 @@ const { listGroups } = require('../../stores/group'); const { isAdmin, isBanned, ban } = require('../../stores/user'); const banHandler = async ({ chat, message, reply, telegram, me, state }) => { - if (!state.isAdmin) { - return null; - } + if (!state.isAdmin) return null; const userToBan = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/commands.js b/handlers/commands/commands.js index 8fd8fc2..11e5503 100644 --- a/handlers/commands/commands.js +++ b/handlers/commands/commands.js @@ -29,9 +29,8 @@ const actions = `\n /removecommand <name> - to remove a custom command.`; const commandReferenceHandler = async ({ chat, replyWithHTML }) => { - if (chat.type !== 'private') { - return null; - } + if (chat.type !== 'private') return null; + const customCommands = await listCommands(); const customCommandsText = customCommands.length ? '\nCustom commands:\n' + diff --git a/handlers/commands/getwarns.js b/handlers/commands/getwarns.js index fc3b871..df8c3e0 100644 --- a/handlers/commands/getwarns.js +++ b/handlers/commands/getwarns.js @@ -11,9 +11,8 @@ const { getWarns } = require('../../stores/user'); const getWarnsHandler = async ({ message, reply, state }) => { const { isAdmin } = state; - if (!isAdmin) { - return null; - } + if (!isAdmin) return null; + const theUser = message.reply_to_message ? message.reply_to_message.from : message.commandMention diff --git a/handlers/commands/help.js b/handlers/commands/help.js index 8260a23..106b974 100644 --- a/handlers/commands/help.js +++ b/handlers/commands/help.js @@ -19,9 +19,8 @@ and @mattatabot might be better choices for you. `; const helpHandler = ({ chat, replyWithHTML }) => { - if (chat.type !== 'private') { - return null; - } + if (chat.type !== 'private') return null; + return replyWithHTML( message, Markup.inlineKeyboard( diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js index e7528dc..e20445f 100644 --- a/handlers/commands/leave.js +++ b/handlers/commands/leave.js @@ -4,9 +4,8 @@ const { removeGroup } = require('../../stores/group'); const leaveCommandHandler = async ({ chat, telegram, state }) => { const { isMaster } = state; - if (!isMaster) { - return null; - } + if (!isMaster) return null; + await removeGroup(chat); return telegram.leaveChat(chat.id); }; diff --git a/handlers/commands/nowarns.js b/handlers/commands/nowarns.js index fdee869..3a533f2 100644 --- a/handlers/commands/nowarns.js +++ b/handlers/commands/nowarns.js @@ -12,9 +12,7 @@ const { getWarns, nowarns } = require('../../stores/user'); const nowarnsHandler = async ({ message, reply, state }) => { const { isAdmin, user } = state; - if (!isAdmin) { - return null; - } + if (!isAdmin) return null; const userToUnwarn = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/removeCommand.js b/handlers/commands/removeCommand.js index a029b48..635a07c 100644 --- a/handlers/commands/removeCommand.js +++ b/handlers/commands/removeCommand.js @@ -9,9 +9,8 @@ const { replyOptions } = require('../../bot/options'); const removeCommandHandler = async ({ chat, message, reply, state }) => { const { isAdmin, isMaster } = state; const { text } = message; - if (chat.type !== 'private') { - return null; - } + if (chat.type !== 'private') return null; + if (!isAdmin) { return reply('ℹ️ Sorry, only admins access this command.', replyOptions); diff --git a/handlers/commands/unadmin.js b/handlers/commands/unadmin.js index 0174a10..91363a0 100644 --- a/handlers/commands/unadmin.js +++ b/handlers/commands/unadmin.js @@ -12,9 +12,7 @@ const { isAdmin, unadmin } = require('../../stores/user'); const unAdminHandler = async ({ message, reply, state }) => { const { isMaster } = state; - if (!isMaster) { - return null; - } + if (!isMaster) return null; const userToUnadmin = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/unban.js b/handlers/commands/unban.js index 37add2b..8554a36 100644 --- a/handlers/commands/unban.js +++ b/handlers/commands/unban.js @@ -15,9 +15,7 @@ const noop = Function.prototype; const unbanHandler = async ({ message, reply, telegram, state }) => { const { isAdmin, user } = state; - if (!isAdmin) { - return null; - } + if (!isAdmin) return null; const userToUnban = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/unwarn.js b/handlers/commands/unwarn.js index 42824c3..fd07e3e 100644 --- a/handlers/commands/unwarn.js +++ b/handlers/commands/unwarn.js @@ -11,9 +11,7 @@ const { getWarns, unwarn } = require('../../stores/user'); const unwarnHandler = async ({ message, reply, state }) => { const { isAdmin, user } = state; - if (!isAdmin) { - return null; - } + if (!isAdmin) return null; const userToUnwarn = message.reply_to_message ? message.reply_to_message.from diff --git a/handlers/commands/warn.js b/handlers/commands/warn.js index bc445cf..091b71c 100644 --- a/handlers/commands/warn.js +++ b/handlers/commands/warn.js @@ -17,9 +17,7 @@ const { isAdmin, ban, getWarns, warn } = require('../../stores/user'); const warnHandler = async ({ message, chat, reply, me, state }) => { const { user } = state; - if (!state.isAdmin) { - return null; - } + if (!state.isAdmin) return null; const userToWarn = message.reply_to_message ? message.reply_to_message.from From 5ad0a723251fb5d235b0c9c26aeb15b9fbe288ad Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Fri, 6 Oct 2017 18:15:28 +0200 Subject: [PATCH 03/17] Added @returns to silence eslint --- utils/log.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/log.js b/utils/log.js index 502de53..a4ed821 100644 --- a/utils/log.js +++ b/utils/log.js @@ -5,12 +5,14 @@ const { inspect } = require('util'); /** * @param {Error} err * Logs errors to console + * @returns {undefined} */ const logError = err => console.error(err); /** * @param {Object} value * Echos the value of a value. + * @returns {undefined} */ const print = value => console.log(inspect(value, { colors: true, depth: null })); From 0f0dee3a7d510d3745d0c095dac38f8780b9ff1d Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Fri, 6 Oct 2017 18:35:53 +0200 Subject: [PATCH 04/17] Removed utils/json, replaced loadJSON with require --- bot/index.js | 3 +-- handlers/commands/warn.js | 3 +-- handlers/messages/addUser.js | 5 +---- handlers/messages/removeLinks.js | 3 +-- utils/json.js | 10 ---------- 5 files changed, 4 insertions(+), 20 deletions(-) delete mode 100644 utils/json.js diff --git a/bot/index.js b/bot/index.js index 409e62e..74ca7fe 100644 --- a/bot/index.js +++ b/bot/index.js @@ -1,8 +1,7 @@ 'use strict'; const Telegraf = require('telegraf'); -const { loadJSON } = require('../utils/json'); -const config = loadJSON('config.json'); +const config = require('../config.json'); const bot = new Telegraf(config.token); diff --git a/handlers/commands/warn.js b/handlers/commands/warn.js index 091b71c..91af0a5 100644 --- a/handlers/commands/warn.js +++ b/handlers/commands/warn.js @@ -1,12 +1,11 @@ 'use strict'; // Utils -const { loadJSON } = require('../../utils/json'); const { link } = require('../../utils/tg'); const { logError } = require('../../utils/log'); // Config -const { numberOfWarnsToBan } = loadJSON('config.json'); +const { numberOfWarnsToBan } = require('../../config.json'); // Bot const bot = require('../../bot'); diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index a278b99..bd3f0da 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -1,10 +1,7 @@ 'use strict'; -// Utils -const { loadJSON } = require('../../utils/json'); - // Config -const { masterID } = loadJSON('config.json'); +const { masterID } = require('../../config.json'); // DB const { addUser, isUser } = require('../../stores/user'); diff --git a/handlers/messages/removeLinks.js b/handlers/messages/removeLinks.js index 36de64f..04b9db8 100644 --- a/handlers/messages/removeLinks.js +++ b/handlers/messages/removeLinks.js @@ -1,7 +1,6 @@ 'use strict'; // Utils -const { loadJSON } = require('../../utils/json'); const { link } = require('../../utils/tg'); const { logError } = require('../../utils/log'); @@ -10,7 +9,7 @@ const { excludedChannels, excludedGroups, numberOfWarnsToBan -} = loadJSON('config.json'); +} = require('../../config.json'); // Bot const bot = require('../../bot'); diff --git a/utils/json.js b/utils/json.js deleted file mode 100644 index 6d57eba..0000000 --- a/utils/json.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -const { readFileSync, writeFileSync } = require('fs'); - -const loadJSON = file => JSON.parse(readFileSync(file, 'utf8')); - -const saveJSON = (file, data) => writeFileSync(file, - JSON.stringify(data, null, '\t')); - -module.exports = { loadJSON, saveJSON }; From f0dbe0d92ea3623f541403688ff10ffa0d386efa Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Fri, 6 Oct 2017 19:05:44 +0200 Subject: [PATCH 05/17] Remove warnings on unban instead of on ban --- stores/user.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stores/user.js b/stores/user.js index 7935fc1..fff0bf4 100644 --- a/stores/user.js +++ b/stores/user.js @@ -47,10 +47,12 @@ const isAdmin = ({ id }) => User.findOne({ id, status: 'admin' }); const ban = ({ id }, ban_reason) => - User.update({ id }, { $set: { ban_reason, status: 'banned', warns: [] } }); + User.update({ id }, { $set: { ban_reason, status: 'banned' } }); const unban = ({ id }) => - User.update({ id }, { $set: { banReason: '', status: 'member' } }); + User.update( + { id }, + { $set: { ban_reason: null, status: 'member', warns: [] } }); const isBanned = ({ id }) => User.findOne({ id, status: 'banned' }) From 70b28b7b53e4eeaa51452624578abe4a41bc0d19 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 21:39:03 +0330 Subject: [PATCH 06/17] fixed id error on adding user to the state --- handlers/messages/addCustomCmd.js | 2 +- handlers/messages/addUser.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index bad7526..4a6763e 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -16,7 +16,7 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { const { text, photo, document, video, audio } = message; const { isAdmin, user } = state; const { id } = user; - console.log(state); + if (text && /^\/\w+/.test(text)) { await removeCommand({ id, isActive: false }); return next(); diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index bd3f0da..5798b8f 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -18,7 +18,7 @@ const addUserHandler = async (ctx, next) => { ctx.state = { isAdmin: user && user.status === 'admin', - isMaster: user.id === masterID, + isMaster: user && user.id === masterID, user: newUser, }; From e8d682d52797f68c75acab0932e686f3fca3e878 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 21:49:33 +0330 Subject: [PATCH 07/17] Custom commands now start with ! --- handlers/messages/runCustomCmd.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/handlers/messages/runCustomCmd.js b/handlers/messages/runCustomCmd.js index 4a55b17..13e58f0 100644 --- a/handlers/messages/runCustomCmd.js +++ b/handlers/messages/runCustomCmd.js @@ -6,13 +6,12 @@ const { getCommand } = require('../../stores/command'); const runCustomCmdHandler = async (ctx, next) => { const { message, state } = ctx; const { isAdmin, isMaster } = state; - const isCommand = message.entities && - message.entities.filter(entity => entity.type === 'bot_command'); - if (!isCommand || !isCommand.length) { + const isCommand = /^!\w+/.test(message.text); + if (!isCommand) { return next(); } - const commandName = message.text.split(' ')[0].replace('/', ''); + const commandName = message.text.split(' ')[0].replace('!', ''); const command = await getCommand({ isActive: true, name: commandName }); if (!command) { From 4b1f57dd7705f5fcd987225907d059586b6235aa Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 22:14:32 +0330 Subject: [PATCH 08/17] commands now work with id --- handlers/messages/checkUsername.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/handlers/messages/checkUsername.js b/handlers/messages/checkUsername.js index 0ec80f4..787efee 100644 --- a/handlers/messages/checkUsername.js +++ b/handlers/messages/checkUsername.js @@ -13,6 +13,7 @@ const checkUsernameHandler = async ({ message }, next) => { entity.type === 'mention'); const hasTextMention = message.entities.some(entity => entity.type === 'text_mention'); + const hasId = /^\d+/.test(messageArr[1]); if (!isCommand) { return next(); @@ -42,6 +43,17 @@ const checkUsernameHandler = async ({ message }, next) => { message.commandMention = user; return next(); } + + if (hasId) { + const [ , id ] = messageArr; + const user = await getUser({ id: Number(id) }); + console.log(id, user); + if (user) { + message.text = message.text.replace(` ${id}`, ''); + message.commandMention = user; + } + return next(); + } return next(); }; From 78fbcdc981baf20ac2b87303321acfebaea3e41a Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Fri, 6 Oct 2017 22:44:16 +0330 Subject: [PATCH 09/17] custom commands name can't be one of preserved names. --- handlers/commands/addCommand.js | 5 +++-- handlers/messages/addCustomCmd.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/handlers/commands/addCommand.js b/handlers/commands/addCommand.js index 85a2822..ff3a191 100644 --- a/handlers/commands/addCommand.js +++ b/handlers/commands/addCommand.js @@ -15,8 +15,9 @@ const addCommandHandler = async ({ chat, reply, state }) => { replyOptions); } await addCommand({ id: user.id }); - return reply('Enter a name for the command.\n\nFor example: rules', - replyOptions); + return reply('Enter a name for the command without forward slash "/".' + + '\n\nFor example: rules', + replyOptions); }; module.exports = addCommandHandler; diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index 4a6763e..518e242 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -12,6 +12,10 @@ const { updateCommand } = require('../../stores/command'); +const preserved = [ 'admin', 'unadmin', 'leave', 'warn', 'unwarn', 'nowarns', + 'getwarns', 'ban', 'unban', 'report', 'staff', 'link', 'groups', 'commands', + 'addcommand', 'removecommand' ]; + const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { const { text, photo, document, video, audio } = message; const { isAdmin, user } = state; @@ -35,6 +39,12 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { reply('Please send a valid command.'); return next(); } + if (preserved.includes(text.toLowerCase())) { + reply('❗️Sorry you can\'t use this name, it\'s preserved.\n\n' + + 'Try another one.'); + return next(); + } + if (await getCommand({ isActive: true, name: text })) { reply( 'ℹ️ This command already exists.\n\n' + From 92c701df829d13d28843abb1199c59da95b3becb Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 11:30:34 +0330 Subject: [PATCH 10/17] adding users on join --- handlers/messages/addUser.js | 9 +++++++++ handlers/messages/checkUsername.js | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/handlers/messages/addUser.js b/handlers/messages/addUser.js index 5798b8f..1eadd76 100644 --- a/handlers/messages/addUser.js +++ b/handlers/messages/addUser.js @@ -8,10 +8,19 @@ const { addUser, isUser } = require('../../stores/user'); const addUserHandler = async (ctx, next) => { const { message } = ctx; + const { new_chat_members } = message; const newUser = message.from; const user = newUser && await isUser(message.from); const usersToAdd = []; + if (new_chat_members) { + new_chat_members.forEach(async member => { + if (!await isUser(member)) { + usersToAdd.push(addUser(member)); + } + }); + } + if (!user && newUser) { usersToAdd.push(addUser(newUser)); } diff --git a/handlers/messages/checkUsername.js b/handlers/messages/checkUsername.js index 787efee..7e1a591 100644 --- a/handlers/messages/checkUsername.js +++ b/handlers/messages/checkUsername.js @@ -47,7 +47,6 @@ const checkUsernameHandler = async ({ message }, next) => { if (hasId) { const [ , id ] = messageArr; const user = await getUser({ id: Number(id) }); - console.log(id, user); if (user) { message.text = message.text.replace(` ${id}`, ''); message.commandMention = user; From d3b4248b10e435ce4870e240b2aa1e398b58ebf2 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 11:40:42 +0330 Subject: [PATCH 11/17] @admins works with words after --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 3302546..96b4bcf 100644 --- a/index.js +++ b/index.js @@ -94,7 +94,7 @@ bot.command('getwarns', getWarnsHandler); bot.command('ban', banHandler); bot.command('unban', unbanHandler); bot.command('report', reportHandler); -bot.hears(/^@admins?$/i, reportHandler); +bot.hears(/^@admins?\s?/i, reportHandler); bot.command('staff', staffHandler); bot.command('link', linkHandler); bot.command('groups', groupsHandler); From 5314328ab66123d1f9c581fa0e595bb8eb06c16c Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 11:45:43 +0330 Subject: [PATCH 12/17] replaced stars with zero-width spaces, resolves #22 --- handlers/commands/report.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/handlers/commands/report.js b/handlers/commands/report.js index 79ab3fb..2ce087d 100644 --- a/handlers/commands/report.js +++ b/handlers/commands/report.js @@ -17,12 +17,12 @@ const reportHandler = async ctx => { } const admins = await getAdmins(); const adminObjects = admins.map(user => ({ - first_name: '⭐️', // small hack to be able to use link function + first_name: '​', // small hack to be able to use link function id: user.id, })); - const stars = adminObjects.map(link).join(''); - const s = `📋 ${link(ctx.from)} reported the message to admins: ` + - `${stars}`; + const adminsMention = adminObjects.map(link).join(''); + const s = `❗️${link(ctx.from)} reported the message to the admins.` + + `${adminsMention}`; return ctx.replyWithHTML(s, { reply_to_message_id: msg.reply_to_message.message_id }); From c42db48de1d9ae32f3b5ca1d08903ccdc147da7f Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 13:19:11 +0330 Subject: [PATCH 13/17] /leave now works with id and name too. closes #20 --- handlers/commands/leave.js | 27 ++++++++++++++++++++++++-- handlers/commands/link.js | 2 +- handlers/middlewares/addedToGroup.js | 2 +- handlers/middlewares/leaveUnmanaged.js | 2 +- stores/group.js | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js index e20445f..8fd54c3 100644 --- a/handlers/commands/leave.js +++ b/handlers/commands/leave.js @@ -1,11 +1,34 @@ 'use strict'; -const { removeGroup } = require('../../stores/group'); +const { managesGroup, removeGroup } = require('../../stores/group'); -const leaveCommandHandler = async ({ chat, telegram, state }) => { +const leaveCommandHandler = async ctx => { + const { chat, message, telegram, state, replyWithHTML } = ctx; const { isMaster } = state; if (!isMaster) return null; + const groupName = message.text.split(' ').slice(1).join(' '); + + if (groupName) { + const group = /^-?\d+/.test(groupName) + ? { id: Number(groupName) } + : { title: groupName }; + console.log(group); + const isGroup = await managesGroup(group); + if (!isGroup) { + return replyWithHTML( + 'ℹ️ Couldn\'t find a group with that ID/name.' + ); + } + await Promise.all([ + removeGroup(isGroup), + telegram.leaveChat(isGroup.id) + ]); + return replyWithHTML( + '✅ I no longer manage that group.' + ); + } + await removeGroup(chat); return telegram.leaveChat(chat.id); }; diff --git a/handlers/commands/link.js b/handlers/commands/link.js index bbd201f..9310eec 100644 --- a/handlers/commands/link.js +++ b/handlers/commands/link.js @@ -7,7 +7,7 @@ const bot = require('../../bot'); const { managesGroup } = require('../../stores/group'); const linkHandler = async ({ chat, replyWithHTML }) => { - const group = await managesGroup(chat); + const group = await managesGroup({ id: chat.id }); const { message_id } = await replyWithHTML( 'ℹ️ Group\'s link:\n\n' + diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index 2c51f57..485bf57 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -15,7 +15,7 @@ const addedToGroupHandler = async (ctx, next) => { user.username === ctx.me); if (wasAdded && ctx.from.id === masterID) { await admin(ctx.from); - if (!await managesGroup(ctx.chat)) { + if (!await managesGroup({ id: ctx.chat.id })) { try { const link = await telegram.exportChatInviteLink(ctx.chat.id); ctx.chat.link = link ? link : ''; diff --git a/handlers/middlewares/leaveUnmanaged.js b/handlers/middlewares/leaveUnmanaged.js index f3edd1f..2997d98 100644 --- a/handlers/middlewares/leaveUnmanaged.js +++ b/handlers/middlewares/leaveUnmanaged.js @@ -47,7 +47,7 @@ const randomChoice = arr => arr[Math.floor(Math.random() * arr.length)]; * @returns {Promise.<*>} - returns next object */ const leaveUnmanagedHandler = async (ctx, next) => { - if (ctx.chat.type === 'private' || await managesGroup(ctx.chat)) { + if (ctx.chat.type === 'private' || await managesGroup({ id: ctx.chat.id })) { return next(); } diff --git a/stores/group.js b/stores/group.js index d458606..88f5ffb 100644 --- a/stores/group.js +++ b/stores/group.js @@ -19,7 +19,7 @@ const listGroups = () => Group.find({}); const managesGroup = group => - Group.findOne({ id: group.id }); + Group.findOne(group); const removeGroup = ({ id }) => Group.remove({ id }); From 506fb11cc1c2f853f5ed4cf3a2ded2eb38a51342 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 13:21:48 +0330 Subject: [PATCH 14/17] fixed eslint errors --- handlers/middlewares/leaveUnmanaged.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/handlers/middlewares/leaveUnmanaged.js b/handlers/middlewares/leaveUnmanaged.js index 2997d98..c047fe1 100644 --- a/handlers/middlewares/leaveUnmanaged.js +++ b/handlers/middlewares/leaveUnmanaged.js @@ -47,7 +47,9 @@ const randomChoice = arr => arr[Math.floor(Math.random() * arr.length)]; * @returns {Promise.<*>} - returns next object */ const leaveUnmanagedHandler = async (ctx, next) => { - if (ctx.chat.type === 'private' || await managesGroup({ id: ctx.chat.id })) { + if ( + ctx.chat.type === 'private' || + await managesGroup({ id: ctx.chat.id })) { return next(); } From 0ed763baf79de44e83d7d46d64a783eb2a9efcda Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 14:09:04 +0330 Subject: [PATCH 15/17] Update README.md --- README.md | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9b02297..8d1ee4c 100644 --- a/README.md +++ b/README.md @@ -20,25 +20,29 @@ You need [Node.js](https://nodejs.org/) (> 8.1) to run this bot. Now you can add the bot as **administrator** to your groups. ## Commands -Command | Role | Description --------- | ------ | ----------- -`/admin` | _Master_ | Makes the user admin. -`/unadmin` | _Master_ | Demotes the user from admin list. -`/leave` | _Master_ | Makes the bot leave the group cleanly. -`/warn ` | _Admin_ | Warns the user. -`/unwarn` | _Admin_ | Removes the last warn from the user. -`/nowarns` | _Admin_ | Clears warns for the user. -`/getwarns` | _Admin_ | Shows a list of warns for the user. -`/ban ` | _Admin_ | Bans the user from groups. -`/unban` | _Admin_ | Removes the user from ban list. -`/staff` | _All_ | Shows a list of admins. -`/link` | _All_ | Show the current group's link. -`/groups` | _All_ | Show a list of groups which the bot is admin in. -`/report` | _All_ | Reports the replied-to message to admins. +Command | Role | Available at | Description +----------------------- | ---------- | ------------ | ----------------- +`/admin` | _Master_ | _Everywhere_ | Makes the user admin. +`/unadmin` | _Master_ | _Everywhere_ | Demotes the user from admin list. +`/leave ` | _Master_ | _Everywhere_ | Make the bot to leave the group cleanly. +`/warn ` | _Admin_ | _Groups_ | Warns the user. +`/unwarn` | _Admin_ | _Everywhere_ | Removes the last warn from the user. +`/nowarns` | _Admin_ | _Everywhere_ | Clears warns for the user. +`/getwarns` | _Admin_ | _Everywhere_ | Shows a list of warns for the user. +`/ban ` | _Admin_ | _Groups_ | Bans the user from groups. +`/unban` | _Admin_ | _Everywhere_ | Removes the user from ban list. +`/addcommand` | _Admin_ | _In-Bot_ | Create a custom command. +`/removecommand ` | _Admin_ | _In-Bot_ | Remove a custom command. +`/staff` | _Everyone_ | _Everywhere_ | Shows a list of admins. +`/link` | _Everyone_ | _Everywhere_ | Shows the current group's link. +`/groups` | _Everyone_ | _Everywhere_ | Shows a list of groups which the bot is admin in. +`/report` | _Everyone_ | _Everywhere_ | Reports the replied-to message to admins. +`/commands` | _Everyone_ | _In-Bot_ | Shows a list of available commands. +`/help` \| `/start` | _Everyone_ | _In-Bot_ | How to use the bot. All commands and actions are synchronized across all of the groups managed by the owner and they work with both **replying** and **mentioning** a user. -If used by reply, `/ban` and `/warn` remove the replied-to message. +If used by reply, `/ban` and `/warn` would remove the replied-to message. The bot is still in alpha phase so feel free to open issues and ask for a _feature_. From 671311290381361f8bee94bdb36d175f129339d7 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 14:17:03 +0330 Subject: [PATCH 16/17] improved custom commands info --- handlers/commands/commands.js | 2 +- handlers/messages/addCustomCmd.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/handlers/commands/commands.js b/handlers/commands/commands.js index 11e5503..f333a99 100644 --- a/handlers/commands/commands.js +++ b/handlers/commands/commands.js @@ -37,7 +37,7 @@ const commandReferenceHandler = async ({ chat, replyWithHTML }) => { customCommands .filter(command => command.isActive) .sort((a, b) => a.role < b.role) - .map(command => `[${command.role}] /${command.name}`) + .map(command => `[${command.role}] !${command.name}`) .join('\n') : ''; diff --git a/handlers/messages/addCustomCmd.js b/handlers/messages/addCustomCmd.js index 518e242..6bd767f 100644 --- a/handlers/messages/addCustomCmd.js +++ b/handlers/messages/addCustomCmd.js @@ -115,7 +115,8 @@ const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => { ]); reply( '✅ New command has been created successfully.\n\n' + - 'This command can be used in groups now. ' + + 'Custom commands work with ! instead of /.\n\n' + + 'For example: !rules\n\n' + 'Custom commands can reply other messages too.\n\n' + '/commands - to see the list of commands.\n' + '/addcommand - to add a new command.\n' + From a55f242470da85c7cb025e73d22f4370559d3ce7 Mon Sep 17 00:00:00 2001 From: Pouria Ezzati Date: Sun, 8 Oct 2017 14:19:30 +0330 Subject: [PATCH 17/17] 0.4.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index fd092c0..ceee8a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "thedevs_bot", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 967eaaa..9ef7664 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thedevs_bot", - "version": "0.3.0", + "version": "0.4.0", "description": "Official Bot for The Devs community.", "main": "index.js", "scripts": {