加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
feihua.js 18.24 KB
一键复制 编辑 原始数据 按行查看 历史
Sicixingchenfeizuoye 提交于 2023-05-29 09:27 . js
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]
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化