Commit 37f688eb authored by nanahira's avatar nanahira

sha512 and mustache

parent ce475270
...@@ -20,7 +20,7 @@ npm install koishi-plugin-fortune ...@@ -20,7 +20,7 @@ npm install koishi-plugin-fortune
module.exports = { module.exports = {
plugins: { plugins: {
"/path/to/fortune/index.js": { "/path/to/fortune/index.js": {
header: '今日 ({date}) 的占卜结果:\n', header: '今日 ({{date}}) 的占卜结果:\n',
masterKey: 'my-master-key', masterKey: 'my-master-key',
results: ['开心', '不开心'] results: ['开心', '不开心']
} }
...@@ -42,6 +42,7 @@ export interface Config { ...@@ -42,6 +42,7 @@ export interface Config {
header?: string; header?: string;
masterKey?: string; masterKey?: string;
results: string[]; results: string[];
useDatabase?: boolean;
} }
``` ```
...@@ -53,6 +54,18 @@ export interface Config { ...@@ -53,6 +54,18 @@ export interface Config {
* `results` 所有可能的占卜结果。 * `results` 所有可能的占卜结果。
* `useDatabase` 从数据库获取用户名。
### 渲染项目
位于 `header``results` 的项目,可以用 Mustache 模板进行填写字段用于渲染。具体如下。
* `id` 用户的 QQ 号。
* `name` 用户的昵称。
* `date` 当前日期 ,如 `2021-10-2`
## 命令 ## 命令
* `fortune` 进行占卜。 * `fortune` 进行占卜。
{ {
"name": "koishi-plugin-fortune", "name": "koishi-plugin-fortune",
"version": "1.0.0", "version": "1.0.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "koishi-plugin-fortune", "name": "koishi-plugin-fortune",
"version": "1.0.0", "version": "1.0.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"source-map-support": "^0.5.20" "source-map-support": "^0.5.20"
}, },
"devDependencies": { "devDependencies": {
"@stdlib/random-base-mt19937": "^0.0.5", "@stdlib/random-base-mt19937": "^0.0.5",
"@types/mustache": "^4.1.2",
"@types/node": "^16.10.2", "@types/node": "^16.10.2",
"@typescript-eslint/eslint-plugin": "^4.32.0", "@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0", "@typescript-eslint/parser": "^4.32.0",
...@@ -21,12 +22,16 @@ ...@@ -21,12 +22,16 @@
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"koishi-core": "^3.14.2", "koishi-core": "^3.14.2",
"moment": "^2.29.1", "moment": "^2.29.1",
"mustache": "^4.2.0",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"raw-loader": "^4.0.2", "raw-loader": "^4.0.2",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.3", "typescript": "^4.4.3",
"webpack": "^5.56.0", "webpack": "^5.56.0",
"webpack-cli": "^4.8.0" "webpack-cli": "^4.8.0"
},
"peerDependencies": {
"koishi-core": "^3.14.2"
} }
}, },
"node_modules/@babel/code-frame": { "node_modules/@babel/code-frame": {
...@@ -7811,6 +7816,12 @@ ...@@ -7811,6 +7816,12 @@
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==",
"dev": true "dev": true
}, },
"node_modules/@types/mustache": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-4.1.2.tgz",
"integrity": "sha512-c4OVMMcyodKQ9dpwBwh3ofK9P6U9ZktKU9S+p33UqwMNN1vlv2P0zJZUScTshnx7OEoIIRcCFNQ904sYxZz8kg==",
"dev": true
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "16.10.2", "version": "16.10.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz",
...@@ -10036,6 +10047,15 @@ ...@@ -10036,6 +10047,15 @@
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true "dev": true
}, },
"node_modules/mustache": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz",
"integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==",
"dev": true,
"bin": {
"mustache": "bin/mustache"
}
},
"node_modules/nanocolors": { "node_modules/nanocolors": {
"version": "0.2.12", "version": "0.2.12",
"resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz", "resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz",
...@@ -14642,6 +14662,12 @@ ...@@ -14642,6 +14662,12 @@
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==",
"dev": true "dev": true
}, },
"@types/mustache": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-4.1.2.tgz",
"integrity": "sha512-c4OVMMcyodKQ9dpwBwh3ofK9P6U9ZktKU9S+p33UqwMNN1vlv2P0zJZUScTshnx7OEoIIRcCFNQ904sYxZz8kg==",
"dev": true
},
"@types/node": { "@types/node": {
"version": "16.10.2", "version": "16.10.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz",
...@@ -16324,6 +16350,12 @@ ...@@ -16324,6 +16350,12 @@
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true "dev": true
}, },
"mustache": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz",
"integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==",
"dev": true
},
"nanocolors": { "nanocolors": {
"version": "0.2.12", "version": "0.2.12",
"resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz", "resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz",
......
...@@ -2,39 +2,59 @@ import 'source-map-support/register'; ...@@ -2,39 +2,59 @@ import 'source-map-support/register';
import { Context } from 'koishi-core'; import { Context } from 'koishi-core';
import { pickOne } from './random'; import { pickOne } from './random';
import moment from 'moment'; import moment from 'moment';
import Mustache from 'mustache';
export interface Config { export interface Config {
header?: string; header?: string;
masterKey?: string; masterKey?: string;
results: string[]; results: string[];
useDatabase?: boolean;
} }
export class MyPlugin { export class MyPlugin {
config: Config; private useDatabase = false;
ctx: Context; private config: Config;
private ctx: Context;
name = 'fortune-main'; name = 'fortune-main';
private getResult(userId: string) { private render(template: string, view: any) {
return Mustache.render(template, view, null, { escape: (v) => v });
}
private getResult(userId: string, name: string) {
if (!userId) { if (!userId) {
return '获取用户信息失败。'; return '获取用户信息失败。';
} }
const view = {
id: userId,
name,
date: moment().format('YYYY-MM-DD'),
};
let result = this.config.header let result = this.config.header
? this.config.header.replace('{date}', moment().format('YYYY-MM-DD')) ? this.render(this.config.header, view)
: ''; : '';
result += pickOne( result += this.render(
this.config.results, pickOne(this.config.results, userId.toString(), this.config.masterKey),
userId.toString(), view,
this.config.masterKey,
); );
return result; return result;
} }
apply(ctx: Context, config: Config) { apply(ctx: Context, config: Config) {
ctx.on('connect', () => {
if (ctx.database && this.config.useDatabase) this.useDatabase = true;
});
this.ctx = ctx; this.ctx = ctx;
this.config = config; this.config = config;
ctx ctx
.command('fortune', '进行占卜') .command('fortune', '进行占卜')
.usage('占卜结果每天固定。') .usage('占卜结果每天固定。')
.action((argv) => { .userFields(['name'])
return this.getResult(argv.session.userId); .action(({ session }) => {
let name = session.author.nickname || session.author.username;
if (this.useDatabase) {
if (session.user.name) {
name = session.user.name;
}
}
return this.getResult(session.userId, name);
}); });
} }
} }
...@@ -14,11 +14,11 @@ export function getTodayBuffer() { ...@@ -14,11 +14,11 @@ export function getTodayBuffer() {
} }
export function getRandomState(id: string, masterKey?: string) { export function getRandomState(id: string, masterKey?: string) {
const hash = createHash('md5').update(id).update(getTodayBuffer()); const hash = createHash('sha512').update(id).update(getTodayBuffer());
if (masterKey) { if (masterKey) {
hash.update(masterKey); hash.update(masterKey);
} }
return hash.digest().readUInt32LE(0); return parseInt(hash.digest('hex'), 16) % 4294967295;
} }
export function getRandomValue(id: string, masterKey?: string) { export function getRandomValue(id: string, masterKey?: string) {
......
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