Skip to content

Commit fdfbfa5

Browse files
committed
Added /leave
1 parent 36f4180 commit fdfbfa5

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="http://imageupload.co.uk/images/2017/09/28/zzzzz.png" width="180" height="180">
33
<h1 align="center">The Guard Bot</h1>
44
</p>
5-
The Guard is a Telegram bot made to help admins manage their groups.
5+
The Guard is a Telegram bot made to help admins manage their groups.
66

77
Initially created to moderate [The Devs Network](https://thedevs.network).
88

@@ -24,6 +24,7 @@ Command | Rule | Description
2424
-------- | ------ | -----------
2525
`/admin` | _Master_ | Makes the user admin.
2626
`/unadmin` | _Master_ | Demotes the user from admin list.
27+
`/leave` | _Master_ | Makes the bot leave the group cleanly.
2728
`/warn <reason>` | _Admin_ | Warns the user.
2829
`/unwarn` | _Admin_ | Removes the last warn from the user.
2930
`/nowarns` | _Admin_ | Clears warns for the user.

handlers/commands/commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const commandReference = `\
44
<b>Master commands</b>:
55
<code>/admin</code> - Makes the user admin.
66
<code>/unadmin</code> - Demotes the user from admin list.
7+
<code>/leave</code> - Makes the bot leave the group cleanly.
78
89
<b>Admin commands</b>:
910
<code>/warn &lt;reason&gt;</code> - Warns the user.

handlers/commands/leave.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const { masterID } = require('../../config.json');
4+
5+
const { removeGroup } = require('../../stores/group');
6+
7+
const leaveCommandHandler = async ctx => {
8+
if (ctx.from.id !== masterID) {
9+
return null;
10+
}
11+
await removeGroup(ctx.chat);
12+
return ctx.telegram.leaveChat(ctx.chat.id);
13+
};
14+
15+
module.exports = leaveCommandHandler;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const addedToGroupHandler = require('./handlers/middlewares/addedToGroup');
2323
// Commmands Handlers
2424
const adminHandler = require('./handlers/commands/admin');
2525
const unAdminHandler = require('./handlers/commands/unadmin');
26+
const leaveCommandHandler = require('./handlers/commands/leave');
2627
const warnHandler = require('./handlers/commands/warn');
2728
const unwarnHandler = require('./handlers/commands/unwarn');
2829
const nowarnsHandler = require('./handlers/commands/nowarns');
@@ -45,6 +46,7 @@ bot.on('new_chat_members', antibotHandler);
4546
bot.on([ 'new_chat_members', 'left_chat_member' ], deleteAfter(10 * 60 * 1000));
4647
bot.command('admin', adminHandler);
4748
bot.command('unadmin', unAdminHandler);
49+
bot.command('leave', leaveCommandHandler);
4850
bot.command('warn', warnHandler);
4951
bot.command('unwarn', unwarnHandler);
5052
bot.command('nowarns', nowarnsHandler);

stores/group.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ const listGroups = () =>
2121
const managesGroup = group =>
2222
Group.findOne({ id: group.id });
2323

24+
const removeGroup = ({ id }) =>
25+
Group.remove({ id });
26+
2427
module.exports = {
2528
addGroup,
2629
listGroups,
2730
managesGroup,
31+
removeGroup,
2832
};

0 commit comments

Comments
 (0)