2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-30 13:47:54 +00:00

Syncing status

On join, apply admin and banned status
This commit is contained in:
GingerPlusPlus
2017-10-21 18:13:34 +02:00
parent 0bd4e2244b
commit 0d52ff3e76
2 changed files with 44 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
'use strict';
const { getUser } = require('../../stores/user');
const syncStatusHandler = (ctx, next) => {
const { message } = ctx;
const { new_chat_members } = message;
new_chat_members.forEach(async newMember => {
if (newMember.is_bot) {
return null;
}
const dbUser = await getUser({ id: newMember.id });
// this handler runs after messages/addUser
// so, it should always be able to obtain the user from db
console.assert(dbUser);
switch (dbUser.status) {
case 'admin':
return ctx.telegram.promoteChatMember(ctx.chat.id, dbUser.id, {
can_change_info: false,
can_delete_messages: true,
can_invite_users: true,
can_pin_messages: true,
can_promote_members: true,
can_restrict_members: true,
});
case 'banned':
return ctx.telegram.kickChatMember(ctx.chat.id, dbUser.id);
case 'member':
// do nothing
return null;
default:
throw new Error(`Unexpected member status: ${dbUser.status}`);
}
});
return next();
};
module.exports = syncStatusHandler;

View File

@@ -36,6 +36,7 @@ const command = './handlers/commands';
const leaveUnmanagedHandler = require(`${middleware}/leaveUnmanaged`);
const removeCommandsHandler = require(`${middleware}/removeCommands`);
const kickBannedHandler = require(`${middleware}/kickBanned`);
const syncStatusHandler = require(`${middleware}/syncStatus`);
const antibotHandler = require(`${middleware}/antibot`);
const addedToGroupHandler = require(`${middleware}/addedToGroup`);
@@ -82,6 +83,7 @@ bot.on('message',
addCustomCmdHandler,
runCustomCmdHandler
);
bot.on('new_chat_members', syncStatusHandler);
bot.on('new_chat_members', antibotHandler);
bot.on([ 'new_chat_members', 'left_chat_member' ], deleteAfter(delTimeout));
bot.command('admin', adminHandler);