Example #1
0
    def find_by(self, gid=None, cid=None, uid=None):
        cond_str = []
        cond_tup = []
        if not gid is None:
            cond_str.append('gid=?')
            cond_tup.append(gid)
        if not cid is None:
            cond_str.append('cid=?')
            cond_tup.append(cid)
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)

        if 0 == len(cond_tup):
            return self.find_all()

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                ret = conn.execute(
                    '''
                    SELECT {1} FROM {0} WHERE {2}
                    '''.format(self._table, self._columns, cond_str),
                    cond_tup).fetchall()
                return [self.row2item(r) for r in ret]
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[MemberDao.find_by] {e}')
                raise DatabaseError('查找成员失败')
Example #2
0
    def gen_icon_img(self, size, star_slot_verbose=True) -> Image:
        try:
            pic = self.icon.open().convert('RGBA').resize((size, size),
                                                          Image.LANCZOS)
        except FileNotFoundError:
            logger.error(f'File not found: {self.icon.path}')
            pic = unknown_chara_icon.convert('RGBA').resize((size, size),
                                                            Image.LANCZOS)

        l = size // 6
        star_lap = round(l * 0.15)
        margin_x = (size - 6 * l) // 2
        margin_y = round(size * 0.05)
        if self.star:
            for i in range(5 if star_slot_verbose else min(self.star, 5)):
                a = i * (l - star_lap) + margin_x
                b = size - l - margin_y
                s = gadget_star if self.star > i else gadget_star_dis
                s = s.resize((l, l), Image.LANCZOS)
                pic.paste(s, (a, b, a + l, b + l), s)
            if 6 == self.star:
                a = 5 * (l - star_lap) + margin_x
                b = size - l - margin_y
                s = gadget_star_pink
                s = s.resize((l, l), Image.LANCZOS)
                pic.paste(s, (a, b, a + l, b + l), s)
        if self.equip:
            l = round(l * 1.5)
            a = margin_x
            b = margin_x
            s = gadget_equip.resize((l, l), Image.LANCZOS)
            pic.paste(s, (a, b, a + l, b + l), s)
        return pic
Example #3
0
    def delete_by(self, gid=None, cid=None, uid=None):
        cond_str = []
        cond_tup = []
        if not gid is None:
            cond_str.append('gid=?')
            cond_tup.append(gid)
        if not cid is None:
            cond_str.append('cid=?')
            cond_tup.append(cid)
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)

        if 0 == len(cond_tup):
            raise DatabaseError('删除成员的条件有误')

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                cur = conn.execute(
                    '''
                    DELETE FROM {0} WHERE {1}
                    '''.format(self._table, cond_str), cond_tup)
                return cur.rowcount
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[MemberDao.find_by] {e}')
                raise DatabaseError('查找成员失败')
Example #4
0
    def find_by(self, uid=None, alt=None, order_by_user=False):
        cond_str = []
        cond_tup = []
        order = 'round, boss, eid' if not order_by_user else 'uid, alt, round, boss, eid'
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)
        if not alt is None:
            cond_str.append('alt=?')
            cond_tup.append(alt)
        if 0 == len(cond_tup):
            return self.find_all()

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                ret = conn.execute(
                    '''
                    SELECT {1} FROM {0} WHERE {2} ORDER BY {3}
                    '''.format(self._table, self._columns, cond_str, order),
                    cond_tup).fetchall()
                return [self.row2item(r) for r in ret]
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[BattleDao.find_by] {e}')
                raise DatabaseError('查找记录失败')
Example #5
0
 def delete(self, gid, cid):
     with self._connect() as conn:
         try:
             conn.execute(
                 ''' DELETE FROM {0} WHERE gid=? AND cid=? '''.format(
                     self._table), (gid, cid))
         except sqlite3.DatabaseError as e:
             logger.error(f'[ClanDao.delete] {e}')
             raise DatabaseError('删除公会失败')
