当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 4 Fork 1

zmx0142857 / qq-tex-bot
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cli.js 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
zmx0142857 提交于 2021-06-04 11:34 . cli 支持 qq 表情
const readline = require('readline')
const { bot } = require('./bot')
const config = require('./config')
const cli = readline.createInterface({
input: process.stdin,
output: process.stdout
})
cli.on('close', () => {
console.log('\ncli closed. ctrl-c again to exit') // 正确换行
})
const getGroupId = index => Object.keys(config.groups)[index]
let currentGroup = getGroupId(0)
const commands = [
[/^\/ls/, () => console.log(config.groups)],
[/^\/cd/, input => console.log(
config.groups[currentGroup = getGroupId(input || 0)]
)
],
[/^/, input => input && bot.sendMessage({
group: currentGroup,
message: buildMsg(input)
}).catch(console.error)
]
]
// 处理消息中的 qq 表情. 如, 滑稽的代码是 \178
function buildMsg(input) {
let match = input.match(/\\(\d+)/)
const msg = []
while (match) {
if (match.index) {
msg.push({ type: 'Plain', text: input.slice(0, match.index) })
}
msg.push({ type: 'Face', faceId: parseInt(match[1]) })
input = input.slice(match.index + match[0].length)
match = input.match(/\\(\d+)/)
}
if (input) {
msg.push({ type: 'Plain', text: input })
}
return msg
}
function processCli (text) {
// 寻找第一个匹配的命令, 并执行
for ([key, method] of commands) {
if (!key.test(text)) continue
return method(text.replace(key, '').trim())
}
}
module.exports = function interact () {
cli.question('> ', input => {
processCli(input)
interact()
})
}
1
https://gitee.com/zmx0142857/qq-tex-bot.git
git@gitee.com:zmx0142857/qq-tex-bot.git
zmx0142857
qq-tex-bot
qq-tex-bot
master

搜索帮助