Quellcode durchsuchen

feat(道具系统): 添加道具相关接口和功能实现

新增道具接口 `Prop` 并在 `UserData` 中添加 `props` 字段以支持道具功能。实现道具的注册、获取、添加、减少等操作,并在 `test` 插件中添加测试道具的使用示例。同时更新权限配置文件,添加道具相关命令的权限。
枫林 vor 3 Monaten
Ursprung
Commit
75ef68a9d6
6 geänderte Dateien mit 130 neuen und 5 gelöschten Zeilen
  1. 15 0
      src/config/permission.yml
  2. 4 1
      src/interface/economy.ts
  3. 11 0
      src/interface/prop.ts
  4. 7 4
      src/lib/economy.ts
  5. 78 0
      src/lib/prop.ts
  6. 15 0
      src/plugins/test.ts

+ 15 - 0
src/config/permission.yml

@@ -59,6 +59,8 @@ users:
       sakulass:
         commands:
           图: true
+          一言: true
+          help: true
       ecomony:
         commands:
           info: true
@@ -67,6 +69,19 @@ users:
       test:
         commands:
           remove: true
+          help: true
+          param: true
+      Botlog:
+        commands:
+          logs: true
+          downloadlog: true
+      Prop:
+        commands:
+          list: true
+          help: true
+          buy: true
+          use: true
+          my: true
   default:
     plugins:
       test:

+ 4 - 1
src/interface/economy.ts

@@ -1,9 +1,12 @@
+import { Prop } from "./prop.js";
+
 export interface UserData {
     userId: string; 
     economy:{
         coins: number;
         logs: Economylogs[];
-    }
+    },
+    props:Prop[]
 }
 export interface Economylogs {
     type: 'add' | 'remove';

+ 11 - 0
src/interface/prop.ts

@@ -0,0 +1,11 @@
+export interface Prop{
+    propId: string;// 道具ID
+    fn: Function; // 道具函数名
+    propName: string; // 道具名称
+    describe?: string;
+    Num: number; // 道具数量
+    maxuse: number; // 道具最大使用次数
+    img?: string; // 道具图片
+    price: number; // 道具价格
+    classConstructor:any;
+}

+ 7 - 4
src/lib/economy.ts

@@ -39,7 +39,8 @@ export function getUserData(userId: string): UserData {
             economy: {
                 coins: 0,
                 logs: []
-            }
+            },
+            props: []
         }
     }
     if (!fs.existsSync(`${economy.data.path}/${userId}.json`)) {
@@ -47,8 +48,10 @@ export function getUserData(userId: string): UserData {
             userId: userId,
             economy: {
                 coins: economy.data.defaultCoins,
-                logs: []
-            }
+                logs: [],
+                
+            },
+            props: []
         };
         fs.writeFileSync(`${economy.data.path}/${userId}.json`, JSON.stringify(newUserData, null, 4));
         return newUserData;
@@ -56,7 +59,7 @@ export function getUserData(userId: string): UserData {
     const userData = JSON.parse(fs.readFileSync(`${economy.data.path}/${userId}.json`, 'utf-8')) as UserData;
     return userData;
 }
-function  saveUserData(userId: string, userData: UserData): void {
+export function  saveUserData(userId: string, userData: UserData): void {
     if (!fs.existsSync(`${economy.data.path}`)) {
         throw new Error(`未找到用户数据目录,请检查配置文件`);
     }

+ 78 - 0
src/lib/prop.ts

@@ -0,0 +1,78 @@
+// 添加金币相关的fn装饰器
+
+import { Prop } from "../interface/prop.js";
+import { getUserData, saveUserData } from "./economy.js";
+import botlogger from "./logger.js";
+
+export const Props = new Map<string,Prop>();
+/**
+ * 道具相关的fn装饰器
+ * @param propId 道具ID
+ * @param propName 道具名称
+ * @param img 道具图片
+ * @param price 道具价格
+ */
+export function prop(propId:string,propName: string, maxuse: number=1, describe?: string ,img?: string ,price?: number) {
+    return function (target: any, propertyKey: string | symbol | undefined, descriptor: PropertyDescriptor): void {
+        const actualPropertyKey = propertyKey!;
+        const fnName = `${target.constructor.name}.${actualPropertyKey.toString()}`;
+        const constructor = target.constructor;
+        
+        const prop:Prop  = {
+            maxuse: maxuse,
+            propId: propId,
+            fn: descriptor.value.bind(target),
+            describe:describe,
+            Num: 0,
+            propName: propName,
+            img: img,
+            price: price || 0,
+            classConstructor: constructor
+        };
+        Props.set(fnName,prop)
+        botlogger.info(`注册${prop.propName}道具成功!`)
+    };
+}
+
+export function getProp(fnName: string): Prop | undefined {
+    return Props.get(fnName);
+}
+
+export async function getuserProp(userId: string): Promise<Prop[]> {
+    return await getUserData(userId).props;
+}
+// 减少道具数量
+export async function reduceProp(userId: string, propId: string, Num: number = 1): Promise<boolean> {
+    const userProp = await getuserProp(userId) || [];
+    let addprop = userProp.find((prop) => prop.propId === propId);
+    if (!addprop) {
+        return false
+    }else{
+        addprop.Num -= Num;
+    }
+    // 保存道具数据
+    const userData = await getUserData(userId)
+    userData.props = userProp;
+    await saveUserData(userId,userData);
+    return true;
+}
+export async function addProp(userId: string, propId: string, Num: number = 1): Promise<boolean> {
+    const userProp = await getuserProp(userId) || [];
+    let addprop = userProp.find((prop) => prop.propId === propId);
+    if (!addprop) {
+        Props.forEach(prop=>{
+            if (prop.propId === propId) {
+                addprop = prop
+                addprop.Num = Num
+                userProp.push(addprop)
+            }
+        })
+    }else{
+        addprop.Num += Num;
+    }
+    // 保存道具数据
+    const userData = await getUserData(userId)
+    userData.props = userProp;
+    await saveUserData(userId,userData);
+    return true;
+}

+ 15 - 0
src/plugins/test.ts

@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
 import { qqBot } from '../app.js';
 import botlogger from '../lib/logger.js';
 import { ParamType } from '../interface/plugin.js';
+import { prop } from '../lib/prop.js';
 
 @plugins({
     easycmd: true,//是否启用简易命令,启用将将命令注册为<命令名称>,不启用将注册为#<插件名称> <命令名称>
@@ -83,5 +84,19 @@ export class test {
     async testschedule() {
         // botlogger.info("定时任务测试")
     }
+    @prop(
+        "testProp",//道具id
+        "测试道具",//道具名称
+        1,//道具最大使用数量
+        "测试使用道具",//道具描述
+        "",//道具图片
+        1//道具价格
+    )
+    async test(
+        userId:string,
+        propparam:string
+    ): Promise<any>{
+        return `成功对用户${userId}使用测试道具--接受道具参数${propparam}`
+    }
 
 }