ecomony.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { addCoins, getUserData, removeCoins } from "../lib/economy.js";
  2. import { param, plugins, runcod } from "../lib/decorators.js";
  3. import { GroupMessage, PrivateFriendMessage, PrivateGroupMessage } from "node-napcat-ts/dist/Interfaces.js";
  4. import path from "path";
  5. import { fileURLToPath } from "url";
  6. import { ParamType } from "../interface/plugin.js";
  7. import { IsAdmin } from "../lib/Permission.js";
  8. @plugins({
  9. name: "经济系统", //插件名称,用于显示在菜单中
  10. version: "1.0.0", //插件版本号,用于显示在菜单中
  11. describe: "官方经济插件", //插件描述,用于显示在菜单中
  12. author: "枫叶秋林",//插件作者,用于显示在菜单中
  13. help: { //插件帮助信息,用于显示在菜单中
  14. enabled: true, //是否启用帮助信息
  15. description: "显示帮助信息" //帮助信息描述
  16. }
  17. })
  18. export class ecomony {
  19. @runcod(["info","个人信息"], "获取个人金币信息")
  20. async ecomonyInfo(
  21. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  22. ) {
  23. const { economy } = await getUserData(context?.sender?.user_id?.toString())
  24. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  25. return {
  26. nickname: context?.sender?.nickname??"未知",
  27. coins: economy.coins,
  28. logs: economy.logs,
  29. avatar: `http://q1.qlogo.cn/g?b=qq&nk=${context?.sender?.user_id??0}&s=640`,
  30. template: {
  31. enabled: true,
  32. sendText: false,
  33. path: path.resolve(__dirname, '..', 'resources', 'ecomony', 'info.html'),//模版路径,推荐按规范放置在resources目录下
  34. render: {//浏览器默认参数设置,用于打开浏览器的设置
  35. width: 800, // 模板宽度
  36. type: 'png',// 模板类型
  37. quality: 100,// 模板质量
  38. fullPage: false,// 是否全屏
  39. background: true// 是否背景
  40. }
  41. },
  42. toString() { //重写toString方法,用于返回文本内容,启用sendText时将发送文本内容,不启用时将发送图片内容,图片发送失败时发送文字内容
  43. let logsString = "";
  44. economy.logs.forEach(log => {
  45. logsString += `类型: ${log.type} 数量: ${log.amount} 原因: ${log.reason} 时间: ${log.date}\n`;
  46. });
  47. return `
  48. 金币: ${economy.coins}\n
  49. ------明细记录----
  50. ${logsString}
  51. `;
  52. }
  53. }
  54. }
  55. @runcod(["add", "增加"], "增加金币")
  56. async addecomony(
  57. @param("QQ号", ParamType.Number,) userid: string,
  58. @param("数量", ParamType.Number) amount: number,
  59. @param("原因", ParamType.String,'管理员增加',true) reason: string,
  60. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  61. ) {
  62. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  63. try {
  64. if (!IsAdmin(context.sender.user_id)) {
  65. return {
  66. msgtype: 'error',
  67. ecomsg: `无权限,无法增加金币`,
  68. template: {
  69. enabled: true,
  70. sendText: false,
  71. path: path.resolve(__dirname, '..','resources', 'ecomony','msg.html'),//模版路径,推荐按规范放置在resources目录下
  72. render: {//浏览器默认参数设置,用于打开浏览器的设置
  73. width: 800, // 模板宽度
  74. height: 600,// 模板高度
  75. type: 'png',// 模板类型
  76. quality: 100,// 模板质量
  77. fullPage: false,// 是否全屏
  78. background: true// 是否背景
  79. }
  80. }
  81. }
  82. }
  83. addCoins(context.sender.user_id.toString(),amount,reason)
  84. const newcoins = (await getUserData(userid)).economy.coins
  85. return {
  86. msgtype: 'success',
  87. ecomsg: `增加成功! 金币 +${amount}, 当前数量: ${newcoins}`,
  88. template: {
  89. enabled: true,
  90. sendText: false,
  91. path: path.resolve(__dirname, '..', 'resources', 'ecomony', 'msg.html'),//模版路径,推荐按规范放置在resources目录下
  92. render: {//浏览器默认参数设置,用于打开浏览器的设置
  93. width: 800, // 模板宽度
  94. type: 'png',// 模板类型
  95. quality: 100,// 模板质量
  96. fullPage: false,// 是否全屏
  97. background: true// 是否背景
  98. }
  99. },
  100. toString() { //重写toString方法,用于返回文本内容,启用sendText时将发送文本内容,不启用时将发送图片内容,图片发送失败时发送文字内容
  101. return `
  102. 增加成功\n
  103. 数量: ${amount}\n
  104. 原因: ${reason}\n
  105. 时间: ${new Date().toLocaleString()}\n
  106. `;
  107. }
  108. }
  109. } catch (error) {
  110. return {
  111. type: 'error',
  112. ecomsg: `增加失败! 原因: ${(error as Error).message??'未知错误'}`,
  113. template: {
  114. enabled: true,
  115. sendText: false,
  116. path: path.resolve(__dirname, '..', 'resources', 'ecomony', 'msg.html'),//模版路径,推荐按规范放置在resources目录下
  117. render: {//浏览器默认参数设置,用于打开浏览器的设置
  118. width: 800, // 模板宽度
  119. type: 'png',// 模板类型
  120. quality: 100,// 模板质量
  121. fullPage: false,// 是否全屏
  122. background: true// 是否背景
  123. }
  124. },
  125. toString() { //重写toString方法,用于返回文本内容,启用sendText时将发送文本内容,不启用时将发送图片内容,图片发送失败时发送文字内容
  126. return `
  127. 增加成功\n
  128. 数量: ${amount}\n
  129. 原因: ${reason}\n
  130. 时间: ${new Date().toLocaleString()}\n
  131. `;
  132. }
  133. }
  134. }
  135. }
  136. @runcod(["reduce", "减少"], "减少金币")
  137. async reduceecomony(
  138. @param("QQ号", ParamType.Number,) userid: string,
  139. @param("数量", ParamType.Number) amount: number,
  140. @param("原因", ParamType.String,'管理员减少',true) reason: string,
  141. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  142. ){
  143. const __dirname = path.dirname(fileURLToPath(import.meta.url)); //获取当前文件的目录名
  144. try {
  145. if (!IsAdmin(context.sender.user_id)) {
  146. return {
  147. msgtype: 'error',
  148. ecomsg: `无权限,无法减少金币`,
  149. template: {
  150. enabled: true,
  151. sendText: false,
  152. path: path.resolve(__dirname, '..','resources', 'ecomony','msg.html'),//模版路径,推荐按规范放置在resources目录下
  153. render: {//浏览器默认参数设置,用于打开浏览器的设置
  154. width: 800, // 模板宽度
  155. height: 400,// 模板高度
  156. type: 'png',// 模板类型
  157. quality: 100,// 模板质量
  158. fullPage: false,// 是否全屏
  159. background: true// 是否背景
  160. }
  161. }
  162. }
  163. }
  164. removeCoins(context.sender.user_id.toString(),-amount,reason)
  165. const newcoins = (await getUserData(userid)).economy.coins
  166. return {
  167. msgtype:'success',
  168. ecomsg: `减少成功! 金币 -${amount}, 当前数量: ${newcoins}`,
  169. template: {
  170. enabled: true,
  171. sendText: false,
  172. path: path.resolve(__dirname, '..','resources', 'ecomony','msg.html'),//模版路径,推荐按规范放置在resources目录下
  173. render: {//浏览器默认参数设置,用于打开浏览器的设置
  174. width: 800, // 模板宽度
  175. height: 400,// 模板高度
  176. type: 'png',// 模板类型
  177. quality: 100,// 模板质量
  178. fullPage: false,// 是否全屏
  179. background: true// 是否背景
  180. }
  181. }
  182. }
  183. }
  184. catch (error) {
  185. return {
  186. msgtype: 'error',
  187. ecomsg: `减少失败! 原因: ${(error as Error).message??'未知错误'}`,
  188. template: {
  189. enabled: true,
  190. sendText: false,
  191. path: path.resolve(__dirname, '..','resources', 'ecomony','msg.html'),//模版路径,推荐按规范放置在resources目录下
  192. render: {//浏览器默认参数设置,用于打开浏览器的设置
  193. width: 800, // 模板宽度
  194. height: 400,// 模板高度
  195. type: 'png',// 模板类型
  196. quality: 100,// 模板质量
  197. fullPage: false,// 是否全屏
  198. background: true// 是否背景
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }