Miao-Yunzai/src/config/qq.ts

133 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-06-09 01:00:07 +08:00
import fs from 'fs'
import inquirer from 'inquirer'
2024-06-09 20:02:18 +08:00
import chalk from 'chalk'
import { BOT_NAME, CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js'
2024-06-09 01:00:07 +08:00
import cfg from './config.js'
import { sleep } from '../utils/common.js'
2024-06-09 01:00:07 +08:00
/**
* qq配置文件 `config/bot/qq.yaml`
* Git Bash npm命令会无法选择列表
* @returns
2024-06-09 01:00:07 +08:00
*/
2024-06-12 09:35:17 +08:00
export async function createQQ() {
2024-06-09 01:00:07 +08:00
/** 跳过登录ICQQ */
if (cfg.bot.skip_login) return
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
if (cfg.qq && !process.argv.includes('login')) {
return
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
console.log(`欢迎使用${chalk.green(`${BOT_NAME} v` + cfg.package.version)}\n请按提示输入完成QQ配置`)
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-11 20:13:21 +08:00
const propmtList = [
2024-06-09 01:00:07 +08:00
{
type: 'Input',
message: '请输入机器人QQ号(建议用小号)',
name: 'QQ',
validate(value) {
2024-06-09 01:00:07 +08:00
if (/^[1-9][0-9]{4,14}$/.test(value)) return true
return '请输入正确的QQ号'
}
},
{
type: process.platform == 'win32' ? 'Input' : 'password',
message: '请输入登录密码(为空则扫码登录)',
name: 'pwd'
},
{
type: 'list',
message: '请选择登录端口:',
name: 'platform',
default: '6',
choices: ['Tim', 'iPad', '安卓手机', '安卓手表', 'MacOS', 'aPad'],
filter: (val) => {
switch (val) {
case 'Tim': return 6
case 'iPad': return 5
case 'MacOS': return 4
case '安卓手机': return 1
case '安卓手表': return 3
case 'aPad': return 2
default: return 6
2024-06-09 01:00:07 +08:00
}
}
}
]
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
if (!process.argv.includes('login')) {
propmtList.push({
type: 'Input',
message: '请输入主人QQ号',
name: 'masterQQ'
})
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
propmtList.push({
type: 'input',
message: '请输入签名API地址可留空',
name: 'signAPI'
})
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
const ret = await inquirer.prompt(propmtList)
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 20:02:18 +08:00
const file = `./${CONFIG_INIT_PATH}`
const fileDef = `./${CONFIG_DEFAULT_PATH}`
2024-06-09 01:00:07 +08:00
let qq = fs.readFileSync(`${fileDef}qq.yaml`, 'utf8')
qq = qq.replace(/qq:/g, 'qq: ' + ret.QQ)
qq = qq.replace(/pwd:/g, `pwd: '${ret.pwd}'`)
qq = qq.replace(/platform: [1-6]/g, 'platform: ' + Number(ret.platform))
fs.writeFileSync(`${file}qq.yaml`, qq, 'utf8')
let bot = fs.readFileSync(`${fileDef}bot.yaml`, 'utf8')
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
if (ret.masterQQ) {
let other = fs.readFileSync(`${fileDef}other.yaml`, 'utf8')
other = other.replace(/masterQQ:/g, `masterQQ:\n - ${ret.masterQQ}`)
fs.writeFileSync(`${file}other.yaml`, other, 'utf8')
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:07 +08:00
if (ret.signAPI) {
bot = bot.replace(/sign_api_addr:/g, `sign_api_addr: ${ret.signAPI}`)
}
fs.writeFileSync(`${file}bot.yaml`, bot, 'utf8')
2024-06-12 09:35:17 +08:00
console.log(`\nQQ配置完成正在登录\n后续修改账号可以运行命令 ${chalk.green('npm run login')}\n`)
2024-06-09 01:00:07 +08:00
2024-06-09 11:40:24 +08:00
/**
*
*/
await sleep(2000)
2024-06-09 20:02:18 +08:00
}