Skip to content

Commit

Permalink
feat: adapter-discord, slack, telegram, sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Aug 5, 2024
1 parent 4fe188c commit 089a8cc
Show file tree
Hide file tree
Showing 126 changed files with 6,019 additions and 1,718 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
uses: pnpm/action-setup@v2
with:
version: 8
- name: Clone submodules
run: |
git submodule init
git submodule update
- name: Install packages
run: pnpm install --no-frozen-lockfile
- name: Build packages
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ data
*.log
tsconfig.tsbuildinfo
test.http

kotori.dev.*
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"name": "nodemon",
"program": "${workspaceFolder}",
"request": "launch",
"restart": true,
"runtimeExecutable": "nodemon",
"skipFiles": ["<node_internals>/**"],
"type": "node"
}
]
}
6 changes: 0 additions & 6 deletions jest.config.ts

This file was deleted.

41 changes: 0 additions & 41 deletions kotori.dev.toml

This file was deleted.

30 changes: 22 additions & 8 deletions modules/adapter-cmd/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
# @kotori-bot/kotori-plugin-adapter-cmd

Base on console i/o,a method for quickly testing modules,only support `private` scope.
Base on console i/o, a method for quickly testing modules, only support `MessageScope.PRIVATE`.

## Config

```typescript
interface CmdConfig extends Adapter {
nickname?: string; // name of user(console)
age?: string; // age of user
sex?: 'male' | 'female'; // sex of user
'self-nickname'?: string; // name of bot
'self-id'?: string | number; // id of bot
}
export const config = Tsu.Object({
nickname: Tsu.String().default('Kotarou').describe('User\'s nickname'),
'self-nickname': Tsu.String().default('KotoriO').describe('Bot\'s nickname'),
'self-id': Tsu.String().default('720').describe('Bot\'s id'),
})
```

## Supports

### Events

- on_message (only `MessageScope.PRIVATE`)

### Api

- sendPrivateMsg

### Elements

- text
- mention
- mentionAll

## Reference

- [Kotori Docs](https://kotori.js.org/)
2 changes: 1 addition & 1 deletion modules/adapter-cmd/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kotori-bot/kotori-plugin-adapter-cmd",
"version": "1.1.0",
"version": "1.1.1",
"description": "Adapter For Cmd",
"main": "lib/index.js",
"keywords": ["kotori", "chatbot", "kotori-plugin"],
Expand Down
16 changes: 6 additions & 10 deletions modules/adapter-cmd/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2024-08-01 17:44:48
* @LastEditTime: 2024-08-04 12:08:31
*/
import { Adapter, type AdapterConfig, type Context, MessageScope, Tsu } from 'kotori-bot'
import CmdApi from './api'
import CmdElements from './elements'

export const config = Tsu.Object({
nickname: Tsu.String().default('Kotarou'),
'self-nickname': Tsu.String().default('KotoriO'),
'self-id': Tsu.String().default('720')
nickname: Tsu.String().default('Kotarou').describe("User's nickname"),
'self-nickname': Tsu.String().default('KotoriO').describe("Bot's nickname"),
'self-id': Tsu.String().default('720').describe("Bot's id")
})

type CmdConfig = Tsu.infer<typeof config> & AdapterConfig

export class CmdAdapter extends Adapter<CmdApi, CmdConfig, CmdElements> {
private messageId = ''
public messageId = 0

public readonly platform = 'cmd'

Expand All @@ -41,7 +41,7 @@ export class CmdAdapter extends Adapter<CmdApi, CmdConfig, CmdElements> {
message = message.replace('\r\n', '').replace('\n', '')
this.session('on_message', {
type: MessageScope.PRIVATE,
messageId: this.messageId,
messageId: String(this.messageId),
message,
messageAlt: message,
userId: this.config.master,
Expand Down Expand Up @@ -81,10 +81,6 @@ export class CmdAdapter extends Adapter<CmdApi, CmdConfig, CmdElements> {
if ((params as { user_id: unknown }).user_id !== this.config.master) return
process.stdout.write(`${this.config['self-nickname']} > ${(params as { message: string }).message} \r\n`)
this.messageId += 1
this.ctx.emit('send', {
api: this.api,
messageId: this.messageId
})
}
}

Expand Down
15 changes: 12 additions & 3 deletions modules/adapter-cmd/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:13
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2024-07-30 19:06:41
* @LastEditTime: 2024-08-04 12:09:25
*/
import { Api, type Message } from 'kotori-bot'
import type CmdAdapter from './adapter'

export class CmdApi extends Api {
public readonly adapter: CmdAdapter

public constructor(adapter: CmdAdapter) {
super(adapter)
this.adapter = adapter
}

public getSupportedEvents(): ReturnType<Api['getSupportedEvents']> {
return ['on_message']
}

public async sendPrivateMsg(message: Message, userId: string) {
this.adapter.send('send_private_msg', { user_id: userId, message })
return { messageId: '', time: 0 }
this.adapter.send('send_private_msg', { user_id: userId, message: this.adapter.elements.decode(message) })
this.adapter.ctx.emit('send', { api: this, messageId: String(this.adapter.messageId) })
return { messageId: String(this.adapter.messageId), time: Date.now() }
}
}

Expand Down
10 changes: 6 additions & 4 deletions modules/adapter-cmd/src/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Elements, type Message, type MessageMapping, MessageSingle } from 'koto

export class CmdElements extends Elements {
public getSupportsElements(): (keyof MessageMapping)[] {
return ['mention', 'mentionAll', 'image', 'voice', 'video', 'text', 'location', 'file']
return ['mention', 'mentionAll', 'text']
}

public decode(message: Exclude<Message, string>): string {
if (!(message instanceof MessageSingle))
public decode(message: Message): string {
if (typeof message === 'string') return message
if (!(message instanceof MessageSingle)) {
return Array.from(message)
.map((el) => this.decode(el))
.join(' ')
.join('')
}
switch (message.data.type) {
case 'text':
return message.toString()
Expand Down
8 changes: 5 additions & 3 deletions modules/adapter-cmd/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CmdAdapter } from './adapter';
import { CmdAdapter } from './adapter'

export { config } from './adapter';
export default CmdAdapter;
export * from './adapter'
export * from './api'
export * from './elements'
export default CmdAdapter
35 changes: 35 additions & 0 deletions modules/adapter-discord/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# @kotori-bot/adapter-discord

Supports for discord. Create own bot: [Discord Developer Portal](https://discord.com/developers/applications).

## Config

```typescript
export const config = Tsu.Object({
token: Tsu.String().describe("Bot's token")
})
```

## Supports

### Events

- on_message (only `MessageScope.CHANNEL`)
- on_message_delete (only `MessageScope.CHANNEL`)

### Api

- sendChannelMsg

### Elements

- text

## Todo

Support more standard api...

## Reference

- [Kotori Docs](https://kotori.js.org/)
- [discord.js](https://discord.js.org/)
26 changes: 26 additions & 0 deletions modules/adapter-discord/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@kotori-bot/adapter-discord",
"version": "1.0.0",
"description": "Adapter For Discord",
"main": "lib/index.js",
"keywords": [
"kotori",
"chatbot",
"kotori-plugin",
"discord"
],
"license": "GPL-3.0",
"files": [
"lib",
"locales",
"LICENSE",
"README.md"
],
"author": "Arimura Sena <[email protected]>",
"peerDependencies": {
"kotori-bot": "workspace:^"
},
"dependencies": {
"discord.js": "^14.15.3"
}
}
Loading

0 comments on commit 089a8cc

Please sign in to comment.