Commit 37f688eb authored by nanahira's avatar nanahira

sha512 and mustache

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