Skip to content

Commit 0a76e28

Browse files
committed
Merge branch 'develop'
2 parents 511cc50 + 23ac796 commit 0a76e28

22 files changed

+524
-134
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2-
data/*
2+
data/
33
config.json
4-
.env
4+
.env
5+
.idea/

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Command | Role | Description
3232
`/ban <reason>` | _Admin_ | Bans the user from groups.
3333
`/unban` | _Admin_ | Removes the user from ban list.
3434
`/staff` | _All_ | Shows a list of admins.
35+
`/link` | _All_ | Show the current group's link.
3536
`/groups` | _All_ | Show a list of groups which the bot is admin in.
3637
`/report` | _All_ | Reports the replied-to message to admins.
3738

@@ -41,6 +42,8 @@ If used by reply, `/ban` and `/warn` remove the replied-to message.
4142

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

45+
[**Roadmap**](https://github.com/TheDevs-Network/the-guard-bot/projects/1)
46+
4447
---
4548

4649
The Guard icon from [Entypo+](http://entypo.com/) by Daniel Bruce.

TODO.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

handlers/commands/addCommand.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// DB
4+
const { isAdmin } = require('../../stores/user');
5+
const { addCommand } = require('../../stores/command');
6+
7+
// Bot
8+
const { replyOptions } = require('../../bot/options');
9+
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)) {
16+
return reply('ℹ️ <b>Sorry, only admins access this command.</b>',
17+
replyOptions);
18+
}
19+
await addCommand({ id: user.id });
20+
return reply('Enter a name for the command.\n\nFor example: <b>rules</b>',
21+
replyOptions);
22+
};
23+
24+
module.exports = addCommandHandler;

handlers/commands/ban.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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 }) => {
15+
const banHandler = async ({ chat, message, reply, telegram, me }) => {
1616
if (!await isAdmin(message.from)) {
1717
return null;
1818
}
@@ -29,6 +29,10 @@ const banHandler = async ({ chat, message, reply, telegram }) => {
2929
replyOptions);
3030
}
3131

32+
if (message.chat.type === 'private' || userToBan.username === me) {
33+
return null;
34+
}
35+
3236
if (reason.length === 0) {
3337
return reply('ℹ️ <b>Need a reason to ban.</b>', replyOptions);
3438
}

handlers/commands/commands.js

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

3+
// DB
4+
const { listCommands } = require('../../stores/command');
5+
36
const commandReference = `\
47
<b>Master commands</b>:
58
<code>/admin</code> - Makes the user admin.
@@ -16,15 +19,30 @@ const commandReference = `\
1619
1720
<b>Commands for everyone</b>:
1821
<code>/staff</code> - Shows a list of admins.
22+
<code>/link</code> - Show the current group's link.
1923
<code>/groups</code> - Show a list of groups which the bot is admin in.
2024
<code>/report</code> - Reports the replied-to message to admins.
2125
`;
2226

23-
const commandReferenceHandler = ({ chat, replyWithHTML }) => {
27+
const actions = `\n
28+
/addcommand - to create custom commands.
29+
/removecommand <code>&lt;name&gt;</code> - to remove a custom command.`;
30+
31+
const commandReferenceHandler = async ({ chat, replyWithHTML }) => {
2432
if (chat.type !== 'private') {
2533
return null;
2634
}
27-
return replyWithHTML(commandReference);
35+
const customCommands = await listCommands();
36+
const customCommandsText = customCommands.length
37+
? '\n<b>Custom commands:</b>\n' +
38+
customCommands
39+
.filter(command => command.isActive)
40+
.sort((a, b) => a.role < b.role)
41+
.map(command => `[${command.role}] <code>/${command.name}</code>`)
42+
.join('\n')
43+
: '';
44+
45+
return replyWithHTML(commandReference + customCommandsText + actions);
2846
};
2947

3048
module.exports = commandReferenceHandler;

handlers/commands/groups.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Utils
44
const { escapeHtml } = require('../../utils/tg');
55

6+
// Bot
7+
const bot = require('../../bot');
8+
69
// DB
710
const { listGroups } = require('../../stores/group');
811

@@ -16,19 +19,23 @@ const entry = group => group.username
1619
? `- @${group.username}`
1720
: `- <a href="${group.link}">${escapeHtml(group.title)}</a>`;
1821

19-
const groupsHandler = async ctx => {
22+
const groupsHandler = async ({ chat, replyWithHTML }) => {
2023
if (config.groupsString) {
21-
return ctx.replyWithHTML(config.groupsString);
24+
return replyWithHTML(config.groupsString);
2225
}
2326

2427
const groups = await listGroups();
2528

2629
const entries = groups.map(entry).join('\n');
2730

28-
return ctx.replyWithHTML(`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
29-
disable_web_page_preview: true,
30-
reply_markup,
31-
});
31+
const { message_id } = await replyWithHTML(
32+
`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
33+
disable_web_page_preview: true,
34+
reply_markup,
35+
});
36+
37+
return setTimeout(() =>
38+
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
3239

3340
};
3441

handlers/commands/link.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// Bot
4+
const bot = require('../../bot');
5+
6+
// DB
7+
const { managesGroup } = require('../../stores/group');
8+
9+
const linkHandler = async ({ chat, replyWithHTML }) => {
10+
const group = await managesGroup(chat);
11+
12+
const { message_id } = await replyWithHTML(
13+
'ℹ️ <b>Group\'s link:</b>\n\n' +
14+
`<a href="${group.link}">${group.title}</a>`
15+
);
16+
return setTimeout(() =>
17+
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
18+
};
19+
20+
module.exports = linkHandler;

handlers/commands/removeCommand.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
// Utils
4+
const { loadJSON } = require('../../utils/json');
5+
6+
// Config
7+
const { masterID } = loadJSON('config.json');
8+
9+
// DB
10+
const { isAdmin } = require('../../stores/user');
11+
const { getCommand, removeCommand } = require('../../stores/command');
12+
13+
// Bot
14+
const { replyOptions } = require('../../bot/options');
15+
16+
const removeCommandHandler = async ({ chat, message, reply }) => {
17+
const user = message.from;
18+
const { text } = message;
19+
if (chat.type !== 'private') {
20+
return null;
21+
}
22+
if (!await isAdmin(user)) {
23+
return reply('ℹ️ <b>Sorry, only admins access this command.</b>',
24+
replyOptions);
25+
}
26+
const [ , commandName ] = text.split(' ');
27+
if (!commandName) {
28+
return reply(
29+
'Enter a command name to remove.\n\n' +
30+
'For example:\n/removecommand <b>rules</b>',
31+
replyOptions);
32+
}
33+
34+
const command = await getCommand({ name: commandName });
35+
if (!command) {
36+
return reply('ℹ️ <b>Command couldn\'t be found.</b>',
37+
replyOptions);
38+
}
39+
40+
if (command.role === 'Master' && user.id !== masterID) {
41+
return reply('ℹ️ <b>Sorry, only master can remove this command.</b>',
42+
replyOptions);
43+
}
44+
45+
await removeCommand({ name: commandName });
46+
return reply(
47+
`✅ <code>/${commandName}</code> ` +
48+
'<b>has been removed successfully.</b>',
49+
replyOptions);
50+
};
51+
52+
module.exports = removeCommandHandler;

handlers/commands/unwarn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const unwarnHandler = async ({ message, reply }) => {
3737
return reply(
3838
`❎ ${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
3939
`<b>for:</b>\n\n${allWarns[allWarns.length - 1]}` +
40-
`(${allWarns.length}/3)`,
40+
`(${allWarns.length - 1}/3)`,
4141
replyOptions);
4242
};
4343

0 commit comments

Comments
 (0)