Commit 29beed82 authored by initialencounter's avatar initialencounter

修复重启后重复发送消息

parent 31dcef2a
Pipeline #21763 failed with stages
in 23 seconds
......@@ -29,4 +29,32 @@ plugins:
仅限 Windows 操作系统,需要安装 [特定版本微信](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.6.0.18/WeChatSetup-3.6.0.18.exe)
# docker部署
## 准备
* docker
* 开通过微信支付的微信账号,(不开通微信支付可能会登不上)
## 步骤
- 1.启动容器
- `docker run -p 5141:5140 initialencounter/koishi-wechaty:latest bash -c "cd /koimux_bot && yarn start"`
- 2.扫码
- `docker logs 容器ID`或者直接登录koishi控制台 `http://公网IP:5141` 或者 `http://127.0.0.1:5141` 扫码
# 联系我
- qq群:399899914
- 哔哩哔哩:https://space.bilibili.com/225995995
先在本地的微信中登录,然后启动该插件。
# 更新日志
- v1.1.7
- 修复重启后重复发送消息
- v1.1.6
- 更改elements的url为base64url
\ No newline at end of file
{
"name": "koishi-plugin-adapter-wechaty",
"name": "@initencounter/koishi-plugin-adapter-wechaty",
"description": "Koishi 微信适配器。",
"version": "1.1.5",
"version": "1.1.7",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
......@@ -12,9 +12,13 @@
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-adapter-wechaty.git"
"url": "https://code.mycard.moe/initencunter/koishi-plugin-adapter-wechaty.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"contributors": [
"Nanahira <nanahira@momobako.com>",
"initencounter <2911583893@qq.com>"
],
"license": "AGPL-3.0",
"keywords": [
"Koishi.js",
......@@ -25,9 +29,9 @@
"wechaty"
],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-adapter-wechaty/issues"
"url": "https://code.mycard.moe/initencunter/koishi-plugin-adapter-wechaty/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-adapter-wechaty",
"homepage": "https://code.mycard.moe/initencunter/koishi-plugin-adapter-wechaty",
"jest": {
"moduleFileExtensions": [
"js",
......@@ -47,7 +51,7 @@
},
"dependencies": {
"file-type": "16.5.4",
"koishi-thirdeye": "^11.1.21",
"koishi-thirdeye": "^11.1.27",
"mime2ext": "^1.0.1",
"qrcode-terminal": "^0.12.0",
"wechaty": "^1.20.2",
......
import {
Context,
Adapter,
Awaitable,
Logger,
......@@ -6,19 +7,47 @@ import {
MaybeArray,
Session,
} from 'koishi';
import type WechatyBot from './index';
import WechatyBot, { WechatyBotConfig } from './index';
import { DefinePlugin, InjectLogger, Reusable } from 'koishi-thirdeye';
import { PassthroughEvents } from './def';
import type { WechatyEventListeners } from 'wechaty/src/schemas/mod';
import { adaptContact, adaptMessage, adaptRoom } from './utils';
import type { ContactInterface } from 'wechaty/src/user-modules/contact';
declare module 'koishi' {
interface Tables {
wechatymsg: WechatyHistory
}
}
export type WechatyHistory = {
id: number
messageId: string
date: number
}
declare module 'koishi' {
interface Context {
wechatyBot: WechatyBot
}
}
@Reusable()
@DefinePlugin()
export class WechatyAdapter extends Adapter.Client<WechatyBot> {
constructor(ctx: Context, bot: WechatyBot) {
super(ctx, bot)
ctx.model.extend('wechatymsg', {
id: 'integer',
messageId: 'string',
date: 'integer'
}, {
primary: 'id',
autoInc: true
})
}
@InjectLogger()
private logger: Logger;
private adaptEvent<K extends keyof WechatyEventListeners>(
event: K,
sessionBuilder: (
......@@ -34,6 +63,12 @@ export class WechatyAdapter extends Adapter.Client<WechatyBot> {
if (!payload) return;
payload.timestamp ??= timestamp;
const session = this.bot.session(payload);
if (
session.type === 'message' &&
Date.now() - this.bot.loginTime < 1000
) {
return;
}
this.bot.dispatch(session);
});
});
......@@ -43,14 +78,31 @@ export class WechatyAdapter extends Adapter.Client<WechatyBot> {
this.adaptEvent('message', async (message) => {
const adaptedMessage = await adaptMessage(this.bot, message);
if (!adaptedMessage) return;
const data = await this.ctx.database.get('wechatymsg',{})
const max_cache:number = this.bot.config.max_msgId_cache
if(data.length>max_cache){
for(var i of data.slice(0,data.length-max_cache-1)){
this.ctx.database.remove('wechatymsg',[i.id])
}
}
const history = await this.ctx.database.get('wechatymsg', { messageId: [adaptedMessage.messageId] })
if (history.length > 1) {
for (var i of history) {
if (i.messageId == adaptedMessage.messageId) {
return;
}
}
} else {
this.ctx.database.create('wechatymsg', { messageId: adaptedMessage.messageId, date: new Date().getMonth() })
}
return {
...adaptedMessage,
type:
message.type() === this.bot.internal.Message.Type.Recalled
? 'message-deleted'
: adaptedMessage.userId === this.bot.selfId
? 'message-sent'
: 'message',
? 'message-sent'
: 'message',
subsubtype: adaptedMessage.subtype,
};
});
......
......@@ -26,7 +26,8 @@ declare module 'koishi' {
export class WechatyBotConfig {
@SchemaProperty({ required: true, description: 'Wechaty 配置文件保存路径。' })
name: string;
@SchemaProperty({description: "消息ID最大缓存数", default: 5000})
max_msgId_cache: number;
@SchemaProperty({
default: 'wechaty-puppet-wechat',
description: 'Wechaty 使用的 Puppet。',
......@@ -68,12 +69,14 @@ export default class WechatyBot extends Bot<
this.internal = WechatyBuilder.build(this.config as any);
return PluginDef(WechatyAdapter, this);
}
loginTime: number;
async initialize() {
this.internal.on('login', async (user) => {
this.selfId = user.id;
this.username = user.name();
this.avatar = await fileBoxToUrl(await user.avatar());
this.loginTime = Date.now();
this.online();
});
this.internal.on('logout', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment