Beispiel #1
0
def event_add(bot_id, group_id, user_id, message, key, value, **argv):
    MessageLogs.add(group_id, user_id, nAIset=1)  #紀錄次數
    filter_fuck = (group_id is None
                   or UserSettings.get(group_id, 'filter_fuck', True))
    if filter_fuck and isValueHaveKeys(message, cfg['詞組']['髒話']):
        return '愛醬覺得說髒話是不對的!!'

    if key is None:
        return cfg['學習說明']

    key = key.lower()
    if value is None or value == '':
        reply_message = ['<%s>' % key]

        if group_id is not None:
            data = UserKeyword.get(group_id, key)
            if data is not None:
                reply_message.append('群組=%s' % data.reply)

        if user_id is not None:
            data = UserKeyword.get(user_id, key)
            if data is not None:
                reply_message.append('個人=%s' % data.reply)

        return '\n'.join(reply_message) if len(
            reply_message) > 1 else '喵喵喵? 愛醬不記得<%s>' % (key)

    while '***' in key:
        key = key.replace('***', '**')
    while '|||' in key:
        key = key.replace('|||', '||')
    while '___' in key:
        key = key.replace('___', '__')

    for i in value.replace('__', '||').split('||'):
        i = i.strip()
        if i[:4] == 'http' and not is_image_and_ready(i):
            return '<%s>\n愛醬發現圖片網址是錯誤的\n請使用格式(jpg, png)\n短網址或網頁嵌圖片可能無效\n必須使用https' % i
        break  #如果全部都檢查時間會太久 只幫第一個檢查格式 通常使用者圖床也會使用同一個 應該不會有問題

    reply_message = ['<%s>記住了喔 ' % key]

    if group_id is not None and UserKeyword.add_and_update(
            group_id, user_id, key, value):
        reply_message.append('(群組)')

    if user_id is not None and UserKeyword.add_and_update(
            user_id, user_id, key, value):
        reply_message.append('(個人)')
    else:
        reply_message.append('(不儲存個人)\nhttps://goo.gl/zTwaL2')

    return ''.join(reply_message)