Example #6
0
 def delete(self, uid, alt):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''DELETE FROM {0} WHERE uid=? AND alt=?'''.format(
                     self._table), (uid, alt))
         except sqlite3.DatabaseError as e:
             logger.error(f'[MemberDao.delete] {e}')
             raise DatabaseError('删除成员失败')
Example #7
0
 def find_all(self):
     with self._connect() as conn:
         try:
             ret = conn.execute('''SELECT {1} FROM {0} '''.format(
                 self._table, self._columns)).fetchall()
             return [self.row2item(r) for r in ret]
         except sqlite3.DatabaseError as e:
             logger.error(f'[ClanDao.find_all] {e}')
             raise DatabaseError('查找公会失败')
Example #8
0
 def wrapper(*args, **kwargs) -> Any:
     try:
         return function(*args, **kwargs)
     except SQLAlchemyError as e:
         logger.error(f"[{function.__qualname__}] {type(e).__name__}:{e}")
         if ignoreException:
             return
         else:
             raise DatabaseError(prompt or "数据库发生未知错误")
Example #9
0
 def delete(self, eid):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''DELETE FROM {0} WHERE eid=?'''.format(self._table),
                 (eid, ))
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleDao.delete] {e}')
             raise DatabaseError('删除记录失败')
Example #10
0
 def find_one(self, uid, alt):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''SELECT {1} FROM {0} WHERE uid=? AND alt=?'''.format(
                     self._table, self._columns), (uid, alt)).fetchone()
             return self.row2item(ret)
         except sqlite3.DatabaseError as e:
             logger.error(f'[MemberDao.find_one] {e}')
             raise DatabaseError('查找成员失败')
Example #11
0
 def find_all(self):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''SELECT {1} FROM {0} ORDER BY round, boss, eid'''.format(
                     self._table, self._columns), ).fetchall()
             return [self.row2item(r) for r in ret]
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleDao.find_all] {e}')
             raise DatabaseError('查找记录失败')
Example #12
0
 def find_one(self, gid, cid):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''SELECT {1} FROM {0} WHERE gid=? AND cid=? '''.format(
                     self._table, self._columns), (gid, cid)).fetchone()
             return self.row2item(ret)
         except sqlite3.DatabaseError as e:
             logger.error(f'[ClanDao.find_one] {e}')
             raise DatabaseError('查找公会失败')
Example #13
0
 def modify(self, clan):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''UPDATE {0} SET name=?, server=? WHERE gid=? AND cid=? '''
                 .format(self._table),
                 (clan['name'], clan['server'], clan['gid'], clan['cid']))
         except sqlite3.DatabaseError as e:
             logger.error(f'[ClanDao.modify] {e}')
             raise DatabaseError('修改公会失败')
Example #14
0
 def add(self, clan):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''INSERT INTO {0} ({1}) VALUES (?, ?, ?, ?) '''.format(
                     self._table, self._columns),
                 (clan['gid'], clan['cid'], clan['name'], clan['server']))
         except sqlite3.DatabaseError as e:
             logger.error(f'[ClanDao.add] {e}')
             raise DatabaseError('添加公会失败')
Example #15
0
 def add(self, member):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''INSERT INTO {0} ({1}) VALUES (?, ?, ?, ?, ?)'''.format(
                     self._table, self._columns),
                 (member['uid'], member['alt'], member['name'],
                  member['gid'], member['cid']))
         except sqlite3.DatabaseError as e:
             logger.error(f'[MemberDao.add] {e}')
             raise DatabaseError('添加成员失败')
Example #16
0
 def add(self, uid, yyyy, mm, dd):
     datetime = yyyy * 10000 + mm * 100 + dd
     with self._connect() as conn:
         try:
             cur = conn.execute(
                 '''INSERT INTO {0} ({1}) VALUES (NULL, ?, ?)'''.format(
                     self._table, self._columns), (uid, datetime))
             return cur.lastrowid
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleSLDao.add] {e}')
             raise DatabaseError('添加记录失败')
