加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
chachong.js 7.70 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')
export class chaChong extends plugin {
constructor () {
super({
name: '查重',
dsc: '查重小游戏',
event: 'message.group',
priority: 6,
rule: [{
reg: '^#开始查重$',
fnc: 'Start'
}, {
reg: '^#结束查重$',
fnc: 'End'
}, {
reg: '^#暂停查重$',
fnc: 'Pause'
}, {
reg: '^#删除(.+)$',
fnc: 'Del'
}, {
reg: '^#记录$',
fnc: 'Record'
}, {
reg: '^#我说过的$',
fnc: 'Said'
}, {
reg: '^#继续查重$',
fnc: 'Continue'
}, {
reg: '^[0-9]+(.+)$',
fnc: 'Recv'
}]
})
}
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 Start(e) {
let gameConfig = getGameConfig(e)
if (gameConfig.gameing) {
e.reply(`查重进行中,无法开始新游戏`);
return true;
}
else {
gameConfig.gameing = true
}
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].pos = 0
logger.info(game)
this.reply(`查重已经开始,请发送序号加诗句,偏离当前序号不超过10的数字会被识别`)
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 Pause(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
}
gameConfig.pausing = true
e.reply('查重已暂停')
}
return
}
async Continue(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
}
gameConfig.pausing = false
e.reply('查重继续')
}
}
async Del(e) {
let index = e.msg.substring(3)
let gameConfig = getGameConfig(e);
if (gameConfig.gameing) {
if (!admins.includes(e.user_id)) {
e.reply(`只有管理员可以删除`)
return false
}
//logger.info(index)
if (isNaN(index)) {
e.reply(`删除失败,请按“删除+数字”的格式发送`)
} else {
index = parseInt(index)
let poem = game[e.group_id].said[index-1]
game[e.group_id].said.splice(index-1, 1)
for (let key of game[e.group_id].record.keys()) {
let i = game[e.group_id].record.get(key).indexOf(poem)
if (i != -1) {
let tmp = game[e.group_id].record.get(key)
if (i != -1) {
tmp.splice(i, 1)
game[e.group_id].record.set(key, tmp)
}
e.reply(`删除成功,下个序号是${game[e.group_id].said.length+1}`)
}
}
//e.reply(`删除成功,下个序号是${game[group_id].said.length+1}`)
}
}
}
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
}
async Recv(e) {
if (!game) return false
if (!game[this.e.group_id]) return false
// 检查比赛是否正在进行
let gameConfig = getGameConfig(e)
if (!gameConfig.gameing || gameConfig.pausing) { return false }
let reg = new RegExp(`[、,!?:-_#\\s\.,。!?《》'"”“]+`, 'g')
let num = parseInt(e.msg)
if (num < game[e.group_id].said.length-10 || num > game[e.group_id].said.length+9) {
e.reply(`序号不对`, true)
return false
}
logger.info(e.msg)
let poem = e.msg.substring(num.toString().length).replace(" ",'').split(reg).filter(item => item != '').join('')
//logger.info(poem)
for (let saidSentence of game[e.group_id].said) {
if (saidSentence.includes(poem) || poem.includes(saidSentence)) {
e.reply(`这句已经说过了`, true)
return false
}
}
game[e.group_id].said.push(poem)
let id = e.user_id
let val = game[e.group_id].record.get(id)
if (val == null) {
val = [];
}
val.push(poem)
game[e.group_id].record.set(id, val)
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,
pausing: false,
}
gameConfigMap.set(key, config);
}
return config;
}
// 游戏初始化
function init(e){
let gameConfig = getGameConfig(e)
gameConfig.gameing = false;
gameConfig.pausing = false;
// clearTimeout(gameConfig.timer);
delete game[e.group_id]
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化