Beispiel #2
0
    def add(self, plus=False):
        '''
            新增關鍵字
        '''
        if self.key is None:
            return text['學習說明']

        #文字處理1
        self.key = self.key.lower()
        while '***' in self.key:
            self.key = self.key.replace('***', '**')

        #查詢
        if self.value is None:
            if self.group:
                row = UserKeyword.get(self.group.id, self.key)
            else:
                row = UserKeyword.get(self.user.id, self.key)
            if row:
                return text_format(text['關鍵字查詢成功'],
                                   key=self.key,
                                   value=row.reply)
            else:
                return text_format(text['關鍵字查詢失敗'], key=self.key)

        self._count({'調教': 1})  #紀錄次數

        #文字處理2
        while '|||' in self.value:
            self.value = self.key.replace('|||', '||')
        while '___' in self.value:
            self.value = self.key.replace('___', '__')

        #新增
        ban_key = ['**', '** **', '愛醬**', '**愛醬**']
        if self.key in ban_key:
            return '%s\n%s' % (text['關鍵字禁用'], text['分隔符'].join(ban_key))

        if self.value[:2] == '##':
            return '由於規則問題 沒辦法使用##開頭的內容喔'

        if self.key != text['名稱'] and self.key[:2] == text['名稱']:
            self.key = self.key[2:].strip(' \n')

        #保護模式過濾 之後option寫入database將此邏輯合併計算中
        n = self.value.rfind('##')
        if n > -1 and '保護' in self.value[
                n:] and self.key[:2] == '**' and self.key[-2:] == '**':
            return '為了避免過度觸發\n保護模式關鍵字不接受前後**喔'

        reply_message = [
            '%s 新增 <%s> ' % (self.user.name if self.user else '', self.key)
        ]

        try:
            if self.group:
                UserKeyword.add_and_update(self.group_id,
                                           self.user_id,
                                           self.key,
                                           self.value,
                                           plus=plus)
            else:
                UserKeyword.add_and_update(self.user_id,
                                           self.user_id,
                                           self.key,
                                           self.value,
                                           plus=plus)

        except Exception as e:
            return '學習失敗: %s' % str(e)

        level = len(self.key) - self.key.count('**') * (
            len('**') + 1)  #database的UserKeyword.level 懶得改上面
        if level < 0:
            reply_message.append('\n愛醬非常不建議這種會過度觸發的詞喔\n請慎用')
        elif level == 0:
            reply_message.append('\n這種容易觸發的詞容易造成過多訊息喔\n請注意使用')
        elif level >= 7:
            reply_message.append('\n這種詞命中率較低喔 請善加利用萬用字元雙米號')

        if '*' in self.key and '**' not in self.key:
            reply_message.append(
                '\n愛醬發現你似乎要使用萬用字元?\n如果是的話請把 *(單米號) 換成 **(雙米號)')
        if '_' in self.value and self.value.count('__') == 0:
            reply_message.append(
                '\n愛醬發現你似乎要使用隨機模式?\n如果是的話請把 _(單底線) 換成 __(雙底線)')

        #for i in self.value.replace('__', '||').split('||'):
        #    i = i.strip()
        #    if i[:4] == 'http' and not is_image_and_ready(i):
        #        reply_message.append('<%s>\n愛醬發現圖片網址是錯誤的\n請使用格式(jpg, png)\n短網址或網頁嵌圖片可能無效\n必須使用https' % i)
        #    break #如果全部都檢查時間會太久 只幫第一個檢查格式 通常使用者圖床也會使用同一個 應該不會有問題

        if self.group is None:
            reply_message.append(
                '\n現在個人詞庫預設是不會在群組觸發的喔\n請在群組設定開啟全回應模式(預設開)或開啟個人詞庫(預設關)')
        else:
            #保護模式提醒 之後option寫入database將此邏輯合併計算中
            n = self.value.rfind('##')
            if n > -1 and '保護' in self.value[n:]:
                reply_message.append('\n(此為保護關鍵字 只有你可以刪除及修改 為了避免爭議 建議不要濫用)')

        return ''.join(reply_message) \
            + '\n\n使用「網頁設定」更好操作'
Beispiel #3
0
def api_keyword(method):
    if 'token' not in session:
        abort(400)

    uid = session['uid'] #request.args.get('user_id')
    wd = WebUI.query.get(uid)
    keyword_mode = request.json.get('mode')
    _id = uid if keyword_mode == '個人' else wd.gid if wd and (datetime.now()-wd.gid_timeout).total_seconds() < 0 else None
    
        
    if method == 'get':
        if _id is None:
            return jsonify({
                'result':[],
                'total':0,
            })

        keywords = UserKeyword.get(_id)
        page = int(request.json.get('page', 1)) - 1
        length = int(request.json.get('length', len(keywords)))
        search = request.json.get('search', '')

        keywords.reverse()

        results = []
        for row in keywords:
            if search == '' or search in row.keyword or search in row.reply:
                results.append({
                    #'id':row._id,
                    'keyword':row.keyword,
                    'reply':row.reply,
                })

        return jsonify({
            'result':results[page*length : page*length+length],
            'total':len(results),
        })

    if method == 'add':
        keyword = request.json.get('keyword')
        reply = request.json.get('reply')

        try:
            UserKeyword.add_and_update(_id, uid, keyword, reply)
            db.session.commit()

            return 'ok'
        except Exception as e:
            abort(400)
            abort(Response(str(e)))

    if method == 'edit':
        old_keyword = request.json.get('old_keyword')
        keyword = request.json.get('keyword')
        reply = request.json.get('reply')

        try:
            if UserKeyword.add_and_update(_id, uid, keyword, reply):
                UserKeyword.delete(_id, uid, old_keyword)
            db.session.commit()

            return 'ok'
        except Exception as e:
            abort(400)
            abort(Response(str(e)))
        
    if method == 'delete':
        keyword = request.json.get('keyword')

        try:
            UserKeyword.delete(_id, uid, keyword)
            db.session.commit()

            return 'ok'
        except Exception as e:
            abort(400)
            abort(Response(str(e)))