Browse Source

✨ feat: 增加/修复MCP功能

Pchen. 1 month ago
parent
commit
097e4868f4
3 changed files with 71 additions and 20 deletions
  1. 17 2
      apis/MCP/McpRPC.js
  2. 1 1
      lib/Lepao/Lepao.js
  3. 53 17
      lib/Lepao/Mcp.js

+ 17 - 2
apis/MCP/McpRPC.js

@@ -39,7 +39,7 @@ class McpRpc extends API {
                         },
                         },
                         {
                         {
                             "name": "get_account_info",
                             "name": "get_account_info",
-                            "description": "获取账号信息",
+                            "description": "获取账号信息,包含姓名学号、帐号状态、乐跑时间次数等",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": { "sender": { "type": "string" } },
                                 "properties": { "sender": { "type": "string" } },
@@ -57,7 +57,7 @@ class McpRpc extends API {
                         },
                         },
                         {
                         {
                             "name": "set_notification",
                             "name": "set_notification",
-                            "description": "消息通知设置,mode参数:bot:智能机器人通知、email:邮件通知、none:关闭通知",
+                            "description": "消息通知设置,mode参数:bot:智能机器人通知、email:邮件通知、none:关闭通知",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": {
                                 "properties": {
@@ -66,6 +66,18 @@ class McpRpc extends API {
                                 },
                                 },
                                 "required": ["sender", "mode"]
                                 "required": ["sender", "mode"]
                             }
                             }
+                        },
+                        {
+                            "name": "change_email",
+                            "description": "更换通知邮箱,需传入合法邮箱地址:email",
+                            "inputSchema": {
+                                "type": "object",
+                                "properties": {
+                                    "sender": { "type": "string" },
+                                    "email": { "type": "string" }
+                                },
+                                "required": ["sender", "email"]
+                            }
                         }
                         }
                     ]
                     ]
                 }
                 }
@@ -87,6 +99,9 @@ class McpRpc extends API {
                     case "set_notification":
                     case "set_notification":
                         output = await MCP.set_notification(args)
                         output = await MCP.set_notification(args)
                         break
                         break
+                    case "change_email":
+                        output = await MCP.change_email(args)
+                        break
                     default:
                     default:
                         output = "未知工具"
                         output = "未知工具"
                 }
                 }

+ 1 - 1
lib/Lepao/Lepao.js

