Exemplo n.º 1
0
    def deletemsg(self, update: Update, context: CallbackContext) -> bool:
        """用于删除消息,清空当前对话框中没有用的消息。
        bot可以删除任意私聊消息,无论是来自用户还是bot。
        如果是群内使用该指令,需要管理员或KP权限,
        以及bot是管理员,此时可以删除群内的任意消息。

        当因为各种操作产生了过多冗杂消息的时候,使用
        `/delmsg --msgnumber`将会删除:delmsg指令的消息
        以及该指令上面的msgnumber条消息。例如:
        `/delmsg 2`将删除包含delmsg指令在内的3条消息。
        没有参数的时候,`/delmsg`默认删除指令和指令的上一条消息。

        因为要进行连续的删除请求,删除的时间会稍微有些滞后,
        请不要重复发送该指令,否则可能造成有用的消息丢失。
        如果感觉删除没有完成,请先随意发送一条消息来拉取删除情况,
        而不是继续用`/delmsg`删除。"""

        delnum = 1
        chatid = getchatid(update)

        if isgroup(update) and not self.isadmin(self.lastchat, BOT_ID):
            return self.errorInfo("Bot没有管理权限")

        if isgroup(update) and self.checkaccess(self.forcegetplayer(update), self.forcegetgroup(update)) & (GROUPKP | GROUPADMIN) == 0:
            return self.errorInfo("没有权限", True)

        if len(context.args) >= 1:
            if not isint(context.args[0]) or int(context.args[0]) <= 0:
                return self.errorInfo("参数错误", True)
            delnum = int(context.args[0])
            if delnum > 10:
                return self.errorInfo("一次最多删除10条消息")

        lastmsgid = self.lastmsgid
        while delnum >= 0:  # 这是因为要连同delmsg指令的消息也要删掉
            if lastmsgid < -100:
                break
            try:
                context.bot.delete_message(
                    chat_id=chatid, message_id=lastmsgid)
            except Exception as e:
                if str(e).find("can't be deleted for everyone") != -1:
                    self.errorInfo("消息删除失败,发送时间较久的消息无法删除")
                    break
                lastmsgid -= 1
            else:
                delnum -= 1
                lastmsgid -= 1

        update.effective_chat.send_message("删除完成").delete()
        return True
Exemplo n.º 2
0
    def link(self, update: Update, context: CallbackContext) -> bool:
        """获取群邀请链接,并私聊发送给用户。

        使用该指令必须要满足两个条件:指令发送者和bot都是该群管理员。"""

        if not isgroup(update):
            return self.errorInfo("在群聊使用该指令。")
        if not self.isadmin(self.lastchat, BOT_ID):
            return self.errorInfo("Bot没有权限")
        if not self.isadmin(self.lastchat, self.lastuser):
            return self.errorInfo("没有权限", True)

        adminid = self.lastuser
        gpid = update.effective_chat.id
        chat = context.bot.get_chat(chat_id=gpid)
        ivlink = chat.invite_link
        if not ivlink:
            ivlink = context.bot.export_chat_invite_link(chat_id=gpid)

        try:
            self.reply(
                chat_id=adminid, text="群:"+chat.title+"的邀请链接:\n"+ivlink)
        except Exception:
            return self.errorInfo("邀请链接发送失败!")

        rtbutton = [[InlineKeyboardButton(
            text="跳转到私聊", callback_data="None", url="t.me/"+self.bot.username)]]
        rp_markup = InlineKeyboardMarkup(rtbutton)

        self.reply("群邀请链接已经私聊发送。", reply_markup=rp_markup)
        return True