Commit ab1b5962 authored by nanahira's avatar nanahira

first

parent 3126aca6
Pipeline #5986 passed with stages
in 1 minute and 43 seconds
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',
},
};
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
/build
/output
/dest
/config.yaml
.idea
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/ s3://nanahira/koishi-plugin/fortune
only:
- master
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . || true
only:
- master
/install-npm.sh
.git*
/output
/dest
/config.yaml
.idea
.dockerignore
Dockerfile
/src
.eslintrc.js
.prettierrc
webpack.config.js
tsconfig.json
\ No newline at end of file
{
"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-fortune
Koishi 的算卦插件
\ No newline at end of file
占卜插件,每人每天可以算一卦。
本插件使用了 mt19937 算法来保证每天每人占卜结果确定。
## 安装
### npm
```bash
npm install koishi-plugin-fortune
```
### 直接安装
在 https://cdn02.moecube.com:444/nanahira/koishi-plugin/fortune/index.js 下载,并配置 `koishi.config.js`
```js
module.exports = {
plugins: {
"/path/to/fortune/index.js": {
adminContext: (ctx) => ctx.private(),
header: '今日 ({date}) 的占卜结果:\n',
masterKey: 'my-master-key',
results: ['开心', '不开心']
}
}
}
```
上例的返回结果为
```
今日 (2021-10-2) 的占卜结果:
不开心
```
## 配置
```ts
export interface Config {
header?: string;
masterKey?: string;
results: string[];
}
```
### 说明
* `header` 占卜结果的标题,会出现在结果首部。
* `masterKey` 占卜随机密钥。占卜结果会由 **日期** **用户ID** **masterKey** 唯一确定。
* `results` 所有可能的占卜结果。
## 命令
* `fortune` 进行占卜。
#!/bin/bash
npm install --save \
source-map-support
npm install --save-dev \
lodash \
@types/node \
@types/lodash \
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-core
This diff is collapsed.
{
"name": "koishi-plugin-fortune",
"version": "1.0.0",
"description": "Koishi 的算卦插件",
"main": "dist/index.js",
"typings": "dist/src/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "webpack"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/nanahira/koishi-plugin-fortune.git"
},
"author": "Nanahira",
"license": "MIT",
"keywords": [
"Koishi.js",
"qqbot",
"cqhttp",
"onebot"
],
"bugs": {
"url": "https://code.mycard.moe/nanahira/koishi-plugin-fortune/issues"
},
"homepage": "https://code.mycard.moe/nanahira/koishi-plugin-fortune",
"dependencies": {
"source-map-support": "^0.5.20"
},
"peerDependencies": {
"koishi-core": "^3.14.2"
},
"devDependencies": {
"@stdlib/random-base-mt19937": "^0.0.5",
"@types/node": "^16.10.2",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"koishi-core": "^3.14.2",
"moment": "^2.29.1",
"prettier": "^2.4.1",
"raw-loader": "^4.0.2",
"ts-loader": "^9.2.6",
"typescript": "^4.4.3",
"webpack": "^5.56.0",
"webpack-cli": "^4.8.0"
}
}
import 'source-map-support/register';
import { Context } from 'koishi-core';
import { Config, MyPlugin } from './plugin';
export { Config } from './plugin';
export const name = 'fortune';
export function apply(ctx: Context, config: Config) {
ctx.plugin(new MyPlugin(), config);
}
import 'source-map-support/register';
import { Context } from 'koishi-core';
import { pickOne } from './random';
import moment from 'moment';
export interface Config {
header?: string;
masterKey?: string;
results: string[];
}
export class MyPlugin {
config: Config;
ctx: Context;
name = 'fortune-main';
private getResult(userId: string) {
if (!userId) {
return '获取用户信息失败。';
}
let result = this.config.header
? this.config.header.replace('{date}', moment().format('YYYY-MM-DD'))
: '';
result += pickOne(
this.config.results,
userId.toString(),
this.config.masterKey,
);
return result;
}
apply(ctx: Context, config: Config) {
this.ctx = ctx;
this.config = config;
ctx
.command('fortune', '进行占卜')
.usage('占卜结果每天固定。')
.action((argv) => {
return this.getResult(argv.session.userId);
});
}
}
import mt19937 from '@stdlib/random-base-mt19937';
import { createHash } from 'crypto';
import moment from 'moment';
export function getTodayBuffer() {
const today = moment();
today.set('hour', 0);
today.set('minute', 0);
today.set('second', 0);
today.set('millisecond', 0);
const buf = Buffer.allocUnsafe(8);
buf.writeBigUInt64LE(BigInt(today.unix()));
return buf;
}
export function getRandomState(id: string, masterKey?: string) {
const hash = createHash('md5').update(id).update(getTodayBuffer());
if (masterKey) {
hash.update(masterKey);
}
return hash.digest().readUInt32LE(0);
}
export function getRandomValue(id: string, masterKey?: string) {
const mt = mt19937.factory({ seed: getRandomState(id, masterKey) });
return mt();
}
export function pickOne<T>(arr: T[], id: string, masterKey?: string): T {
if (!arr.length) {
return undefined;
}
if (arr.length === 1) {
return arr[0];
}
const index = getRandomValue(id, masterKey) % arr.length;
return arr[index];
}
import { getRandomValue } from '../src/random';
console.log(getRandomValue('1112222', '222'));
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts"
]
}
const path = require("path");
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, "dist"),
},
externals: {
'koishi-core': 'koishi-core',
'koishi': 'koishi',
}
};
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