Skip to content

Commit

Permalink
chore prettier: prettier the world with new config
Browse files Browse the repository at this point in the history
  • Loading branch information
91khr committed Sep 6, 2023
1 parent 1385f34 commit e05ff3d
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 263 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ function reload() {
});
}

reload();
reload();
2 changes: 1 addition & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ function setBotCommand() {
app.bot!.setMyCommands(botCommands);
}

setBotCommand();
setBotCommand();
37 changes: 11 additions & 26 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import yaml from 'js-yaml';
import std from '@/util/std.js';
import fatalError from '@/util/fatal_error.js';
import db from '@/lib/db.js';
import {
registCommand,
registGlobalMessageHandle,
registReplyHandle,
} from '@/lib/command.js';
import { registCommand, registGlobalMessageHandle, registReplyHandle } from '@/lib/command.js';
import { App } from '@/types/app.js';
import { YamlConfig } from '@/types/config.js';
import process from '@/lib/process.js';
Expand Down Expand Up @@ -44,8 +40,8 @@ export class Application implements App {
this._configExample = '';
console.warn(
chalk.yellow(
'No config.example.yml found. This program will not be able to provide a config example.',
),
'No config.example.yml found. This program will not be able to provide a config example.'
)
);
}
}
Expand All @@ -56,34 +52,23 @@ export class Application implements App {
console.log(this.config);
console.log('---------------');
console.log('Initializing bot ...');
if (
this.config.platform.settings[this.config.platform.enabled] === undefined
) {
console.log(
`未找到平台${this.config.platform.enabled}的配置,请检查config.yml`,
);
if (this.config.platform.settings[this.config.platform.enabled] === undefined) {
console.log(`未找到平台${this.config.platform.enabled}的配置,请检查config.yml`);
process.exit(-1);
}
console.log(
`Launching bot at Platform[${this.config.platform.enabled}]...`,
);
console.log(`Launching bot at Platform[${this.config.platform.enabled}]...`);

const createBot = (
await import(`../bridges/${this.config.platform.enabled}/index.js`)
).createBot as CreateBot;
const createBot = (await import(`../bridges/${this.config.platform.enabled}/index.js`))
.createBot as CreateBot;

try {
this._bot = createBot(
this.config.platform.settings[this.config.platform.enabled],
);
this._bot = createBot(this.config.platform.settings[this.config.platform.enabled]);
if (!this._bot) {
throw 'bot is undefined';
}
} catch (err) {
console.log(`-------${chalk.red('[ERROR]')}--------`);
console.log(
'Launching bot failed. Please check the error information below:',
);
console.log('Launching bot failed. Please check the error information below:');
console.error(err);
process.exit(-1);
}
Expand Down Expand Up @@ -140,7 +125,7 @@ export class Application implements App {
console.log('请正确配置config.yml. 本程序将自动退出.');
}
process.exit(0);
},
}
);
return;
}
Expand Down
23 changes: 5 additions & 18 deletions src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const replyHandles: ReplyHandleConfig[] = [];
*/
let on_off_mode: (app: App, msg: Message) => boolean = () => true;

export type commandHandleFunction = (
app: App,
message: Message,
message_text?: string,
) => void;
export type commandHandleFunction = (app: App, message: Message, message_text?: string) => void;
export type handleFunction = (app: App, message: Message) => void;

