跳到主要内容

Wechaty Plugin

Middleware is computer software that connects software components or applications. The software consists of a set of services that allows multiple processes running on one or more machines to interact.
Wikipedia

See also: What is middleware exactly?

A Purpose from @Gcaufy

Yesterday, in our contributor group, @Gcaufy suggested that it would be great to add supporting of middleware to the Wechaty ecosystem, like the following usage:

有没有人把 踢人那个做成通用组件。。。那个很实用呀

wechaty.use(KickoutPlugin({
room: 'RoomName',
}));

然后这个房间就有踢人功能了。

I feel that it is a Brilliant idea!

So how about we design a middleware system like this:

Wechaty.use(middleware: WechatyMiddleware)

type WechatyMiddleware = (this: Wechaty) => void

class Wechaty {
public use (middleware: WechatyMiddleWare) {
middleware.apply(this)
}
}

const kickoutPlugin = (options = {}) => {
const roomTopic = options.roomTopic
return function (this: Wechaty) {
this.on('message'), message => {
if (message.room()) && message.room().topic() === roomTopic) {
if (message.mentionSelf()) {
// check vote
message.room().del(...)
}
}
})
}
}

const wechaty = new Wechaty()
wechaty.use(kickOffPlugin({ roomTopic: 'Test Room' }))

Any comments about this design will be welcome.

P.S. The Kickout Feature was originally introduced from the PR add vote manager to manage vote message in room #4 authored by @windmemory.

History