From fdfbfa572718e0cf95ecc8105e7e873b7a36921c Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Thu, 28 Sep 2017 22:59:35 +0200 Subject: [PATCH] Added /leave --- README.md | 3 ++- handlers/commands/commands.js | 1 + handlers/commands/leave.js | 15 +++++++++++++++ index.js | 2 ++ stores/group.js | 4 ++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 handlers/commands/leave.js diff --git a/README.md b/README.md index f732621..b54cd36 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

The Guard Bot

-The Guard is a Telegram bot made to help admins manage their groups. +The Guard is a Telegram bot made to help admins manage their groups. Initially created to moderate [The Devs Network](https://thedevs.network). @@ -24,6 +24,7 @@ Command | Rule | 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. diff --git a/handlers/commands/commands.js b/handlers/commands/commands.js index 3f3dbeb..a4653f0 100644 --- a/handlers/commands/commands.js +++ b/handlers/commands/commands.js @@ -4,6 +4,7 @@ const commandReference = `\ Master commands: /admin - Makes the user admin. /unadmin - Demotes the user from admin list. +/leave - Makes the bot leave the group cleanly. Admin commands: /warn <reason> - Warns the user. diff --git a/handlers/commands/leave.js b/handlers/commands/leave.js new file mode 100644 index 0000000..f06f052 --- /dev/null +++ b/handlers/commands/leave.js @@ -0,0 +1,15 @@ +'use strict'; + +const { masterID } = require('../../config.json'); + +const { removeGroup } = require('../../stores/group'); + +const leaveCommandHandler = async ctx => { + if (ctx.from.id !== masterID) { + return null; + } + await removeGroup(ctx.chat); + return ctx.telegram.leaveChat(ctx.chat.id); +}; + +module.exports = leaveCommandHandler; diff --git a/index.js b/index.js index b4fbb17..aeed837 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ const addedToGroupHandler = require('./handlers/middlewares/addedToGroup'); // Commmands Handlers const adminHandler = require('./handlers/commands/admin'); const unAdminHandler = require('./handlers/commands/unadmin'); +const leaveCommandHandler = require('./handlers/commands/leave'); const warnHandler = require('./handlers/commands/warn'); const unwarnHandler = require('./handlers/commands/unwarn'); const nowarnsHandler = require('./handlers/commands/nowarns'); @@ -45,6 +46,7 @@ bot.on('new_chat_members', antibotHandler); bot.on([ 'new_chat_members', 'left_chat_member' ], deleteAfter(10 * 60 * 1000)); bot.command('admin', adminHandler); bot.command('unadmin', unAdminHandler); +bot.command('leave', leaveCommandHandler); bot.command('warn', warnHandler); bot.command('unwarn', unwarnHandler); bot.command('nowarns', nowarnsHandler); diff --git a/stores/group.js b/stores/group.js index 723ed28..d458606 100644 --- a/stores/group.js +++ b/stores/group.js @@ -21,8 +21,12 @@ const listGroups = () => const managesGroup = group => Group.findOne({ id: group.id }); +const removeGroup = ({ id }) => + Group.remove({ id }); + module.exports = { addGroup, listGroups, managesGroup, + removeGroup, };