Commit a26ca2e8 authored by nanahira's avatar nanahira

readme

parent 415410b9
Pipeline #3102 passed with stages
in 45 minutes and 38 seconds
#YuzuDice
## Description
# YuzuDice
下一代的骰娘。
## 环境变量
* `DB_HOST` `DB_PORT` `DB_USER` `DB_PASS` `DB_NAME` 数据库配置。
* `CQ_ID` QQ 号。
* `CQ_SERVER` OneBot 服务端地址。
* `CQ_TOKEN` OneBot 密钥。
* `ADMIN_TOKEN` http api 密钥,请求需要放在 `Authorization` 头。
* `DICE_MAX_COUNT` 最大的骰子数量。 默认 1000。
* `DICE_MAX_SIZE` 最大的骰子面数。 默认 1000。
## 推荐 docker-compose
```yaml
version: '2.4'
services:
mysql:
restart: always
image: mariadb:10
volumes:
- './db:/var/lib/mysql'
environment:
MYSQL_ROOT_PASSWORD: db_rootpass
MYSQL_DATABASE: yuzudice
MYSQL_USER: yuzudice
MYSQL_PASSWORD: db_pass
cqhttp:
restart: always
image: git-registry.mycard.moe/nanahira/docker-mirai-cqhttp:novnc
volumes:
- ./cqhttp/data:/usr/src/app/data
- ./cqhttp/config:/usr/src/app/config
- ./cqhttp/bots:/usr/src/app/bots
environment:
QQ_ID: 11111111
QQ_PASS: qq_pass
WS_TOKEN: cq_token
yuzudice:
restart: always
image: git-registry.mycard.moe/nanahira/yuzudice
ports:
- 3000:3000
environment:
DB_HOST: mysql
DB_PORT: 3306
DB_USER: yuzudice
DB_PASS: db_pass
CQ_ID: 11111111
CQ_SERVER: ws://cqhttp
CQ_TOKEN: cq_token
ADMIN_TOKEN: admin_token
DICE_MAX_COUNT: 1000
DICE_MAX_SIZE: 1000
```
之后使用命令 `curl -H 'Authorization: admin_token' http://<服务器IP>:3000/api/user -d 'id=<你的QQ号> -d 'permissions=0xffffffff'` 赋予自己管理员权限。
## Installation
```bash
......@@ -23,18 +83,6 @@ $ npm run start:dev
$ npm run start:prod
```
## Test
```bash
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
```
## License
AGPLv3
import { Body, Controller, Get, Post, Query, Headers } from '@nestjs/common';
import { AppService } from './app.service';
import { HttpServerService } from './http-server/http-server.service';
import { UserPermissions } from './constants';
import { DefaultTemplate } from './entities/DefaultTemplate';
import { defaultTemplateMap } from './DefaultTemplate';
@Controller('api')
export class AppController {
......@@ -22,9 +25,31 @@ export class AppController {
@Body('id') id,
@Body('name') name,
@Body('permissions') permissions,
@Body('addperm') addperm,
@Body('removeperm') removeperm,
) {
this.httpServerService.checkAccess(token);
await this.httpServerService.setUser(id, name, parseInt(permissions));
await this.httpServerService.setUser(
id,
name,
parseInt(permissions),
addperm,
removeperm,
);
return { success: true };
}
@Get('/values/perms')
getAllPermissionValues() {
return {
success: true,
data: { perms: Array.from(Object.keys(UserPermissions)) },
};
}
@Get('/values/templates')
getAllTemplateKeys() {
return {
success: true,
data: { perms: Array.from(defaultTemplateMap.keys()) },
};
}
}
......@@ -10,6 +10,7 @@ import { AppLogger } from '../app.logger';
import { BotService } from '../bot/bot.service';
import { User } from '../entities/User';
import { HttpServerLogger } from './http-server.logger';
import { UserPermissions } from '../constants';
@Injectable()
export class HttpServerService {
......@@ -49,7 +50,13 @@ export class HttpServerService {
}
}
async setUser(id: string, name: string, permissions: number) {
async setUser(
id: string,
name: string,
permissions: number,
addperm: string,
removeperm: string,
) {
try {
const user = await this.botService.findOrCreateUser(id);
if (name) {
......@@ -64,6 +71,26 @@ export class HttpServerService {
}
user.permissions = permissions;
}
if (addperm) {
const value: number = UserPermissions[addperm];
if (!value) {
throw new BadRequestException({
success: false,
message: `Permission not found: ${addperm}`,
});
}
user.permissions |= value;
}
if (removeperm) {
const value: number = UserPermissions[removeperm];
if (!value) {
throw new BadRequestException({
success: false,
message: `Permission not found: ${removeperm}`,
});
}
user.permissions &= ~value;
}
await this.db.getRepository(User).save(user);
} catch (e) {
throw new NotFoundException({
......
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