代码拉取完成,页面将自动刷新
import plugin from "../../lib/plugins/plugin.js";
import { segment } from "oicq";
import request from "request"
import iconv from "iconv-lite";
import cheerio from "cheerio";
import lodash from "lodash";
import Common from "../../lib/common/common.js";
let game = {} //游戏房间
const admins = [] //填入管理员QQ号
const biaodian = new RegExp(`[、,!?:-_#\\s\.,。!?《》'"”“]+`, 'g')
const ignore_biaodian = new RegExp(`[#《》'"”“]`, 'g')
// 开发者 似此星辰非昨夜
// 参考了 西北一枝花(1679659)同功能插件
export class flyFlower extends plugin {
constructor () {
super({
name: '飞花令',
dsc: '飞花令小游戏',
event: 'message.group',
priority: 666,
rule: [{
reg: '^#开始(逐字|接龙|逐句)?飞花令(.*)$',
fnc: 'Start'
}, {
reg: '^#结束飞花令$',
fnc: 'End'
}, {
reg: '^#查询诗词(.*)$',
fnc: 'Query'
}, {
reg: '^#飞花令规则$',
fnc: 'gameRule'
}, {
reg: '^#记录$',
fnc: 'Record'
}, {
reg: '^#我说过的$',
fnc: 'Said'
}, {
reg: '^#冒冒$',
fnc: 'Hello'
}, {
reg: '^#(.*)$',
fnc: 'Check'
}]
})
}
async Hello(e) {
e.reply(`圣火昭昭,圣火耀耀,唯我i冒,冒冒冒冒`)
return true
}
async makeForwardMsg (qq, title, msg, end = '') {
//let nickname = this.e.bot.nickname
let nickname = "飞花令机器人"
if (this.e.isGroup) {
let info = await this.e.bot.getGroupMemberInfo(this.e.group_id, qq)
nickname = info.card ?? info.nickname
}
let userInfo = {
user_id: this.e.bot.uin,
nickname
}
let forwardMsg = [
{
...userInfo,
message: title
}
]
let msgArr = lodash.chunk(msg, 10000)
msgArr.forEach(v => {
v[v.length - 1] = lodash.trim(v[v.length - 1], '\n')
forwardMsg.push({ ...userInfo, message: v })
})
if (end) {
forwardMsg.push({ ...userInfo, message: end })
}
/** 制作转发内容 */
if (this.e.isGroup) {
forwardMsg = await this.e.group.makeForwardMsg(forwardMsg)
} else {
forwardMsg = await this.e.friend.makeForwardMsg(forwardMsg)
}
/** 处理描述 */
forwardMsg.data = forwardMsg.data
.replace(/\n/g, '')
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
.replace(/___+/, `<title color="#777777" size="26">${title}</title>`)
return forwardMsg
}
async Test(e) {
//古诗词网API,感谢桀桀提供网址。似乎比古诗文网好,但号被风控,暂时搁置
let msg = "春江花月夜"
let url = `https://www.gushici.net/chaxun/all/${encodeURI(msg)}`
let headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36' }
var p = new Promise(function(resolve, reject){
let recv = ""
request(url, { encoding: null, strictSSL: false, headers }, (error, res, body) => {
resolve(body)
})
})
let body = await p
let html = iconv.decode(body, 'utf8');
const $ = cheerio.load(html);
let theRes = $('.gushici-box-text').text();
const reg = new RegExp('\n', 'g')
theRes = theRes.split(reg)
e.reply(theRes[1])
logger.info(theRes.length)
let x = $('.chaxun/all/*').text()
e.reply(x)
return
}
async Search(msg) {
//let msg = this.e.msg.replace('#','').trim().substring(4)
let url = `https://so.gushiwen.cn/search.aspx?value=${encodeURI(msg)}&valuej=${encodeURI(msg[0])}`
let headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36' }
var p = new Promise(function(resolve, reject){
let recv = ""
request(url, { encoding: null, strictSSL: false, headers }, (error, res, body) => {
resolve(body)
})
})
let body = await p
let html = iconv.decode(body, 'utf8');
const $ = cheerio.load(html);
let theRes = $('textarea').text();
theRes = theRes.split("aspx")[0];
if (theRes != "") {
theRes = theRes.toString().trim() + "aspx";
}
return theRes
}
async Query(e) {
let msg = this.e.msg.replace('#','').trim().substring(4)
let standard_msg = msg.split(biaodian).filter(item => item != '').join(',')
let poem = await this.Search(standard_msg)
if (poem != "") {
let title = '查询结果'
let forward_msg = await this.makeForwardMsg(this.e.bot.uin, title, poem)
this.reply(forward_msg)
} else {
this.reply("未找到相关诗词。请尝试发送半句或一句。", true)
}
return true
}
async Start(e) {
let gameConfig = getGameConfig(e)
if (gameConfig.gameing) {
e.reply(`飞花令进行中,无法开始新游戏`);
return true;
}
else {
gameConfig.gameing = true
e.reply(`飞花令已开始,输入【#飞花令规则】以获取更多规则`)
}
game[e.group_id] = {};
game[e.group_id].starter = e.user_id
game[e.group_id].said = []
game[e.group_id].record = new Map()
game[e.group_id].said_keywords = [] //只存含有关键字的,查重用
if (e.msg.includes("逐字")) {
game[e.group_id].type = 1
} else if (e.msg.includes("接龙")) {
game[e.group_id].type = 2
} else if (e.msg.includes("逐句")) {
game[e.group_id].type = 3
} else {
game[e.group_id].type = 0
}
game[e.group_id].pos = 0
let pos = e.msg.indexOf("飞花令") + 3
let key = e.msg.substring(pos).trim()
if (key == "") {
game[e.group_id].keyword = await this.getWord();
} else if (game[e.group_id].type == 3) {
game[e.group_id].keyword = key.replace(biaodian, '')
} else if (isNaN(key)){
game[e.group_id].keyword = key;
} else {
game[e.group_id].keyword = await this.getWord(Number(key))
}
logger.info(game)
if (game[e.group_id].type == 2) {
this.setContext('start_jielong')
this.reply(`接龙飞花令已经开始,请发起者回复一句诗词`, true)
} else {
this.reply(`飞花令已经开始,本局比赛抽到的词为【${game[e.group_id].keyword}】,请发送带有含【${game[e.group_id].keyword}】的古诗词`)
}
return true;
}
async start_jielong(e) {
this.Check(e)
this.finish('start_jielong')
return true
}
async Check(e) {
if (!game) return false
if (!game[this.e.group_id]) return false
// 检查比赛是否正在进行
let gameConfig = getGameConfig(e)
if (!gameConfig.gameing) { return false }
let msg = this.e.msg.replace(ignore_biaodian,'').trim()
if (msg.length<4){
this.reply('字数过少,请补充完整!')
return true
}
//检查当前规则。0:普通。1:逐字。2:接龙。3:逐句。
let has_keyword = []
if (game[e.group_id].type == 0) {
has_keyword = msg.split(biaodian).filter(item => item.includes(game[e.group_id].keyword))
} else if (game[e.group_id].type == 1) {
//logger.info(game[e.group_id].pos)
has_keyword = msg.split(biaodian).filter(item => item[game[e.group_id].pos] == game[e.group_id].keyword)
} else if (game[e.group_id].type == 2) {
if (game[e.group_id].said.length == 0) {
has_keyword = msg.split(biaodian).filter(item => item != "").slice(-1)
logger.info(has_keyword)
} else {
has_keyword = msg.split(biaodian).filter(item => item[0] == game[e.group_id].keyword)
}
} else if (game[e.group_id].type == 3) {
//logger.info(game[e.group_id].pos)
let pos = game[e.group_id].pos
has_keyword = msg.split(biaodian).filter(item => item.includes(game[e.group_id].keyword[pos]))
}
if (has_keyword.length == 0) {
this.reply('不满足当前飞花令条件,请重新作答')
return true
}
let standard_msg = msg.split(biaodian).filter(item => item != '').join(',')
let use_standard_msg = (standard_msg.search(',') != -1)
if (use_standard_msg) {
for (let saidSentence of game[e.group_id].said) {
if (saidSentence.includes(standard_msg) || standard_msg.includes(saidSentence)) {
this.reply('这句已被说过了,请重新作答')
return true
}
}
has_keyword.push(standard_msg)
}
// 查重
for (let saidSentence of game[e.group_id].said_keywords) {
// has_keyword = has_keyword.filter(item => !saidSentence.includes(item))
has_keyword = has_keyword.filter(item => !(saidSentence == item))
if (has_keyword.length == 0) {
this.reply('这句已被说过了,请重新作答')
return true
}
}
for (let key of has_keyword) {
let theRes = await this.Search(key)
theRes = theRes.replace(ignore_biaodian, '')
let split_res = theRes.split(biaodian)
let standard_res = split_res.join(',')
// logger.info(theRes)
// logger.info(key)
// logger.info(standard_res)
if ((split_res.includes(key) && standard_res.includes(standard_msg)) || (use_standard_msg && key == standard_msg && standard_res.includes(key))) {
// logger.info(split_res.includes(key))
// logger.info(split_res)
this.reply('回答正确', true)
game[e.group_id].said.push(standard_msg)
game[e.group_id].said_keywords.push(key)
let id = e.user_id
let val = game[e.group_id].record.get(id)
if (val == null) {
val = [];
}
val.push(standard_msg)
game[e.group_id].record.set(id, val)
if (game[e.group_id].type == 1) {
game[e.group_id].pos = (game[e.group_id].pos+1) % 7
} else if (game[e.group_id].type == 3) {
game[e.group_id].pos = (game[e.group_id].pos+1) % (game[e.group_id].keyword.length)
}
if (game[e.group_id].type == 2) {
game[e.group_id].keyword = standard_msg[standard_msg.length-1]
}
return true
}
}
this.reply('抱歉,你说的可能不是古诗词或者未收录该版本,请重新作答', true)
logger.info(standard_msg)
return true
}
async End(e) {
let gameConfig = getGameConfig(e);
if (gameConfig.gameing ) {
if (e.user_id != game[e.group_id].starter && !admins.includes(e.user_id)) {
e.reply(`只有飞花令发起人和管理员可以结束飞花令`)
return false
}
e.reply(`飞花令已结束,本次共飞了${game[e.group_id].said.length}句`)
await this.Record(e)
init(e)
} else { e.reply(`飞花令未发起`); }
return true;
}
// 抽词
async getWord(mode = 1){
let word
switch (mode) {
case 1:
word = lodash.sample(['风', '花', '月', '人', '春', '天', '河', '红', '白', '秋', '水', '江', '海', '如', '中', '上',
'下', '一', '日', '不', '无', '明', '山', '云', '大', '小', '我', '时', '有', '长', '何', '空',
'心', '来', '去', '里', '为', '君', '知', '见', '在', '相', '年', '自', '此'])
break
case 2:
word = lodash.sample(['安', '百', '半', '杯', '北', '碧', '别', '波', '残', '草', '尘', '成', '城', '愁', '出', '处',
'从', '翠', '当', '到', '道', '得', '地', '东', '独', '断', '对', '发', '方','飞',
'非', '复', '高', '歌', '更', '孤', '古', '故', '关', '光',
'归', '国', '过', '寒', '好', '合', '后', '黄', '回', '几', '家', '间', '将',
'今', '尽', '近', '惊', '酒', '旧', '开',
'看', '可', '客', '老', '冷', '离', '连', '林', '流', '龙', '楼', '路', '露', '落', '绿', '马',
'满', '门', '梦', '明', '莫', '木', '暮', '南', '难', '能', '鸟', '平', '起', '气', '千',
'前', '桥', '青', '清', '情', '然', '入', '三', '色', '少', '谁', '身', '深', '生', '声', '十',
'石', '似', '是', '书', '树', '双',
'霜', '思', '随', '岁', '台', '同', '头', '外', '晚', '万', '望',
'微', '文', '闻', '五', '西', '溪', '闲', '乡', '香', '晓', '新', '行', '须', '雪', '烟',
'阳', '野', '叶', '夜', '衣', '已', '意', '吟', '应', '影', '游', '与', '雨', '玉',
'欲', '远', '照', '终', '重', '竹', '子', '醉'])
break
case 3:
word = lodash.sample(['八', '冰', '病', '不+尽', '采', '彩', '蚕', '曾', '常', '沉', '池', '赤', '川', '船', '窗',
'吹', '垂', '辞', '待', '但', '灯', '灯', '低', '帝', '点',
'动', '都', '儿', '而', '二',
'峰', '浮', '阁', '隔', '功', '宫', '共', '观', '还', '汉', '恨', '鸿', '忽', '胡', '湖', '画',
'荒', '昏', '火', '寄', '佳', '剑', '皆', '接', '节', '锦',
'景', '镜', '九', '菊', '觉', '堪', '口', '哭', '兰', '浪', '乐', '帘', '柳', '六', '乱', '漫',
'眉', '梅', '美', '面', '鸣', '目', '念', '暖', '蓬', '片', '破', '七', '旗', '浅', '墙', '琴',
'晴', '曲', '全', '泉', '犬', '食', '始', '世', '手', '说', '丝', '死', '四', '素', '堂',
'桐', '童', '土', '王', '问', '舞', '雾', '细', '霞', '夏',
'想', '笑', '斜', '星', '休', '寻', '言', '眼', '雁', '燕', '杨',
'也', '依', '疑', '音', '银', '悠', '于', '于', '语', '怨', '愿', '早', '章', '真', '征', '正',
'枝', '只', '州', '烛', '转', '紫', '足', '最', '户', '映',
'松', '凤', '指', '却', '柔', '定',])
break
default:
word = lodash.sample(['哀', '爱', '岸', '把', '薄', '杯', '边', '别', '鬓', '兵', '病',
'残', '蚕', '草', '曾', '车', '沉', '陈', '城', '池', '迟', '尺', '虫', '初',
'楚', '朝', '吹', '垂', '词', '辞', '此', '从', '翠', '打', '旦', '淡', '当', '登', '底', '点',
'雕', '蝶', '冬', '豆', '杜', '儿', '帆', '翻','芳', '飞', '肥',
'分', '纷', '枫', '逢', '夫', '拂', '复',
'敢', '更', '鼓', '故', '顾', '关', '光', '桂', '国', '还', '好', '号', '浩', '和',
'黑', '侯', '乎', '呼', '胡', '户', '华', '化', '悔', '昏', '鸡', '急', '记', '家', '间','渐', '脚',
'金', '经', '惊', '镜', '旧', '举', '卷', '绝', '槛', '可', '客', '口', '哭', '狂',
'栏', '阑', '蓝', '郎', '乐', '泪', '梨', '篱', '立', '丽', '连', '怜',
'帘', '莲', '零', '留', '落', '麦', '忙', '茫', '没', '眉','门', '眠',
'名', '名', '鸣', '磨', '莫', '木', '泥', '娘', '牛', '弄', '女', '怕', '旁', '萍', '破', '其',
'旗', '气', '泣', '浅', '强', '穷', '泉', '然', '若', '桑', '沙',
'裳', '舍', '谁', '身', '深', '神', '胜', '石', '识', '食', '事', '是', '首',
'双', '送', '所', '苔', '滩', '潭', '叹', '涕',
'铁', '听', '庭', '停', '同', '桐', '头', '王', '往', '未',
'问', '翁', '卧', '吴', '夕', '兮', '息', '稀', '细', '闲', '弦', '乡',
'相', '向', '晓', '歇', '斜', '辛', '行', '杏', '兄', '休', '寻', '压', '涯', '颜',
'雁', '燕', '阳', '遥', '亦', '阴', '饮', '应', '幽', '犹', '又', '右', '鱼',
'羽', '元', '原', '岳', '在', '赠', '折', '征', '之', '枝', '至', '终', '钟', '舟',
'烛', '妆', '字', '总', '最', '左', '作', '染', '进', '腹'])
break
}
await Common.sleep(50)
return word
}
async gameRule(e) {
if (!game) return false
if (!game[this.e.group_id]) return false
// 检查比赛是否正在进行
let gameConfig = getGameConfig(e)
if (!gameConfig.gameing) { return false }
let rule = this.get_game_rule(e)
let rep = `【飞花令】规则\n当前是【${rule[0]}飞花令】\n当前关键字为【${rule[1]}】`
if (game[e.group_id].type == 1 || game[e.group_id].type == 3) {
rep = rep + `\n当前关键字位置为【${rule[2]+1}】`
}
if (game[e.group_id].type == 3) {
rep = rep + `\n完整关键字为【${game[e.group_id].keyword}】`
}
e.reply(rep)
return true
}
get_game_rule(e) {
let cur = game[e.group_id]
let ret = []
if (cur.type == 1) {
ret.push("逐字")
ret.push(cur.keyword)
} else if (cur.type == 2) {
ret.push("接龙")
ret.push(cur.keyword)
} else if (cur.type == 3) {
ret.push("逐句")
ret.push(cur.keyword[cur.pos])
} else {
ret.push("普通")
ret.push(cur.keyword)
}
ret.push(cur.pos)
return ret
}
async Record(e) {
if (!game) return false
if (!game[this.e.group_id]) return false
// 检查比赛是否正在进行
let gameConfig = getGameConfig(e)
if (!gameConfig.gameing) { return false }
let tmp = []
for (let i = 0; i < game[e.group_id].said.length; i++) {
tmp.push((i+1) + '. ' + game[e.group_id].said[i]);
}
let title = '飞花记录'
let forward_msg = await this.makeForwardMsg(this.e.bot.uin, title, tmp.join('\n'))
this.reply(forward_msg)
//this.reply(tmp.join('\n'))
return true
}
async Said(e) {
if (!game) return false
if (!game[this.e.group_id]) return false
// 检查比赛是否正在进行
let gameConfig = getGameConfig(e)
if (!gameConfig.gameing) { return false }
let tmp = []
let rec = game[e.group_id].record.get(e.user_id)
if (rec == null) {
this.reply("你还没有参与,请好好飞花不要捣乱", true)
}
for (let i = 0; i < rec.length; i++) {
tmp.push((i+1) + '. ' + rec[i]);
}
this.reply(tmp.join('\n'), true)
return true
}
}
// 游戏配置与获取配置
const gameConfigMap = new Map()
function getGameConfig(e) {
let key = e.group_id;
let config = gameConfigMap.get(key);
if (config == null) {
config = {
gameing: false,
}
gameConfigMap.set(key, config);
}
return config;
}
// 游戏初始化
function init(e){
let gameConfig = getGameConfig(e)
gameConfig.gameing = false;
// gameConfig.current = false;
// clearTimeout(gameConfig.timer);
delete game[e.group_id]
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。