From fdfbfa572718e0cf95ecc8105e7e873b7a36921c Mon Sep 17 00:00:00 2001
From: GingerPlusPlus
The Guard Bot
/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,
};