Example #17
0
 def delete(self, uid, yyyy, mm, dd):
     datetime = yyyy * 10000 + mm * 100 + dd
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''DELETE FROM {0} WHERE uid=? AND time=?'''.format(
                     self._table, self._columns),
                 (uid, datetime)).fetchone()
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleSLDao.find] {e}')
             raise DatabaseError('查找记录失败')
Example #18
0
 def find_one(self, eid):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''
                 SELECT {1} FROM {0} WHERE eid=?
                 '''.format(self._table, self._columns),
                 (eid, )).fetchone()
             return self.row2item(ret)
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[BattleDao.find_one] {e}')
             raise DatabaseError('查找记录失败')
Example #19
0
 def modify(self, member):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 UPDATE {0} SET name=?, gid=?, cid=? WHERE uid=? AND alt=?
                 '''.format(self._table),
                 (member['name'], member['gid'], member['cid'],
                  member['uid'], member['alt']))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.modify] {e}')
             raise DatabaseError('修改成员失败')
Example #20
0
 def find_by_gid(self, gid):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''
                 SELECT {1} FROM {0} WHERE gid=?
                 '''.format(self._table, self._columns),
                 (gid, )).fetchall()
             return [self.row2item(r) for r in ret]
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.find_by_gid] {e}')
             raise DatabaseError('查找公会失败')
Example #21
0
 def modify(self, challenge):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''UPDATE {0} SET uid=?, alt=?, time=?, round=?, boss=?, dmg=?, flag=? WHERE eid=?'''
                 .format(self._table),
                 (challenge['uid'], challenge['alt'], challenge['time'],
                  challenge['round'], challenge['boss'], challenge['dmg'],
                  challenge['flag'], challenge['eid']))
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleDao.modify] {e}')
             raise DatabaseError('修改记录失败')
Example #22
0
 def add(self, challenge):
     with self._connect() as conn:
         try:
             cur = conn.execute(
                 '''INSERT INTO {0} ({1}) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)'''
                 .format(self._table, self._columns),
                 (challenge['uid'], challenge['alt'], challenge['time'],
                  challenge['round'], challenge['boss'], challenge['dmg'],
                  challenge['flag']))
             return cur.lastrowid
         except sqlite3.DatabaseError as e:
             logger.error(f'[BattleDao.add] {e}')
             raise DatabaseError('添加记录失败')
Example #23
0
async def broadcast(session:CommandSession):
    msg = session.current_arg
    self_ids = session.bot._wsr_api_clients.keys()
    for sid in self_ids:
        gl = await session.bot.get_group_list(self_id=sid)
        gl = [ g['group_id'] for g in gl ]
        for g in gl:
            await asyncio.sleep(0.1)
            try:
                await session.bot.send_group_msg(self_id=sid, group_id=g, message=msg)
                logger.info(f'群{g} 投递广播成功')
            except CQHttpError as e:
                logger.error(f'Error: 群{g} 投递广播失败 {type(e)}')
                try:
                    await session.send(f'Error: 群{g} 投递广播失败 {type(e)}')
                except CQHttpError as e:
                    logger.critical(f'向广播发起者进行错误回报时发生错误:{type(e)}')
    await session.send(f'广播完成!')
Example #24
0
async def hb_handler(context):
    message = context['message']
    self_id = context['self_id']
    group_id = context['group_id']
    user_id = context['user_id']
    for m in message:
        if m['type'] == 'hb':
            try:
                title = m['data']['title']
                if title in ban_hb_title:
                    bot.set_group_ban(self_id=self_id,
                                      group_id=group_id,
                                      user_id=user_id,
                                      duration=12 * 60 * 60)
                # sleep(0.5 + 3 * random.random())
                # await bot.send(context, title)
            except Exception as e:
                logger.error(f'hb_handler: {type(e)}')