-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
388 additions
and
384 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,15 @@ | |
* @Blog: https://hotaru.icu | ||
* @Date: 2023-09-29 14:31:09 | ||
* @LastEditors: Hotaru [email protected] | ||
* @LastEditTime: 2024-05-02 18:12:01 | ||
* @LastEditTime: 2024-06-07 20:14:31 | ||
*/ | ||
import { Adapter, AdapterConfig, Context, MessageScope, Tsu, stringTemp } from 'kotori-bot'; | ||
import { AdapterConfig, Adapters, Context, MessageScope, Tsu, stringTemp } from 'kotori-bot'; | ||
import Mcwss from 'mcwss'; | ||
import WebSocket from 'ws'; | ||
import McApi from './api'; | ||
import McElements from './elements'; | ||
|
||
export const config = Tsu.Object({ | ||
port: Tsu.Number().int().range(1, 65535), | ||
address: Tsu.String() | ||
.regexp(/^ws(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$/) | ||
.default('ws://127.0.0.1'), | ||
nickname: Tsu.String().default('Romi'), | ||
template: Tsu.Union([Tsu.Null(), Tsu.String()]).default('<%nickname%> %msg%') | ||
}); | ||
|
@@ -30,9 +27,7 @@ type MessageData = | |
: never | ||
: never; | ||
|
||
export class McAdapter extends Adapter<McApi> { | ||
private app?: Mcwss; | ||
|
||
export class McAdapter extends Adapters.WebSocket<McApi, MessageData> { | ||
private clients: Record<string, MessageData['client']> = {}; | ||
|
||
private messageId = 1; | ||
|
@@ -44,7 +39,27 @@ export class McAdapter extends Adapter<McApi> { | |
this.config = config; | ||
} | ||
|
||
public handle(data: MessageData) { | ||
public start() { | ||
this.connection = (ws, req) => { | ||
const fakeServer = new WebSocket.Server({ noServer: true }); | ||
const app = new Mcwss({ server: fakeServer }); | ||
app.on('error', (err) => this.ctx.logger.error(err)); | ||
app.on('connection', (data) => { | ||
this.online(); | ||
this.clients[data.client.sessionId] = data.client; | ||
}); | ||
app.on('player_message', (data) => this.onMessage(data)); | ||
app.start(); | ||
fakeServer.emit('connection', ws, req); | ||
}; | ||
this.setup(); | ||
} | ||
|
||
public handle() { | ||
this.handle.toString(); | ||
} | ||
|
||
public onMessage(data: MessageData) { | ||
this.session('on_message', { | ||
type: data.body.type === 'chat' ? MessageScope.GROUP : MessageScope.PRIVATE, | ||
messageId: this.messageId, | ||
|
@@ -60,29 +75,6 @@ export class McAdapter extends Adapter<McApi> { | |
this.messageId += 1; | ||
} | ||
|
||
public start() { | ||
this.connect(); | ||
this.ctx.emit('connect', { | ||
type: 'connect', | ||
adapter: this, | ||
normal: true, | ||
mode: 'ws-reverse', | ||
address: `${this.config.address}:${this.config.port}` | ||
}); | ||
} | ||
|
||
public stop() { | ||
this.app?.stop(); | ||
this.ctx.emit('connect', { | ||
type: 'disconnect', | ||
adapter: this, | ||
normal: true, | ||
mode: 'other', | ||
address: `${this.config.address}:${this.config.port}` | ||
}); | ||
this.offline(); | ||
} | ||
|
||
public send(action: string, params: { msg: string }) { | ||
const [sessionId, playerName] = action.split('@'); | ||
const { msg } = params; | ||
|
@@ -99,17 +91,6 @@ export class McAdapter extends Adapter<McApi> { | |
messageId: this.messageId | ||
}); | ||
} | ||
|
||
private connect() { | ||
this.app = new Mcwss({ port: this.config.port, autoRegister: true, autoClearEvents: true }); | ||
this.app.on('error', (err) => this.ctx.logger.error(err)); | ||
this.app.on('connection', (data) => { | ||
if (Object.keys(this.clients).length === 0) this.online(); | ||
this.clients[data.client.sessionId] = data.client; | ||
}); | ||
this.app.on('player_message', (data) => this.handle(data)); | ||
this.app.start(); | ||
} | ||
} | ||
|
||
export default McAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,11 @@ | |
* @Blog: https://hotaru.icu | ||
* @Date: 2023-09-29 14:31:09 | ||
* @LastEditors: Hotaru [email protected] | ||
* @LastEditTime: 2024-02-16 15:33:09 | ||
* @LastEditTime: 2024-06-07 19:53:00 | ||
*/ | ||
import { Adapter, AdapterConfig, Context, EventDataApiBase, EventDataTargetId, MessageScope, Tsu } from 'kotori-bot'; | ||
import { Adapters, AdapterConfig, Context, EventDataApiBase, EventDataTargetId, MessageScope, Tsu } from 'kotori-bot'; | ||
import WebSocket from 'ws'; | ||
import OnebotApi from './api'; | ||
import WsServer from './services/wsserver'; | ||
import { EventDataType } from './types'; | ||
import OnebotElements from './elements'; | ||
|
||
|
@@ -24,37 +23,40 @@ declare module 'kotori-bot' { | |
} | ||
} | ||
|
||
export const config = Tsu.Intersection([ | ||
export const config = Tsu.Union([ | ||
Tsu.Object({ | ||
mode: Tsu.Literal('ws'), | ||
port: Tsu.Number().int().range(1, 65535), | ||
address: Tsu.String() | ||
.regexp(/^ws(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$/) | ||
.default('ws://127.0.0.1'), | ||
retry: Tsu.Number().int().min(1).default(10) | ||
}), | ||
Tsu.Union([ | ||
Tsu.Object({ | ||
mode: Tsu.Literal('ws') | ||
}), | ||
Tsu.Object({ | ||
mode: Tsu.Literal('ws-reverse') | ||
}) | ||
]) | ||
Tsu.Object({ | ||
mode: Tsu.Literal('ws-reverse') | ||
}) | ||
]); | ||
|
||
type OnebotConfig = Tsu.infer<typeof config> & AdapterConfig; | ||
|
||
const handleMsg = (msg: string) => msg.replace(/\[CQ:at,qq=(.*?)\]/g, '$1'); | ||
|
||
export class OnebotAdapter extends Adapter { | ||
export class OnebotAdapter extends Adapters.WebSocket<OnebotApi> { | ||
private readonly address: string; | ||
|
||
private readonly isReverse: boolean; | ||
|
||
public readonly config: OnebotConfig; | ||
|
||
public constructor(ctx: Context, config: OnebotConfig, identity: string) { | ||
super(ctx, config, identity, OnebotApi, new OnebotElements()); | ||
this.config = config; | ||
this.address = `${this.config.address ?? 'ws://127.0.0.1'}:${this.config.port}`; | ||
this.address = this.config.mode === 'ws' ? `${this.config.address ?? 'ws://127.0.0.1'}:${this.config.port}` : ''; | ||
this.isReverse = !this.address; | ||
if (!this.isReverse) return; | ||
this.connection = (ws) => { | ||
this.socket = ws; | ||
}; | ||
} | ||
|
||
public handle(data: EventDataType) { | ||
|
@@ -168,32 +170,48 @@ export class OnebotAdapter extends Adapter { | |
groupId: data.group_id! | ||
}); | ||
} | ||
if (!this.onlineTimerId) this.onlineTimerId = setTimeout(() => this.offline, 50 * 1000); | ||
if (!this.onlineTimerId) this.onlineTimerId = setTimeout(() => this.offline(), 50 * 1000); | ||
} | ||
|
||
public start() { | ||
if (this.config.mode === 'ws-reverse') { | ||
if (this.isReverse) { | ||
this.setup(); | ||
return; | ||
} | ||
/* ws mode handle */ | ||
this.ctx.emit('connect', { | ||
type: 'connect', | ||
mode: 'ws', | ||
adapter: this, | ||
normal: true, | ||
address: this.address | ||
}); | ||
this.socket = new WebSocket(`${this.address}`); | ||
this.socket.on('close', () => { | ||
this.ctx.emit('connect', { | ||
type: 'connect', | ||
mode: 'ws-reverse', | ||
type: 'disconnect', | ||
adapter: this, | ||
normal: true, | ||
normal: false, | ||
mode: 'ws', | ||
address: this.address | ||
}); | ||
} | ||
this.connectWss(); | ||
}); | ||
this.socket.on('message', (raw) => this.handle(JSON.parse(raw.toString()))); | ||
} | ||
|
||
public stop() { | ||
if (this.isReverse) { | ||
super.stop(); | ||
return; | ||
} | ||
this.ctx.emit('connect', { | ||
type: 'disconnect', | ||
adapter: this, | ||
normal: true, | ||
address: this.address, | ||
mode: this.config.mode | ||
mode: 'ws' | ||
}); | ||
this.socket?.close(); | ||
this.offline(); | ||
} | ||
|
||
public send(action: string, params?: object) { | ||
|
@@ -202,45 +220,6 @@ export class OnebotAdapter extends Adapter { | |
|
||
private socket: WebSocket | null = null; | ||
|
||
private async connectWss() { | ||
if (this.config.mode === 'ws-reverse') { | ||
const wss = await WsServer(this.config.port); | ||
const [socket] = wss; | ||
this.socket = socket; | ||
} else { | ||
this.ctx.emit('connect', { | ||
type: 'connect', | ||
mode: 'ws', | ||
adapter: this, | ||
normal: true, | ||
address: this.address | ||
}); | ||
this.socket = new WebSocket(`${this.address}`); | ||
this.socket.on('close', () => { | ||
this.ctx.emit('connect', { | ||
type: 'disconnect', | ||
adapter: this, | ||
normal: false, | ||
mode: 'ws', | ||
address: this.address | ||
}); | ||
setTimeout(() => { | ||
if (!this.socket) return; | ||
this.socket.close(); | ||
this.ctx.emit('connect', { | ||
type: 'connect', | ||
mode: 'ws-reverse', | ||
adapter: this, | ||
normal: false, | ||
address: this.address | ||
}); | ||
this.connectWss(); | ||
}, this.config.retry * 1000); | ||
}); | ||
} | ||
this.socket.on('message', (data) => this.handle(JSON.parse(data.toString()))); | ||
} | ||
|
||
/* global NodeJS */ | ||
private onlineTimerId: NodeJS.Timeout | null = null; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.