From b768f5509ff3e5e17c5cbbf1162a6e8721dfa215 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 13 Sep 2020 15:16:35 +0800 Subject: [PATCH] feat(tomon): init tomon adapter --- packages/adapter-tomon/package.json | 41 +++++++++++++++++++++++ packages/adapter-tomon/src/api.ts | 13 ++++++++ packages/adapter-tomon/src/index.ts | 49 ++++++++++++++++++++++++++++ packages/adapter-tomon/tsconfig.json | 10 ++++++ tsconfig.json | 1 + 5 files changed, 114 insertions(+) create mode 100644 packages/adapter-tomon/package.json create mode 100644 packages/adapter-tomon/src/api.ts create mode 100644 packages/adapter-tomon/src/index.ts create mode 100644 packages/adapter-tomon/tsconfig.json diff --git a/packages/adapter-tomon/package.json b/packages/adapter-tomon/package.json new file mode 100644 index 0000000000..e4f28999ed --- /dev/null +++ b/packages/adapter-tomon/package.json @@ -0,0 +1,41 @@ +{ + "name": "koishi-adapter-tomon", + "description": "Tomon adapter for Koishi", + "version": "0.1.0", + "main": "dist/index.js", + "typings": "dist/index.d.ts", + "files": [ + "dist" + ], + "author": "Shigma <1700011071@pku.edu.cn>", + "license": "MIT", + "scripts": { + "lint": "eslint src --ext .ts", + "precommit": "yarn lint", + "prepack": "tsc -b" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/koishijs/koishi.git" + }, + "bugs": { + "url": "https://github.com/koishijs/koishi/issues" + }, + "homepage": "https://github.com/koishijs/koishi/tree/master/packages/koishi-core#readme", + "keywords": [ + "bot", + "tomon", + "chatbot", + "koishi" + ], + "peerDependencies": { + "koishi-core": "^2.2.2" + }, + "devDependencies": { + "koishi-test-utils": "^5.0.1" + }, + "dependencies": { + "koishi-utils": "^3.1.5", + "tomon-sdk": "^0.2.0" + } +} diff --git a/packages/adapter-tomon/src/api.ts b/packages/adapter-tomon/src/api.ts new file mode 100644 index 0000000000..8973c742b3 --- /dev/null +++ b/packages/adapter-tomon/src/api.ts @@ -0,0 +1,13 @@ +import { Bot, Session } from 'koishi-core' + +Bot.prototype.sendGroupMsg = Bot.prototype.sendPrivateMsg = async function (this: Bot, channelId, content) { + if (!content) return + const data = await this.tomon.api.route(`/channels/${channelId}/messages`).post({ data: { content } }) + console.log(data) + return 0 +} + +Session.prototype.$send = async function $send(this: Session, message: string) { + if (!message) return + await this.$bot.sendPrivateMsg(this.channelId, message) +} diff --git a/packages/adapter-tomon/src/index.ts b/packages/adapter-tomon/src/index.ts new file mode 100644 index 0000000000..935e1edc42 --- /dev/null +++ b/packages/adapter-tomon/src/index.ts @@ -0,0 +1,49 @@ +import Tomon from 'tomon-sdk' +import { Bot, Server } from 'koishi-core' + +export * from './api' + +declare module 'koishi-core/dist/server' { + interface BotOptions { + token?: string + } + + interface Bot { + tomon?: Tomon + } +} + +declare module 'koishi-core/dist/session' { + interface Session { + channelId?: number + } +} + +Server.types.tomon = class TomonServer extends Server { + async __listen(bot: Bot) { + const tomon = bot.tomon = new Tomon() + await tomon.start(bot.token) + bot.ready = true + const selfId = bot.selfId = +tomon.discriminator + tomon.on('MESSAGE_CREATE', async ({ d }) => { + const userId = +d.author.discriminator + if (userId === selfId) return + const session = this.prepare({ + ...d, + selfId, + userId, + message: d.content, + postType: 'message', + messageType: d['guild_id'] ? 'group' : 'private', + groupId: d['guild_id'], + }) + this.dispatch(session) + }) + } + + async _listen() { + await Promise.all(this.bots.map(bot => this.__listen(bot))) + } + + _close() {} +} diff --git a/packages/adapter-tomon/tsconfig.json b/packages/adapter-tomon/tsconfig.json new file mode 100644 index 0000000000..a497f05e83 --- /dev/null +++ b/packages/adapter-tomon/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + }, + "include": [ + "src", + ], +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 5bbe1c23f1..99e422d9a2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ { "path": "./packages/koishi-core" }, { "path": "./packages/koishi-test-utils" }, { "path": "./packages/adapter-cqhttp" }, + { "path": "./packages/adapter-tomon" }, { "path": "./packages/plugin-mysql" }, { "path": "./packages/plugin-mongo" },