Commit 5114624a authored by nanahira's avatar nanahira

finish

parent cfb005f0
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
webpack.config.js
dist/*
build/*
\ No newline at end of file
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
\ No newline at end of file
stages:
- build
- deploy
variables:
GIT_DEPTH: "1"
build:
stage: build
tags:
- linux
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
upload_to_minio:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- aws s3 --endpoint=https://minio.mycard.moe:9000 sync --delete dist/full/ s3://nanahira/koishi-plugin/order-picker
only:
- master
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . --access public
only:
- master
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/dist/full
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Koishi Plugin Order Picker
# koishi-plugin-order-picker
Koishi 抢单器
\ No newline at end of file
Koishi 抢单插件。
## 安装
### npm
```bash
npm install koishi-plugin-order-picker
```
### 直接安装
在 https://cdn02.moecube.com:444/nanahira/koishi-plugin/order-picker/index.js 下载即可。
## 配置
详见 `config.ts` 部分,或详见 Schema 描述配置。
#!/bin/bash
npm install --save \
source-map-support \
koishi-thirdeye
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier \
raw-loader \
ts-loader \
webpack \
webpack-cli \
koishi@next \
ws
This diff is collapsed.
{
"name": "koishi-plugin-order-picker",
"description": "一个 Koishi 插件",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "webpack && env PACK_ALL=1 webpack"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [
"Koishi.js",
"qqbot",
"cqhttp",
"onebot",
"required:cache"
],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker",
"peerDependencies": {
"koishi": "^4.0.0-beta.6"
},
"dependencies": {
"koishi-thirdeye": "^5.0.8",
"moment": "^2.29.1",
"source-map-support": "^0.5.21"
},
"devDependencies": {
"@types/node": "^17.0.2",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-beta.7",
"prettier": "^2.5.1",
"raw-loader": "^4.0.2",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"ws": "^8.4.0"
}
}
import 'source-map-support/register';
import { DefineSchema, RegisterSchema } from 'koishi-thirdeye';
import { MaybeArray, Schema, segment, Session } from 'koishi';
@RegisterSchema()
export class OrderPickerConfig {
constructor(config: OrderPickerConfigLike) {}
@DefineSchema({ type: String, description: '目标群。', required: true })
targetGuilds: string[];
@DefineSchema({ type: String, description: '发布人。', required: true })
publishers: string[];
@DefineSchema({ description: '主人 ID。', required: true })
masterId: string;
@DefineSchema({
type: String,
description: '不接的关键词列表。',
default: [],
})
blacklistMatchers: string[];
@DefineSchema({ description: '抢单的文本。', default: '我要' })
pickWord: string;
acceptMessage(session: Session) {
if (!session.content) {
return false;
}
if (
!(
session.guildId &&
this.targetGuilds.includes(session.guildId.toString()) &&
session.userId &&
this.publishers.includes(session.userId)
)
)
return false;
if (
this.blacklistMatchers.some((matcher) => session.content.match(matcher))
)
return false;
const segments = segment.parse(session.content);
if (!segments.find((s) => s.type === 'at' && s.data.type === 'all')) {
return false;
}
return true;
}
@DefineSchema({ description: 'CD 时间', default: 60 })
cooldown: number;
}
export type OrderPickerConfigLike = Partial<OrderPickerConfig>;
import 'source-map-support/register';
import { Context, Cache, Session, NextFunction, Bot, segment } from 'koishi';
import { OrderPickerConfig, OrderPickerConfigLike } from './config';
import {
KoishiPlugin,
InjectConfig,
Inject,
UseMiddleware,
OnGuild,
OnPlatform,
OnApply,
} from 'koishi-thirdeye';
import moment from 'moment';
export * from './config';
export interface PickedOrderInfo {
sourceGuildId: string;
sourceUserId: string;
sourceUserName: string;
description: string;
time: number;
}
declare module 'koishi' {
interface Modules {
'order-picker': typeof import('.');
}
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cache {
interface Tables {
orderPickerOnline: boolean;
pickedOrderInfo: PickedOrderInfo;
}
}
}
@OnPlatform('onebot')
@KoishiPlugin({ name: 'order-picker', schema: OrderPickerConfig })
export default class OrderPicker implements OnApply {
constructor(private ctx: Context, config: OrderPickerConfigLike) {}
@InjectConfig()
private config: OrderPickerConfig;
@Inject(true)
private cache: Cache;
onApply() {
this.cache.table('orderPickerOnline', {});
this.cache.table('pickedOrderInfo', {
maxAge: 1000 * 60 * this.config.cooldown,
});
const cmd = this.ctx
.private(this.config.masterId)
.command('order-picker', '抢单管理');
cmd
.subcommand('.status', '抢单状态')
.usage('获取抢单状态')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
const orderInfo = await this.getLastInfo(session.selfId);
const firstLine = `抢单状态:${online ? '上班' : '下班'}`;
let secondLine = '';
if (orderInfo) {
const time = moment.unix(orderInfo.time);
const toTime = time.clone().add(this.config.cooldown, 'minutes');
secondLine = `来自群:${orderInfo.sourceGuildId}\n发布人:${
orderInfo.sourceUserName
}(${orderInfo.sourceUserId})\n描述:${
orderInfo.description
}\n时间:${time.format(
'YYYY-MM-DD HH:mm:ss',
)}\n结束时间:${toTime.format('YYYY-MM-DD HH:mm:ss')}`;
} else {
secondLine = '最近没有抢单';
}
return `${firstLine}\n${secondLine}`;
});
cmd
.subcommand('.on', '上班')
.usage('开启抢单')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
if (online) {
return '已经在上班了。';
}
await this.setOnline(session.selfId, true);
return '上班成功。';
});
cmd
.subcommand('.off', '下单')
.usage('关闭抢单')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
if (!online) {
return '还没有上班。';
}
await this.setOnline(session.selfId, false);
return '下班成功。';
});
cmd
.subcommand('.clear', '清除当前单')
.usage('强制清除当前单')
.action(async ({ session }) => {
const orderInfo = await this.getLastInfo(session.selfId);
if (!orderInfo) {
return '当前没有单。';
}
await this.removeLastInfo(session.selfId);
return '清除成功。';
});
}
private async getLastInfo(selfId: string) {
return this.cache.get('pickedOrderInfo', selfId);
}
private async setLastInfo(selfId: string, info: PickedOrderInfo) {
return this.cache.set('pickedOrderInfo', selfId, info);
}
private async removeLastInfo(selfId: string) {
return this.cache.del('pickedOrderInfo', selfId);
}
private async isOnline(selfId: string) {
return this.cache.get('orderPickerOnline', selfId);
}
private async setOnline(selfId: string, online: boolean) {
return this.cache.set('orderPickerOnline', selfId, online);
}
@OnGuild()
@UseMiddleware()
async detectOrder(session: Session, next: NextFunction) {
if (!this.config.acceptMessage(session)) return next();
if (!(await this.isOnline(session.selfId))) return next();
if (await this.getLastInfo(session.selfId)) return next();
const description = segment
.parse(session.content)
.filter((s) => s.type === 'text')
.map((s) => s.data.content)
.join('');
await this.setLastInfo(session.selfId, {
description,
time: moment().unix(),
sourceGuildId: session.guildId,
sourceUserId: session.userId,
sourceUserName:
session.author?.nickname ||
session.author?.username ||
session.username ||
session.userId,
});
await session.send(this.config.pickWord);
await session.bot.sendPrivateMessage(
this.config.masterId,
`接到单:\n${description}`,
);
return;
}
}
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true,
"skipLibCheck": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts"
]
}
const path = require('path');
const packgeInfo = require('./package.json');
function externalsFromDep() {
return Object.fromEntries(
[
...Object.keys(packgeInfo.dependencies || {}),
...Object.keys(packgeInfo.peerDependencies || {}),
]
.filter((dep) => dep !== 'source-map-support')
.map((dep) => [dep, dep]),
);
}
const packAll = !!process.env.PACK_ALL;
module.exports = {
entry: './src/index.ts',
mode: 'production',
target: 'node',
devtool: 'source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{ test: /\.mustache$/, use: 'raw-loader' },
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'index.js',
library: {
type: 'commonjs',
},
path: path.resolve(__dirname, packAll ? 'dist/full' : 'dist'),
},
externals: {
koishi: 'koishi',
...(packAll ? {} : externalsFromDep()),
},
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment