From 10aebe67e775007c30117e9dc8436232724e179e Mon Sep 17 00:00:00 2001 From: theluyuan <1162963624@qq.com> Date: Thu, 2 Jun 2022 00:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=BA=8B=E4=BB=B6=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/channel-core/base.js | 16 +++++++++ bin/channel-core/index.js | 44 +++++++++++++++++++++++ bin/message/base.js | 4 +++ bin/message/index.js | 31 ++++++++++++++-- index.js | 60 +++---------------------------- plugins/activeStatistics/index.js | 10 ++++++ 6 files changed, 106 insertions(+), 59 deletions(-) create mode 100644 bin/channel-core/base.js create mode 100644 bin/channel-core/index.js create mode 100644 bin/message/base.js create mode 100644 plugins/activeStatistics/index.js diff --git a/bin/channel-core/base.js b/bin/channel-core/base.js new file mode 100644 index 0000000..601fcf6 --- /dev/null +++ b/bin/channel-core/base.js @@ -0,0 +1,16 @@ +import { createOpenAPI, createWebsocket } from 'qq-guild-bot'; + + +const testConfig = { + appID: '102011802', // 申请机器人时获取到的机器人 BotAppID + token: 'YPwwxVTwbOy9ULfvsKpmax2PC4xXr74x', // 申请机器人时获取到的机器人 BotToken + intents: ['GUILD_MESSAGES', 'MESSAGE_AUDIT'], // 事件订阅,用于开启可接收的消息类型 + sandbox: true, // 沙箱支持,可选,默认false. v2.7.0+ +}; + +// 创建 client +export const client = createOpenAPI(testConfig); + +// 创建 websocket 连接 +export const ws = createWebsocket(testConfig); + diff --git a/bin/channel-core/index.js b/bin/channel-core/index.js new file mode 100644 index 0000000..f09bea3 --- /dev/null +++ b/bin/channel-core/index.js @@ -0,0 +1,44 @@ +import { messageCreate, messageDelete } from "../message/index.js"; +import { ws } from "./base.js"; +// 消息监听 +ws.on('READY', (wsdata) => { + console.log('[READY] 事件接收 :', wsdata); +}); +ws.on('ERROR', (data) => { + console.log('[ERROR] 事件接收 :', data); +}); +ws.on('GUILDS', (data) => { + console.log('[GUILDS] 事件接收 :', data); +}); +ws.on('GUILD_MEMBERS', (data) => { + console.log('[GUILD_MEMBERS] 事件接收 :', data); +}); +ws.on('GUILD_MESSAGES', (data) => { + // console.log('[GUILD_MESSAGES] 事件接收 :', JSON.stringify(data)); + if(data.eventType == "MESSAGE_CREATE"){ + messageCreate(data.msg) + }else if(data.eventType == "MESSAGE_DELETE"){ + messageDelete(data.msg) + } +}); +ws.on('GUILD_MESSAGE_REACTIONS', (data) => { + console.log('[GUILD_MESSAGE_REACTIONS] 事件接收 :', data); +}); +ws.on('DIRECT_MESSAGE', (data) => { + console.log('[DIRECT_MESSAGE] 事件接收 :', data); +}); +ws.on('INTERACTION', (data) => { + console.log('[INTERACTION] 事件接收 :', data); +}); +ws.on('MESSAGE_AUDIT', (data) => { + console.log('[MESSAGE_AUDIT] 事件接收 :', data); +}); +ws.on('FORUMS_EVENT', (data) => { + console.log('[FORUMS_EVENT] 事件接收 :', data); +}); +ws.on('AUDIO_ACTION', (data) => { + console.log('[AUDIO_ACTION] 事件接收 :', data); +}); +ws.on('PUBLIC_GUILD_MESSAGES', (data) => { + console.log('[PUBLIC_GUILD_MESSAGES] 事件接收 :', data); +}); diff --git a/bin/message/base.js b/bin/message/base.js new file mode 100644 index 0000000..1483091 --- /dev/null +++ b/bin/message/base.js @@ -0,0 +1,4 @@ +import mitt from 'mitt' +const emitter = mitt() + +export default emitter \ No newline at end of file diff --git a/bin/message/index.js b/bin/message/index.js index 1483091..5c37fab 100644 --- a/bin/message/index.js +++ b/bin/message/index.js @@ -1,4 +1,29 @@ -import mitt from 'mitt' -const emitter = mitt() +import msg from "./base.js" + + +/** + * 新消息通知 + * @param {Object} message message对象 + */ +export function messageCreate(message){ + msg.emit("messageCreate",message) + +} + +/** + * 注册新消息通知 + * @param {Function} fun 回调函数 + */ + +export function onMessageCreate(fun){ + msg.on("messageCreate",fun) +} + +/** + * 撤回消息通知 + * @param {Object} message message对象 + */ +export function messageDelete(message){ + msg.emit("messageDelete",message) +} -export default emitter \ No newline at end of file diff --git a/index.js b/index.js index ebcf773..9ef21e7 100644 --- a/index.js +++ b/index.js @@ -1,59 +1,7 @@ -import { createOpenAPI, createWebsocket } from 'qq-guild-bot'; +import "./bin/channel-core/index.js" -import message from './bin/message/index.js' +// 加载插件 +import "./plugins/activeStatistics/index.js" -message.$emit("aaa") -const testConfig = { - appID: '102011802', // 申请机器人时获取到的机器人 BotAppID - token: 'YPwwxVTwbOy9ULfvsKpmax2PC4xXr74x', // 申请机器人时获取到的机器人 BotToken - intents: ['GUILD_MESSAGES','MESSAGE_AUDIT'], // 事件订阅,用于开启可接收的消息类型 - sandbox: true, // 沙箱支持,可选,默认false. v2.7.0+ -}; -// 创建 client -const client = createOpenAPI(testConfig); - -// 创建 websocket 连接 -const ws = createWebsocket(testConfig); -// 消息监听 -ws.on('READY', (wsdata) => { - console.log('[READY] 事件接收 :', wsdata); - }); - ws.on('ERROR', (data) => { - console.log('[ERROR] 事件接收 :', data); - }); - ws.on('GUILDS', (data) => { - console.log('[GUILDS] 事件接收 :', data); - }); - ws.on('GUILD_MEMBERS', (data) => { - console.log('[GUILD_MEMBERS] 事件接收 :', data); - }); - ws.on('GUILD_MESSAGES', (data) => { - console.log('[GUILD_MESSAGES] 事件接收 :', JSON.stringify(data)); - client.messageApi.deleteMessage(data.msg.channel_id, data.msg.id, false ).then(({d})=>{ - console.log(d) - }).catch((err)=>{ - console.log(err) - }) - }); - ws.on('GUILD_MESSAGE_REACTIONS', (data) => { - console.log('[GUILD_MESSAGE_REACTIONS] 事件接收 :', data); - }); - ws.on('DIRECT_MESSAGE', (data) => { - console.log('[DIRECT_MESSAGE] 事件接收 :', data); - }); - ws.on('INTERACTION', (data) => { - console.log('[INTERACTION] 事件接收 :', data); - }); - ws.on('MESSAGE_AUDIT', (data) => { - console.log('[MESSAGE_AUDIT] 事件接收 :', data); - }); - ws.on('FORUMS_EVENT', (data) => { - console.log('[FORUMS_EVENT] 事件接收 :', data); - }); - ws.on('AUDIO_ACTION', (data) => { - console.log('[AUDIO_ACTION] 事件接收 :', data); - }); - ws.on('PUBLIC_GUILD_MESSAGES', (data) => { - console.log('[PUBLIC_GUILD_MESSAGES] 事件接收 :', data); - }); \ No newline at end of file +console.log(1) \ No newline at end of file diff --git a/plugins/activeStatistics/index.js b/plugins/activeStatistics/index.js new file mode 100644 index 0000000..7145fce --- /dev/null +++ b/plugins/activeStatistics/index.js @@ -0,0 +1,10 @@ +import { onMessageCreate } from "../../bin/message/index.js" + +/** + * 收到消息处理函数 + * @param {Object} msg 消息对象 + */ +function createMessage(msg){ + console.log("收到消息",msg) +} +onMessageCreate(createMessage) \ No newline at end of file