Skip to content

Commit 7533e97

Browse files
authored
Merge pull request #29 from TheDevs-Network/develop
better custom commands and overall improvements
2 parents 4925cf6 + a55f242 commit 7533e97

33 files changed

+230
-206
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"consistent-return": "error",
4040
"curly": [
4141
"error",
42-
"all"
42+
"multi-line"
4343
],
4444
"default-case": "error",
4545
"dot-location": [

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,29 @@ You need [Node.js](https://nodejs.org/) (> 8.1) to run this bot.
2020
Now you can add the bot as **administrator** to your groups.
2121

2222
## Commands
23-
Command | Role | Description
24-
-------- | ------ | -----------
25-
`/admin` | _Master_ | Makes the user admin.
26-
`/unadmin` | _Master_ | Demotes the user from admin list.
27-
`/leave` | _Master_ | Makes the bot leave the group cleanly.
28-
`/warn <reason>` | _Admin_ | Warns the user.
29-
`/unwarn` | _Admin_ | Removes the last warn from the user.
30-
`/nowarns` | _Admin_ | Clears warns for the user.
31-
`/getwarns` | _Admin_ | Shows a list of warns for the user.
32-
`/ban <reason>` | _Admin_ | Bans the user from groups.
33-
`/unban` | _Admin_ | Removes the user from ban list.
34-
`/staff` | _All_ | Shows a list of admins.
35-
`/link` | _All_ | Show the current group's link.
36-
`/groups` | _All_ | Show a list of groups which the bot is admin in.
37-
`/report` | _All_ | Reports the replied-to message to admins.
23+
Command | Role | Available at | Description
24+
----------------------- | ---------- | ------------ | -----------------
25+
`/admin` | _Master_ | _Everywhere_ | Makes the user admin.
26+
`/unadmin` | _Master_ | _Everywhere_ | Demotes the user from admin list.
27+
`/leave <name\|id>` | _Master_ | _Everywhere_ | Make the bot to leave the group cleanly.
28+
`/warn <reason>` | _Admin_ | _Groups_ | Warns the user.
29+
`/unwarn` | _Admin_ | _Everywhere_ | Removes the last warn from the user.
30+
`/nowarns` | _Admin_ | _Everywhere_ | Clears warns for the user.
31+
`/getwarns` | _Admin_ | _Everywhere_ | Shows a list of warns for the user.
32+
`/ban <reason>` | _Admin_ | _Groups_ | Bans the user from groups.
33+
`/unban` | _Admin_ | _Everywhere_ | Removes the user from ban list.
34+
`/addcommand` | _Admin_ | _In-Bot_ | Create a custom command.
35+
`/removecommand <name>` | _Admin_ | _In-Bot_ | Remove a custom command.
36+
`/staff` | _Everyone_ | _Everywhere_ | Shows a list of admins.
37+
`/link` | _Everyone_ | _Everywhere_ | Shows the current group's link.
38+
`/groups` | _Everyone_ | _Everywhere_ | Shows a list of groups which the bot is admin in.
39+
`/report` | _Everyone_ | _Everywhere_ | Reports the replied-to message to admins.
40+
`/commands` | _Everyone_ | _In-Bot_ | Shows a list of available commands.
41+
`/help` \| `/start` | _Everyone_ | _In-Bot_ | How to use the bot.
3842

3943
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.
4044

41-
If used by reply, `/ban` and `/warn` remove the replied-to message.
45+
If used by reply, `/ban` and `/warn` would remove the replied-to message.
4246

4347
The bot is still in alpha phase so feel free to open issues and ask for a _feature_.
4448

bot/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

33
const Telegraf = require('telegraf');
4-
const { loadJSON } = require('../utils/json');
5-
const config = loadJSON('config.json');
4+
const config = require('../config.json');
65

76
const bot = new Telegraf(config.token);
87

handlers/commands/addCommand.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
'use strict';
22

33
// DB
4-
const { isAdmin } = require('../../stores/user');
54
const { addCommand } = require('../../stores/command');
65

76
// Bot
87
const { replyOptions } = require('../../bot/options');
98

10-
const addCommandHandler = async ({ chat, message, reply }) => {
11-
const user = message.from;
12-
if (chat.type !== 'private') {
13-
return null;
14-
}
15-
if (!await isAdmin(user)) {
9+
const addCommandHandler = async ({ chat, reply, state }) => {
10+
const { isAdmin, user } = state;
11+
if (chat.type !== 'private') return null;
12+
13+
if (!isAdmin) {
1614
return reply('ℹ️ <b>Sorry, only admins access this command.</b>',
1715
replyOptions);
1816
}
1917
await addCommand({ id: user.id });
20-
return reply('Enter a name for the command.\n\nFor example: <b>rules</b>',
21-
replyOptions);
18+
return reply('Enter a name for the command without forward slash "/".' +
19+
'\n\nFor example: <b>rules</b>',
20+
replyOptions);
2221
};
2322

2423
module.exports = addCommandHandler;

handlers/commands/admin.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict';
22

33
// Utils
4-
const { loadJSON } = require('../../utils/json');
54
const { link } = require('../../utils/tg');
65
const { logError } = require('../../utils/log');
76

8-
// Config
9-
const { masterID } = loadJSON('config.json');
10-
117
// Bot
128
const { replyOptions } = require('../../bot/options');
139

@@ -20,16 +16,15 @@ const {
2016
nowarns
2117
} = require('../../stores/user');
2218

23-
const adminHandler = async ({ message, reply }) => {
24-
if (message.from.id !== masterID) {
25-
return null;
26-
}
19+
const adminHandler = async ({ message, reply, state }) => {
20+
const { isMaster, user } = state;
21+
if (!isMaster) return null;
2722

2823
const userToAdmin = message.reply_to_message
2924
? message.reply_to_message.from
3025
: message.commandMention
3126
? message.commandMention
32-
: message.from;
27+
: user;
3328

3429
if (await isBanned(userToAdmin)) {
3530
return reply('ℹ️ <b>Can\'t admin banned user.</b>', replyOptions);

handlers/commands/ban.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ const { replyOptions } = require('../../bot/options');
1212
const { listGroups } = require('../../stores/group');
1313
const { isAdmin, isBanned, ban } = require('../../stores/user');
1414

15-
const banHandler = async ({ chat, message, reply, telegram, me }) => {
16-
if (!await isAdmin(message.from)) {
17-
return null;
18-
}
15+
const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
16+
if (!state.isAdmin) return null;
1917

2018
const userToBan = message.reply_to_message
2119
? message.reply_to_message.from
@@ -69,7 +67,7 @@ const banHandler = async ({ chat, message, reply, telegram, me }) => {
6967
logError(err);
7068
}
7169

72-
return reply(`🚫 ${link(message.from)} <b>banned</b> ${link(userToBan)} ` +
70+
return reply(`🚫 ${link(state.user)} <b>banned</b> ${link(userToBan)} ` +
7371
`<b>for:</b>\n\n${reason}`, replyOptions);
7472
};
7573

handlers/commands/commands.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ const actions = `\n
2929
/removecommand <code>&lt;name&gt;</code> - to remove a custom command.`;
3030

3131
const commandReferenceHandler = async ({ chat, replyWithHTML }) => {
32-
if (chat.type !== 'private') {
33-
return null;
34-
}
32+
if (chat.type !== 'private') return null;
33+
3534
const customCommands = await listCommands();
3635
const customCommandsText = customCommands.length
3736
? '\n<b>Custom commands:</b>\n' +
3837
customCommands
3938
.filter(command => command.isActive)
4039
.sort((a, b) => a.role < b.role)
41-
.map(command => `[${command.role}] <code>/${command.name}</code>`)
40+
.map(command => `[${command.role}] <code>!${command.name}</code>`)
4241
.join('\n')
4342
: '';
4443

handlers/commands/getwarns.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const { link } = require('../../utils/tg');
77
const { replyOptions } = require('../../bot/options');
88

99
// DB
10-
const { isAdmin, getWarns } = require('../../stores/user');
10+
const { getWarns } = require('../../stores/user');
11+
12+
const getWarnsHandler = async ({ message, reply, state }) => {
13+
const { isAdmin } = state;
14+
if (!isAdmin) return null;
1115

12-
const getWarnsHandler = async ({ message, reply }) => {
13-
if (!await isAdmin(message.from)) {
14-
return null;
15-
}
1616
const theUser = message.reply_to_message
1717
? message.reply_to_message.from
1818
: message.commandMention

handlers/commands/help.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ and @mattatabot might be better choices for you.
1919
`;
2020

2121
const helpHandler = ({ chat, replyWithHTML }) => {
22-
if (chat.type !== 'private') {
23-
return null;
24-
}
22+
if (chat.type !== 'private') return null;
23+
2524
return replyWithHTML(
2625
message,
2726
Markup.inlineKeyboard(

handlers/commands/leave.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
'use strict';
22

3-
const { masterID } = require('../../config.json');
4-
5-
const { removeGroup } = require('../../stores/group');
3+
const { managesGroup, removeGroup } = require('../../stores/group');
64

75
const leaveCommandHandler = async ctx => {
8-
if (ctx.from.id !== masterID) {
9-
return null;
6+
const { chat, message, telegram, state, replyWithHTML } = ctx;
7+
const { isMaster } = state;
8+
if (!isMaster) return null;
9+
10+
const groupName = message.text.split(' ').slice(1).join(' ');
11+
12+
if (groupName) {
13+
const group = /^-?\d+/.test(groupName)
14+
? { id: Number(groupName) }
15+
: { title: groupName };
16+
console.log(group);
17+
const isGroup = await managesGroup(group);
18+
if (!isGroup) {
19+
return replyWithHTML(
20+
'ℹ️ <b>Couldn\'t find a group with that ID/name.</b>'
21+
);
22+
}
23+
await Promise.all([
24+
removeGroup(isGroup),
25+
telegram.leaveChat(isGroup.id)
26+
]);
27+
return replyWithHTML(
28+
'✅ <b>I no longer manage that group.</b>'
29+
);
1030
}
11-
await removeGroup(ctx.chat);
12-
return ctx.telegram.leaveChat(ctx.chat.id);
31+
32+
await removeGroup(chat);
33+
return telegram.leaveChat(chat.id);
1334
};
1435

1536
module.exports = leaveCommandHandler;

0 commit comments

Comments
 (0)