export interface _HandleConfigBase_ {
Expand All @@ -41,7 +37,6 @@ export interface _HandleConfigBase_ {
description?: BotCommand['description'];
}


export interface CommandHandleConfig extends _HandleConfigBase_ {
handle: commandHandleFunction;
/**
Expand Down Expand Up @@ -126,11 +121,7 @@ export interface canUseCommandResult {
* @param cmd 命令名(string)
* @returns 是否允许运行
*/
export function canUseCommand(
app: App,
message: Message,
cmd: string,
): canUseCommandResult {
export function canUseCommand(app: App, message: Message, cmd: string): canUseCommandResult {
if (
commands[cmd].premission === 'sysAdmin' &&
!(message.from?.id && app.config.bot_sysadmin_id.includes(message.from?.id))
Expand Down Expand Up @@ -161,9 +152,7 @@ export function canUseCommand(
*/
export function commandParser(app: App, message: Message) {
if (message.text?.startsWith(app.config.command_style)) {
const matched = message.text
.substring(app.config.command_style.length)
.match(/[^\s@]+/);
const matched = message.text.substring(app.config.command_style.length).match(/[^\s@]+/);
if (!matched) {
return;
}
Expand Down Expand Up @@ -194,7 +183,7 @@ export function commandParser(app: App, message: Message) {
*/
export function getCommands(): {
[key: string]: CommandHandleConfig;
} {
} {
return commands;
}

Expand All @@ -218,9 +207,7 @@ export function getReplyHandles(): ReplyHandleConfig[] {
* 注册bot on off mode管理器
* @param func 一个函数,调用后返回当前bot处于打开还是关闭状态
*/
export function botOnOffRegister(
func: (app: App, message: Message) => boolean,
) {
export function botOnOffRegister(func: (app: App, message: Message) => boolean) {
on_off_mode = func;
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ class ChatDB extends DB {
*/
user(uid) {
if (!this._user_caches[uid]) {
this._user_caches[uid] = new Proxy(
new GroupUserDB(this._chatId, uid),
GroupUserDB.handler,
);
this._user_caches[uid] = new Proxy(new GroupUserDB(this._chatId, uid), GroupUserDB.handler);
}
return this._user_caches[uid];
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/file_lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class functionQueue {
export async function writeFileSafe(
file: fs.PathOrFileDescriptor,
data: string | NodeJS.ArrayBufferView,
options?: fs.WriteFileOptions,
options?: fs.WriteFileOptions
) {
fileLocks.find(String(file)).push(() => {
fs.writeFileSync(file, data, options);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/bot_on_off/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bot_status: commandHandleFunction = (app, msg) => {
msg.chat.id,
db.chat(msg.chat.id).turned_off
? `${app.config?.bot_name}关机中`
: `${app.config?.bot_name}开机中`,
: `${app.config?.bot_name}开机中`
);
};

Expand Down
50 changes: 18 additions & 32 deletions src/plugins/chengyu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,10 @@ function gameEnded(msg: Message): boolean {
// 从starter获取随机成语开局
function setRandomChenyu(msg: Message, starter?: string): string {
console.log(starter);
if (
starter === '' ||
starter === undefined ||
cylist[starter] === undefined
) {
if (starter === '' || starter === undefined || cylist[starter] === undefined) {
return setRandomChenyu(msg, py[Math.floor(Math.random() * py.length)]);
} else {
const find: string =
cylist[starter][Math.floor(Math.random() * cylist[starter].length)];
const find: string = cylist[starter][Math.floor(Math.random() * cylist[starter].length)];
if (!getJielongStatus(msg)) {
clearJielongStatus(msg);
}
Expand Down Expand Up @@ -120,9 +115,9 @@ function getJielongInfo(msg: Message): string {
}
uList.sort((a, b) => Number(b.score) - Number(a.score));
for (let i = 0; i < uList.length; i++) {
res += `\n第${numToChinese(i + 1)}名: @${uList[i].username} ${
uList[i].score
}分,最多连击${uList[i].combo}次`;
res += `\n第${numToChinese(i + 1)}名: @${uList[i].username} ${uList[i].score}分,最多连击${
uList[i].combo
}次`;
}
return res;
} else {
Expand All @@ -136,15 +131,12 @@ const startJielong: commandHandleFunction = (app, msg, starter) => {
msg.chat.id,
`现在正在游戏哦,上一个成语是 ${getJielongStatus(msg).stcy},请接:${
getJielongStatus(msg).st
}\n场上情况:${getJielongInfo(msg)}`,
}\n场上情况:${getJielongInfo(msg)}`
);
} else {
if (starter?.length && starter?.length >= 3) {
if (cy[starter] === undefined) {
app.bot?.sendMessage(
msg.chat.id,
`这个成语是什么,${app.config?.bot_name}不知道哦OoO`,
);
app.bot?.sendMessage(msg.chat.id, `这个成语是什么,${app.config?.bot_name}不知道哦OoO`);
return;
}
db.chat(msg.chat.id).jielong_status = {
Expand All @@ -157,10 +149,7 @@ const startJielong: commandHandleFunction = (app, msg, starter) => {
userStatus: {}, // 用户得分信息
startAt: Number(new Date()), // 开始时间
};
app.bot?.sendMessage(
msg.chat.id,
`游戏开始!请接 ${getJielongStatus(msg)?.st}`,
);
app.bot?.sendMessage(msg.chat.id, `游戏开始!请接 ${getJielongStatus(msg)?.st}`);
} else {
db.chat(msg.chat.id).jielong_status = {
started: true,
Expand All @@ -173,9 +162,8 @@ const startJielong: commandHandleFunction = (app, msg, starter) => {
setRandomChenyu(msg);
app.bot?.sendMessage(
msg.chat.id,
`游戏开始,${app.config
?.bot_name}来给出第一个成语吧:${getJielongStatus(msg)
?.stcy},请接 ${getJielongStatus(msg)?.st}`,
`游戏开始,${app.config?.bot_name}来给出第一个成语吧:${getJielongStatus(msg)
?.stcy},请接 ${getJielongStatus(msg)?.st}`
);
}
}
Expand All @@ -185,7 +173,7 @@ const stopJielong: commandHandleFunction = (app, msg) => {
if (getJielongStatus(msg)?.started) {
app.bot?.sendMessage(
msg.chat.id,
`接龙被 @${msg.from?.username} 结束!\n最终情况:${getJielongInfo(msg)}`,
`接龙被 @${msg.from?.username} 结束!\n最终情况:${getJielongInfo(msg)}`
);
try {
db.chat(msg.chat.id).jielong_status = {
Expand All @@ -202,8 +190,9 @@ const stopJielong: commandHandleFunction = (app, msg) => {
const newMessageHandle: handleFunction = (app, msg) => {
if (getJielongStatus(msg)?.started && msg.text && cy[msg.text]) {
if (gameEnded(msg)) {
const res: string = `成语接龙结束啦!${app.config
?.bot_name}来宣布结果:${getJielongInfo(msg)}`;
const res: string = `成语接龙结束啦!${app.config?.bot_name}来宣布结果:${getJielongInfo(
msg
)}`;
app.bot?.sendMessage(msg.chat.id, res);
db.chat(msg.chat.id).jielong_status = {};
return;
Expand Down Expand Up @@ -234,8 +223,7 @@ const newMessageHandle: handleFunction = (app, msg) => {
combo: 1,
};
}
userStatus[msg.from?.id].score =
Number(userStatus[msg.from?.id].score) + 1;
userStatus[msg.from?.id].score = Number(userStatus[msg.from?.id].score) + 1;
// 判断combo
if (!getJielongStatus(msg).combo) {
getJielongStatus(msg).combo = 0;
Expand All @@ -247,7 +235,7 @@ const newMessageHandle: handleFunction = (app, msg) => {
if (lastJielonger) {
userStatus[lastJielonger].combo = Math.max(
Number(userStatus[lastJielonger].combo),
Number(getJielongStatus(msg).combo),
Number(getJielongStatus(msg).combo)
);
}
getJielongStatus(msg).combo = 1;
Expand All @@ -259,16 +247,14 @@ const newMessageHandle: handleFunction = (app, msg) => {
if (Number(getJielongStatus(msg).combo) <= 2) {
app.bot?.sendMessage(
msg.chat.id,
`接龙成功!${getName(msg)} 分数+1。\n下一个开头:${
getJielongStatus(msg).st
}`,
`接龙成功!${getName(msg)} 分数+1。\n下一个开头:${getJielongStatus(msg).st}`
);
} else {
app.bot?.sendMessage(
msg.chat.id,
`${getName(msg)} ${getJielongStatus(msg).combo} 连击!\n下一个开头:${
getJielongStatus(msg).st
}`,
}`
);
}
}
Expand Down
Loading

0 comments on commit e05ff3d

Please sign in to comment.