orloow777
@orloow777
Программист.

Не исключает после блокировки?

Всем доброго времени суток! Есть у меня бот, на python, для контролирования конференции ВКонтакте.
И вот в нём, есть такая команда "+ban" и когда я её ввожу, то пользователя блокирует в конференции но не исключает (если он там есть). Как сделать, чтобы исключало?

Ниже дал вам код команд "+кик" (исключение) и "+ban"

+ban:
from handler.base_plugin import BasePlugin

from vk.helpers import parse_user_id


class AdminPlugin(BasePlugin):
    __slots__ = ("admins", "moders", "banset", "prefixes", "commands", "setadmins")

    def __init__(self, commands=None, admins=None, moders=None, banset=None, prefixes=(), setadmins=True):
        """Allows admins to ban people and control admins for plugins.
        Admins are global.
        Moders are local for chats"""

        super().__init__()

        self.commands = list(commands) if commands else ["ban", "unban", "addadmin", "unadmin", "addmoder", "unmoder", "banlist", "admins"]
        self.admins = list(admins) if admins else list()
        self.moders =  moders if moders else {}
        self.banset = list(banset) if banset else list()
        self.setadmins = setadmins
        self.prefixes = prefixes

        self.load()

    def initiate(self):
        if not self.setadmins:
            return

        for plugin in self.handler.plugins:
            if hasattr(plugin, "admins"):
                plugin.admins = self.admins

    def get_pathes(self):
        return self.get_path("/admins.notjson"), self.get_path("/moders.notjson"), self.get_path("/batset.notjson")

    def stop(self):
        self.save()

    def load(self):
        path_admins, path_moders, path_banset = self.get_pathes()

        try:
            with open(path_admins, "r") as o:
                for u in o.read().split(","):
                    if u and int(u) not in self.admins: self.admins.append(int(u))
        except FileNotFoundError:
            pass

        try:
            with open(path_moders, "r") as o:
                current_chat = None
                for u in o.read().split(","):
                    if not u:
                        continue

                    if u[:2] == "ci":
                        current_chat = int(u[2:])
                    else:
                        moders = self.moders.get(current_chat, [])

                        if int(u) not in moders:
                            moders.append(int(u))
                            self.moders[current_chat] = moders
        except FileNotFoundError:
            pass

        try:
            with open(path_banset, "r") as o:
                for u in o.read().split(","):
                    if u and int(u) not in self.banset and int(u) not in self.admins: self.banset.append(u)
        except FileNotFoundError:
            pass

    def save(self):
        path_admins, path_moders, path_banset = self.get_pathes()

        with open(path_admins, "w") as o:
            for u in self.admins:
                o.write(str(u) + ",")

        with open(path_moders, "w") as o:
            for k, v in self.moders.items():
                o.write("ci" + str(k) + ",")

                for u in v:
                    o.write(str(u) + ",")

        with open(path_banset, "w") as o:
            for u in self.banset:
                o.write(str(u) + ",")

    async def check_message(self, msg):
        return any(c in msg.text for c in self.commands)

    async def process_message(self, msg):
        text = msg.text

        for p in self.prefixes:
            if msg.text.startswith(p):
                text = text.replace(p, "", 1)
                break

        else:
            return

        if text == self.commands[6]:
            users = ""

            temp = ""
            temp_amount = 0

            for bu in self.banset:
                temp += bu + ","
                temp_amount += 1

                if temp_amount > 99:
                    for u in (await self.api.users.get(user_ids=temp) or []):
                        users += u["first_name"] + " " + u["last_name"] + f" ({u['id']}), "

                    temp = ""
                    temp_amount = 0

            if temp_amount:
                for u in (await self.api.users.get(user_ids=temp) or []):
                    users += u["first_name"] + " " + u["last_name"] + f" ({u['id']}), "

            return await msg.answer("Заблокированные пользователи:\n" + (users[:-2] if users[:-2] else "Нет"))

        if text == self.commands[7]:
            a_users = ""
            for u in (await self.api.users.get(user_ids=",".join(str(u) for u in self.admins)) or []):
                a_users += u["first_name"] + " " + u["last_name"] + f" ({u['id']}), "

            m_users = ""
            for u in (await self.api.users.get(user_ids=",".join(str(u) for u in msg.meta["moders"])) or []):
                m_users += u["first_name"] + " " + u["last_name"] + f" ({u['id']}), "

            return await msg.answer("Администраторы:\n" + (a_users[:-2] if a_users[:-2] else "Нет") + "\n"
                                    "Модераторы:\n" + (m_users[:-2] if m_users[:-2] else "Нет"))

        if not msg.meta["is_admin"] and not msg.meta["is_moder"]:
            return await msg.answer("Вы не администратор!")

        puid = await parse_user_id(msg)

        if not puid:
            return await msg.answer(f"ID указан не верно")

        if text.startswith(self.commands[0]):
            if puid in self.banset:
                return await msg.answer("Пользователь уже заблокирован")

            if puid in self.admins or puid in msg.meta["moders"]:
                return await msg.answer("Блокировка администраторов запрещена")

            self.banset.append(puid)
            return await msg.answer(f"{puid} заблокирован")

        if text.startswith(self.commands[1]):
            if puid not in self.banset:
                return await msg.answer(f"{puid} не заблокирован")
            else:
                self.banset.remove(puid)
                return await msg.answer(f"{puid} разблокирован")

        if text.startswith(self.commands[2]):
            if not msg.meta["is_admin"]:
                return await msg.answer(f"Вы не администратор")

            if len(self.admins) > 99:
                return await msg.answer(f"Уже максимум администраторов")

            if puid in self.admins:
                return await msg.answer("Уже администратор")

            if puid in self.banset:
                return await msg.answer("Этот пользователь забанен")

            self.admins.append(puid)
            return await msg.answer(f"{puid} выданы права администрирования")

        if text.startswith(self.commands[3]):
            if puid not in self.admins:
                return await msg.answer(f"{puid} не администратор")
            else:
                self.admins.remove(puid)
                return await msg.answer(f"{puid} сняты права администратора")

        if text.startswith(self.commands[4]):
            if len(msg.meta["moders"]) > 50:
                return await msg.answer(f"Уже максимум модераторов")

            if puid in msg.meta["moders"]:
                return await msg.answer("Уже модератор")

            msg.meta["moders"].append(puid)
            return await msg.answer(f"{puid} выданы права модератора")

        if text.startswith(self.commands[5]):
            if puid not in msg.meta["moders"]:
                return await msg.answer(f"{puid} не модератор")
            else:
                msg.meta["moders"].remove(puid)
                return await msg.answer(f"{puid} сняты права модератора")

    async def global_before_message(self, msg, plugin):
        for n, s in (("admin", self.admins), ("banned", self.banset)):
            msg.meta[f"is_{n}"] = msg.user_id in s

        moders = self.moders.get(msg.chat_id, [])
        msg.meta[f"is_moder"] = msg.user_id in moders
        self.moders[msg.chat_id] = moders

        msg.meta["admins"] = self.admins
        msg.meta["moders"] = self.moders[msg.chat_id]
        msg.meta["banset"] = self.banset

        return not msg.meta["is_banned"]
  • Вопрос задан
  • 59 просмотров