@@ -424,7 +424,7 @@ class Lepao {
                 umo: rows[0].bot_umo,
                 umo: rows[0].bot_umo,
                 name: rows[0].name,
                 name: rows[0].name,
                 account,
                 account,
-                reason: reason === 'Request failed with status code 503' ? 'RunForge系统维护中,请稍后再试' : reason
+                reason: (reason === 'Request failed with status code 503' || reason === 'Request failed with status code 500') ? '系统繁忙,请稍后再试' : reason
             }
             }
 
 
             if (rows[0].notice_type === 'bot' && rows[0].bot_umo) {
             if (rows[0].notice_type === 'bot' && rows[0].bot_umo) {

+ 53 - 17
lib/Lepao/Mcp.js

@@ -1,10 +1,13 @@
 const db = require('../../plugin/DataBase/db')
 const db = require('../../plugin/DataBase/db')
 const path = require('path')
 const path = require('path')
 const Logger = require('../Logger')
 const Logger = require('../Logger')
+const mq = require('../../plugin/mq')
 
 
 class Mcp {
 class Mcp {
     constructor() {
     constructor() {
         this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
         this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
+        
+        this.messageQueue = 'runforge_message_queue'
 
 
         this.auto_day = [
         this.auto_day = [
             { label: '周一', value: 1 },
             { label: '周一', value: 1 },
@@ -25,6 +28,9 @@ class Mcp {
             const match = auto_time.find(item => item.value === record.auto_time)
             const match = auto_time.find(item => item.value === record.auto_time)
             return match ? match.label : '-'
             return match ? match.label : '-'
         }
         }
+
+        this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
+        this.banEmailList = ['icloud.com']
     }
     }
 
 
     async bind_account({ sender, bind_code }) {
     async bind_account({ sender, bind_code }) {
@@ -32,7 +38,7 @@ class Mcp {
             this.logger.info(`MCP接收绑定账号请求:${sender},绑定码:${bind_code}`)
             this.logger.info(`MCP接收绑定账号请求:${sender},绑定码:${bind_code}`)
             let sql = `
             let sql = `
                 SELECT 
                 SELECT 
-                    f.student_num, f.name, f.bot_account, a.name
+                    f.student_num, a.name, f.bot_account
                 FROM 
                 FROM 
                     lepao_extra f
                     lepao_extra f
                 LEFT JOIN 
                 LEFT JOIN 
@@ -97,25 +103,21 @@ class Mcp {
 
 
             let data = rows[0]
             let data = rows[0]
             let returnMsg = `
             let returnMsg = `
-                姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'}
-                学号:${data.student_num}
-                账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'}
-                乐跑跑区:${data.area ?? "随机分配"}
-                自动乐跑状态:${data.state === 1 ? '开启' : '关闭'}
+                姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'};学号:${data.student_num};账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'};乐跑跑区:${data.area ?? "随机分配"};自动乐跑状态:${data.state === 1 ? '开启' : '关闭'}
                 `
                 `
             if (data.auto_run === 1) {
             if (data.auto_run === 1) {
-                returnMsg += `自动乐跑时间:${this.autoTimeLabel(data)}`
+                returnMsg += `自动乐跑时间:${this.autoTimeLabel(data)};`
                 returnMsg += `自动乐跑星期:${data.auto_day.slice().sort((a, b) => {
                 returnMsg += `自动乐跑星期:${data.auto_day.slice().sort((a, b) => {
                     if (a === 0) return 1; if (b === 0) return -1; return a - b;
                     if (a === 0) return 1; if (b === 0) return -1; return a - b;
-                }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',').replace(/周/g, '')}`
+                }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',').replace(/周/g, '')};`
             }
             }
-            if (data.sex) returnMsg += `性别:${data.sex === 1 ? '男' : '女'}`
-            if (data.email) returnMsg += `邮箱:${data.email}`
-            if (data.grade) returnMsg += `邮箱:${data.grade}`
-            if (data.academy_name) returnMsg += `学院:${data.academy_name}`
-            if (data.grade_id) returnMsg += `年级:${data.grade_id}级`
-            if (data.target_count) returnMsg += `目标乐跑次数:${data.target_count}`
-            if (data.total_num) returnMsg += `累计乐跑次数:${data.total_num}`
+            if (data.sex) returnMsg += `性别:${data.sex === 1 ? '男' : '女'};`
+            if (data.email) returnMsg += `邮箱:${data.email};`
+            if (data.grade) returnMsg += `邮箱:${data.grade};`
+            if (data.academy_name) returnMsg += `学院:${data.academy_name};`
+            if (data.grade_id) returnMsg += `年级:${data.grade_id}级;`
+            if (data.target_count) returnMsg += `目标乐跑次数:${data.target_count};`
+            if (data.total_num) returnMsg += `累计乐跑次数:${data.total_num};`
 
 
             return returnMsg
             return returnMsg
         } catch (error) {
         } catch (error) {
@@ -128,7 +130,7 @@ class Mcp {
         try {
         try {
             this.logger.info(`MCP接收解绑账号请求:${sender}`)
             this.logger.info(`MCP接收解绑账号请求:${sender}`)
             let insertSql = `
             let insertSql = `
-                UPDATE lepao_extra SET bot_account = NULL, bot_nmo = NULL WHERE bot_account = ?
+                UPDATE lepao_extra SET bot_account = NULL, bot_umo = NULL WHERE bot_account = ?
             `
             `
             let insertRows = await db.query(insertSql, [sender])
             let insertRows = await db.query(insertSql, [sender])
             if (!insertRows || insertRows.affectedRows !== 1)
             if (!insertRows || insertRows.affectedRows !== 1)
@@ -140,10 +142,44 @@ class Mcp {
         }
         }
     }
     }
 
 
+    async change_email({ sender, email }) {
+        try {
+            this.logger.info(`MCP接收更换邮箱请求:${sender}`)
+
+            if (!this.emailRegex.test(email))
+                return '请检查邮箱格式是否正确'
+
+            const emailDomain = email.split('@')[1].toLowerCase()
+            if (this.banEmailList.includes(emailDomain))
+                return `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
+
+            let insertSql = `
+                UPDATE 
+                    lepao_account la
+                JOIN 
+                    lepao_extra le 
+                ON 
+                    la.student_num = le.student_num
+                SET 
+                    la.email = ?
+                WHERE 
+                    le.bot_account = ?
+            `
+
+            let insertRows = await db.query(insertSql, [email, sender])
+            if (!insertRows || insertRows.affectedRows !== 1)
+                return '系统出错,请稍后再试'
+            return `更换成功`
+        } catch (error) {
+            this.logger.error(`MCP更换邮箱出错:${e}`)
+            return '系统出错,请稍后再试'
+        }
+    }
+
     async set_notification({ sender, mode }) {
     async set_notification({ sender, mode }) {
         try {
         try {
             this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
             this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
-            if(mode !== 'email' || mode !== 'bot' || mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
+            if (mode !== 'email' && mode !== 'bot' && mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
 
 
             let insertSql = `
             let insertSql = `
                 UPDATE 
                 UPDATE