prop.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import botlogger from "../lib/logger.js";
  2. import { param, plugins, runcod } from "../lib/decorators.js";
  3. import { addProp, getuserProp, Props, reduceProp } from "../lib/prop.js";
  4. import { GroupMessage, PrivateFriendMessage, PrivateGroupMessage } from "node-napcat-ts";
  5. import { removeCoins } from "../lib/economy.js";
  6. import path from "path";
  7. import { fileURLToPath } from "url";
  8. import { Prop } from "../interface/prop.js";
  9. import { Receive } from 'node-napcat-ts/dist/Structs.js';
  10. @plugins({
  11. easycmd: true,//是否启用简易命令,启用将将命令注册为<命令名称>,不启用将注册为#<插件名称> <命令名称>
  12. name: "道具插件", //插件名称,用于显示在菜单中
  13. version: "1.0.0", //插件版本号,用于显示在菜单中
  14. describe: "官方道具插件", //插件描述,用于显示在菜单中
  15. author: "枫叶秋林",//插件作者,用于显示在菜单中
  16. help: { //插件帮助信息,用于显示在菜单中
  17. enabled: true, //是否启用帮助信息
  18. description: "显示道具插件" //帮助信息描述
  19. }
  20. })
  21. export class Propplu {
  22. @runcod(["use", "使用道具"],"使用道具" )
  23. async useprop(
  24. @param("道具Id","text") propId: Receive["text"],
  25. @param("QQ", "at") userId: Receive["at"],
  26. @param("数量", "text", {type:'text',data:{text:"1"}}, true) Num: Receive["text"],
  27. @param("道具参数","text",{type:'text',data:{text:""}},true) propparam:Receive["text"],
  28. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  29. ): Promise<any> {
  30. const userProp = await getuserProp(context?.sender?.user_id.toString()??"0") || [];
  31. let findprop = userProp.find((prop) => prop.propId == propId?.data?.text);
  32. if (!findprop) {
  33. return this.tl(`你没有${propId}道具`,'error',`你没有${propId}道具`)
  34. }
  35. if (findprop.Num < Number(Num?.data?.text)) {
  36. return this.tl(`道具数量不足`,'error',`道具数量不足`)
  37. }
  38. Props.forEach((prop) => {
  39. if (prop.propId === propId?.data?.text) {
  40. if(prop.maxuse<Number(Num?.data?.text)){
  41. return this.tl(`该道具允许最大使用数量为${prop.maxuse}超过最大使用数量`,'error',`该道具允许最大使用数量为${prop.maxuse}超过最大使用数量`)
  42. }
  43. }
  44. });
  45. try {
  46. for (let i = 0; i < Number(Num?.data?.text); i++) {
  47. let fn;
  48. let classConstructor;
  49. let propid=''
  50. Props.forEach((prop) => {
  51. if (prop.propId==propId?.data?.text) {
  52. fn = prop.fn;
  53. classConstructor = prop.classConstructor
  54. propid= prop.propId
  55. }
  56. })
  57. if (await reduceProp(context?.sender?.user_id.toString()??"0", propid, 1)){
  58. const result = (fn as any).call(classConstructor, userId.data.qq, propparam,context);
  59. if (result) {
  60. return result;
  61. }
  62. };
  63. }
  64. } catch (error: any) {
  65. botlogger.error(error);
  66. return this.tl(`道具使用失败:${error.message}`,'error',`道具使用失败:${error.message}`)
  67. }
  68. return "道具使用失败";
  69. }
  70. @runcod(["list", "道具列表"],"道具列表")
  71. async getprop(){
  72. let s ='道具列表:\n'
  73. let p: Prop[] =[]
  74. Props.forEach((prop) => {
  75. p.push(prop)
  76. s += `名称:${prop.propName}[${prop.propId}]---描述:${prop.describe}---价格:${prop.price}\n`;
  77. })
  78. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  79. return {
  80. Prs:p,
  81. template:{
  82. enabled: true,
  83. sendText: false,
  84. path: path.resolve(__dirname, '..','resources', 'prop',`getprop.html`),//模版路径,推荐按规范放置在resources目录下
  85. render: {//浏览器默认参数设置,用于打开浏览器的设置
  86. width: 800, // 模板宽度
  87. type: 'png',// 模板类型
  88. quality: 100,// 模板质量
  89. fullPage: false,// 是否全屏
  90. background: true// 是否背景
  91. },
  92. },
  93. toString(){
  94. return s;
  95. }
  96. }
  97. }
  98. @runcod(["my", "道具" ,"我的道具"], "我的道具")
  99. async userprop(context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage){
  100. const props = (await getuserProp(context?.sender?.user_id?.toString()??"0"));
  101. let p: Prop[] =[]
  102. let s ='道具列表:\n'
  103. props.forEach((prop) => {
  104. Props.forEach((Allprop) => {
  105. if(Allprop.propId === prop.propId){
  106. Allprop.Num=prop.Num
  107. p.push(Allprop)
  108. s += `名称:${Allprop.propName}---描述:${Allprop.describe}---数量:${prop.Num}\n`;
  109. }
  110. })
  111. })
  112. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  113. return {
  114. Prs:p,
  115. nickname:context?.sender?.nickname??"未知",
  116. template:{
  117. enabled: true,
  118. sendText: false,
  119. path: path.resolve(__dirname, '..','resources', 'prop',`userprop.html`),//模版路径,推荐按规范放置在resources目录下
  120. render: {//浏览器默认参数设置,用于打开浏览器的设置
  121. width: 800, // 模板宽度
  122. type: 'png',// 模板类型
  123. quality: 100,// 模板质量
  124. fullPage: false,// 是否全屏
  125. background: true// 是否背景
  126. },
  127. },
  128. toString(){
  129. return s;
  130. }
  131. }
  132. }
  133. @runcod(["buy","买"],"购买道具")
  134. async buyprop(
  135. @param("道具Id", 'text') propId: Receive["text"],
  136. @param("数量", 'text', {type:'text',data:{text:"1"}}, true) Num: Receive["text"],
  137. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  138. ){
  139. if(Number(Num?.data?.text)<=0){
  140. return this.tl('道具数量不能小于0','error','道具数量不能小于0')
  141. }
  142. let res = ''
  143. Props.forEach(async (prop) => {
  144. if(prop.propId===propId?.data?.text){
  145. removeCoins(context?.sender?.user_id?.toString(),prop?.price??0 * Number(Num?.data?.text),`购买道具${prop?.propName??'未知'}`)
  146. addProp(context?.sender?.user_id?.toString(),prop?.propId,Number(Num?.data?.text))
  147. res = `购买${prop.propName}成功!消费${prop.price * Number(Num?.data?.text)}!`
  148. }
  149. })
  150. return this.tl(res,'success',res)
  151. }
  152. tl ( msg:string, type:'success'|'error',text:string) {
  153. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  154. return {
  155. msgtype: 'success',
  156. ecomsg: msg,
  157. template: {
  158. enabled: true,
  159. sendText: false,
  160. path: path.resolve(__dirname, '..', 'resources', 'ecomony', 'msg.html'),//模版路径,推荐按规范放置在resources目录下
  161. render: {//浏览器默认参数设置,用于打开浏览器的设置
  162. width: 800, // 模板宽度
  163. type: 'png',// 模板类型
  164. quality: 100,// 模板质量
  165. fullPage: false,// 是否全屏
  166. background: true// 是否背景
  167. }
  168. },
  169. toString() { //重写toString方法,用于返回文本内容,启用sendText时将发送文本内容,不启用时将发送图片内容,图片发送失败时发送文字内容
  170. return text;
  171. }
  172. }
  173. }
  174. }