Пригласить эксперта
Ответы на вопрос 1
orloow777
@orloow777 Автор вопроса
Программист.
+кик:
from handler.base_plugin_command import CommandPlugin
from vk.utils import EventType
from vk.helpers import parse_user_id

import time


class ChatKickerPlugin(CommandPlugin):
    __slots__ = ("admins", "exiled", "admins_only", "unkick")

    def __init__(self, commands=None, free_commands=None, prefixes=None, strict=False, admins=(), admins_only=True):
        """Allows admins to kick users for short amount of time.
        [prefix][command] [time in seconds if kicking]"""

        if not commands:
            commands = ["кик", "kick"]

        super().__init__(*(commands + free_commands), prefixes=prefixes, strict=strict)

        self.admins_only = admins_only
        self.unkick = free_commands
        self.admins = admins
        self.exiled = {}

    async def process_message(self, msg):
        if not msg.is_multichat:
            return await msg.answer("Данную команду можно использовать только в беседах!")

        if self.admins_only and msg.user_id not in self.admins and not msg.meta.get("is_moder"):
            return await msg.answer("Вы не являетесь модератором/администратором.")

        command, text = self.parse_message(msg)

        if command in self.unkick:
            inv = True
        else:
            inv = False

        parts = text.split(" ")
        kick_time = 0
        puid = None

        puid = await parse_user_id(msg)

        if len(parts) > 0 and parts[0].isdigit():
            if puid is not None:
                kick_time = int(parts[0])

            else:
                puid = int(parts[0])

        if not puid:
            return await msg.answer(f"ID указан не верно")

        if inv:
            if puid in self.exiled:
                del self.exiled[puid]

            return await self.api.messages.addChatUser(chat_id=msg.chat_id, user_id=puid)

        if len(parts) > 1 and " ".join(parts[1:]).isdigit():
            kick_time = int(" ".join(parts[1:]))

        self.exiled[puid] = time.time() + kick_time

        await self.api.messages.removeChatUser(chat_id=msg.chat_id, user_id=puid)

    async def check_event(self, evnt):
        return evnt.type == EventType.ChatChange

    async def process_event(self, evnt):
        if evnt.source_act == "chat_invite_user" and evnt.source_mid in self.exiled:
            dtime = self.exiled[evnt.source_mid] - time.time()

            if dtime > 0:
                await self.bot.api.messages.send(chat_id=evnt.chat_id, message=f"Вы сможете вернуться в чат "
                                                                               f"через {int(dtime)} секунд(у/ы)")

                return await self.api.messages.removeChatUser(chat_id=evnt.chat_id, user_id=evnt.source_mid)

            del self.exiled[evnt.source_mid]

        return False
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы