Skip to content

Commit

Permalink
feat(tomon): init tomon adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 13, 2020
1 parent 356b9e4 commit b768f55
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions packages/adapter-tomon/src/api.ts
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions packages/adapter-tomon/src/index.ts
Original file line number Diff line number Diff line change
@@ -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() {}
}
10 changes: 10 additions & 0 deletions packages/adapter-tomon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down

0 comments on commit b768f55

Please sign in to comment.