def search_card(context, modian_id): """ 查询当前已经获得的卡片 :param context: :param modian_id: :return: """ try: from modian.modian_card_draw import CardDrawHandler card_draw_handler = CardDrawHandler() card_draw_handler.read_config() from utils import util # is_digit = util.is_positive_integer(modian_id) # if not is_digit: # bot.send('摩点ID为纯数字,请重试~') # return report = card_draw_handler.get_cards(modian_id) bot.send(context, report) if '当前暂未抽中任何卡片' not in report: pic = '[CQ:image,file=%s]' % 'result.jpg' logger.debug(pic) bot.send(context, pic) except Error as e: logger.error(e) # bot.send(context, '查询出现错误!\n{}'.format(traceback.print_exc())) except Exception as exp: logger.exception(exp) logger.exception(traceback.print_exc())
def solve_lottery(user_id, group_id): """ 解签 :param user_id: :param group_id: :return: """ logger.info('解签,QQ={}, group_id={}'.format(user_id, group_id)) res_str = '' cursor_2 = lottery_db.cursor() try: # 是否有抽签记录 c = cursor_2.execute( """ SELECT `lot_date`, `has_solve`, `lot_id` FROM `t_lot` WHERE user_id=? and group_id=? """, (user_id, group_id)) rst = c.fetchone() now_date = date.today() if not rst: logger.info('你今天还没有抽过签,赶快去抽签吧!') return '[CQ:at,qq={}] 你今天还没有抽过签,赶快去抽签吧!'.format(user_id) else: last_lot_date = datetime.datetime.strptime(rst[0], "%Y-%m-%d").date() logger.info('上次抽签日期: {}, 今天日期为: {}'.format(last_lot_date, now_date)) if last_lot_date < now_date: logger.info('你今天还没有抽过签,赶快去抽签吧!') return '[CQ:at,qq={}] 你今天还没有抽过签,赶快去抽签吧!'.format(user_id) has_solve = int(rst[1]) if has_solve == 1: logger.info('你今天已经解过签了,请明天再来抽签吧~') return '[CQ:at,qq={}] 你今天已经解过签了,请明天再来抽签吧~'.format(user_id) else: lot_id = str(rst[2]) current_lot = lottery_data_map[lot_id] cursor_2.execute( """ UPDATE `t_lot` SET `has_solve`=1 WHERE `user_id`=? and `group_id`=? """, (user_id, group_id)) lottery_db.commit() res_str = '[CQ:at,qq={}] {}'.format(user_id, current_lot['解签']) logger.info(res_str) except Exception as e: logger.exception(e) finally: cursor_2.close() return res_str
def draw_lottery(user_id, group_id): """ 抽签 :param user_id: :param group_id: :return: """ logger.info('抽签,QQ={}, group_id={}'.format(user_id, group_id)) cursor_2 = lottery_db.cursor() res_str = '' try: # 是否有抽签记录 c = cursor_2.execute( """ SELECT `lot_date` FROM `t_lot` WHERE user_id=? and group_id=? """, (user_id, group_id)) rst = c.fetchone() can_lot = True # 是否可以抽签 now_date = date.today() if rst: last_lot_date = datetime.datetime.strptime(rst[0], "%Y-%m-%d").date() logger.info('上次抽签日期: {}, 今天日期为: {}'.format(last_lot_date, now_date)) if last_lot_date >= now_date: can_lot = False if not can_lot: return '[CQ:at,qq={}] 你今天已经抽过签了,请明天再来!'.format(user_id) # 抽签步骤:随机抽取,存进DB current_lot = util.choice(lottery_data)[0] logger.info('本次抽签: {}'.format(current_lot)) cursor_2.execute( """ INSERT OR REPLACE INTO `t_lot` (`user_id`, `group_id`, `lot_date`, `lot_id`, `has_solve`) VALUES (?, ?, ?, ?, ?) """, (user_id, group_id, now_date.strftime('%Y-%m-%d'), current_lot['ID'], 0)) lottery_db.commit() res_str = '[CQ:at,qq={}] 您抽到了第{}签:{}'.format(user_id, current_lot['ID'], current_lot['抽签']) logger.info(res_str) except Exception as e: logger.exception(e) finally: cursor_2.close() return res_str
def send_to_groups(cls, groups, message): for group in groups: try: bot.send_group_msg(group_id=group, message=message) except Exception as exp: logger.exception(exp)
def handle_msg(context): # 下面这句等价于 bot.send_private_msg(user_id=context['user_id'], message='你好呀,下面一条是你刚刚发的:') try: message = context['message'] group_id = context['group_id'] user_id = context['user_id'] logger.info('收到一条消息: 群: %s, 发送人: %s, %s', group_id, user_id, message) if user_id == context['self_id']: logger.debug('不处理自己发送的消息') return logger.info(AUTO_REPLY) # 关键词自动回复 for k, v in AUTO_REPLY.items(): if k in message.lower(): logger.info('命中关键词: %s', k) bot.send(context, v) break if str(group_id) in test_groups: # AI智能回复 logger.debug('AI智能回复') if len(message) > 1 and message.startswith('%'): content = message[1:] logger.debug('提问内容: %s' % content) reply = ai_bot.nlp_textchat(content, user_id) bot.send(context, reply) elif message == '-express': express_message = '[CQ:image,file=%s]' % ('lt_020.png') express_message2 = '[CQ:image,file=%s]' % ('tsj_013.gif') express_message3 = '[CQ:image,file=%s]' % ('xxy_040.png') logger.debug(express_message) logger.debug(express_message2) logger.debug(express_message3) bot.send(context, express_message) bot.send(context, express_message2) bot.send(context, express_message3) elif message == '-audio': import random files = ['1.aac', '2.aac', '3.aac', '4.aac', '5.aac'] express_message = '[CQ:record,file=%s]'.format( random.choice(files)) bot.send(context, express_message) elif message == '抽签': try: message = draw_lottery(user_id, group_id) bot.send(context, message) except Error as err: logger.exception(err) except Exception as e: logger.exception(e) bot.send(context, '抽签出现错误!') elif message == '解签': try: message = solve_lottery(user_id, group_id) bot.send(context, message) except Error as err: logger.exception(err) except Exception as e: logger.exception(e) bot.send(context, '解签出现错误!') # 查询集资 if str(group_id) in groups: if len(modian_array) > 0: if message == '-today': get_jizi_ranking_list_by_date(context, 0) elif message == '-yesterday': get_jizi_ranking_list_by_date(context, 1) elif message.startswith('-集资排名'): from utils import util admins = util.read_txt( os.path.join(BASE_DIR, 'data', 'card_draw', 'admin.txt')) if str(user_id) not in admins: logger.info('QQ:{} 无权限操作!') return strs = message.split(' ') if len(strs) != 3: num = 25 start_date = '1970-01-01' else: start_date = strs[1] num = int(strs[2]) message = get_donate_rank(start_date, num) bot.send(context, message) elif message == '-2019': from utils import util admins = util.read_txt( os.path.join(BASE_DIR, 'data', 'card_draw', 'admin.txt')) if str(user_id) not in admins: logger.info('QQ:{} 无权限操作!') return message = get_2019_donate_rank() bot.send(context, message) elif message == '-战况': from utils import util admins = util.read_txt( os.path.join(BASE_DIR, 'data', 'card_draw', 'admin.txt')) if str(user_id) not in admins: logger.info('QQ:{} 无权限操作!') return message = get_modian_pk() bot.send(context, message) # elif message == '-排行榜': # get_huitui_rank(context) elif message == '-help': help_msg = """查询当前集卡情况: 【-查询 桃叭ID】, 积分抽卡(积分数量必须是15的倍数): 【-积分抽 摩点ID 桃叭数量】, 补抽卡: 【-补抽 桃叭ID 补抽金额】, 消息以"%"开头,机器人可免费陪聊,每日数额有限 """ bot.send(context, help_msg) elif message.startswith('-查询'): strs = message.split(' ') if len(strs) == 2: search_card(context, strs[1]) else: bot.send(context, '格式为【-查询 桃叭ID】的形式,请重试~') elif message.startswith('-积分抽'): from utils import util admins = util.read_txt( os.path.join(BASE_DIR, 'data', 'card_draw', 'admin.txt')) if str(user_id) not in admins: logger.info('QQ:{} 无权限操作积分抽卡!') return strs = message.split(' ') if len(strs) == 3: draw_card_using_score(context, strs[1], strs[2]) else: bot.send(context, '格式为【-积分抽 桃叭ID 积分数量】的形式,请重试~') elif message.startswith('-补抽'): from utils import util admins = util.read_txt( os.path.join(BASE_DIR, 'data', 'card_draw', 'admin.txt')) if str(user_id) not in admins: logger.info('QQ:{} 无权限操作补抽卡!') return strs = message.split(' ') if len(strs) == 3: draw_missed_card(context, strs[1], strs[2]) else: bot.send(context, '格式为【-补抽 桃叭ID 补抽金额】的形式,请重试~') # elif message.upper() == '-PK': # try: # from modian_plugin import modian_handler # message = modian_handler.pk_modian_activity() # bot.send(context, message) # except Error as e: # logger.exception(e) # except Exception as e: # logger.exception(e) # bot.send(context, '查询PK出现错误!') else: bot.send(context, '目前并没有正在进行的集资项目T_T') except Error: pass
lottery_db = sqlite3.connect(os.path.join(BASE_DIR, 'data', 'lottery', 'lottery.db'), check_same_thread=False) cursor = lottery_db.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS 't_lot' ( user_id VARCHAR( 100 ), group_id VARCHAR( 100 ), lot_date DATE, lot_id INTEGER, has_solve INTEGER DEFAULT ( 0 ), UNIQUE ( user_id, group_id ) );""") except Exception as e: logger.exception(e) finally: cursor.close() try: from utils.mysql_util import mysql_util except Exception as e: logger.exception('导入mysql出现错误', e) @bot.on_message() def handle_msg(context): # 下面这句等价于 bot.send_private_msg(user_id=context['user_id'], message='你好呀,下面一条是你刚刚发的:') try: message = context['message'] group_id = context['group_id']