Skip to content

Commit

Permalink
feat!: migrate to satori v3
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamOfIce committed Oct 7, 2023
1 parent d18d266 commit 9f378f0
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 154 deletions.
39 changes: 20 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,35 @@
"file-type": "^16.5.4"
},
"devDependencies": {
"@commitlint/cli": "^17.7.0",
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@koishijs/eslint-config": "^1.0.4",
"@release-it/conventional-changelog": "^7.0.0",
"@tsconfig/esm": "^1.0.4",
"@tsconfig/node-lts": "^18.12.3",
"@tsconfig/strictest": "^2.0.1",
"@types/koa": "^2.13.8",
"@types/koa__router": "^12.0.0",
"@types/node": "^20.4.9",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"dotenv-cli": "^7.2.1",
"eslint": "^8.46.0",
"@release-it/conventional-changelog": "^7.0.2",
"@satorijs/protocol": "^1.0.1",
"@tsconfig/node-lts": "^18.12.5",
"@tsconfig/strictest": "^2.0.2",
"@types/koa": "^2.13.9",
"@types/koa__router": "^12.0.1",
"@types/node": "^20.8.3",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"dotenv-cli": "^7.3.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-import": "^2.28.1",
"husky": "^8.0.3",
"koishi": "^4.14.1",
"koishi": "^4.15.0",
"nano-staged": "^0.8.0",
"pinst": "^3.0.0",
"prettier": "^3.0.1",
"prettier-plugin-packagejson": "^2.4.5",
"release-it": "^16.1.3",
"prettier": "^3.0.3",
"prettier-plugin-packagejson": "^2.4.6",
"release-it": "^16.2.1",
"tsup": "^7.2.0",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"peerDependencies": {
"koishi": "^4.14.0"
"@satorijs/satori": "^3.0.0-rc.0",
"koishi": "^4.15.0"
},
"koishi": {
"description": {
Expand Down
106 changes: 57 additions & 49 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
type Fragment,
h,
Quester,
type SendOptions,
type Universal,
} from "koishi";
Universal,
} from "@satorijs/satori";
import type { SendOptions } from "@satorijs/protocol";
import { VillaBotConfig } from "./config";
import {
calcSecretHash,
Expand Down Expand Up @@ -44,7 +44,7 @@ export class VillaBot extends Bot<VillaBotConfig> {
/** emoticons */
protected emoticon: {
list: Emoticon.Emoticon[];
task?: NodeJS.Timer;
task?: ReturnType<typeof setInterval>;
expries?: number;
} = { list: [] };

Expand All @@ -57,6 +57,7 @@ export class VillaBot extends Bot<VillaBotConfig> {
this.id = config.id;
this.secret = config.secret;
this.selfId = config.id;
this.user = { id: config.id, isBot: true };

this.axios = ctx.http;
}
Expand Down Expand Up @@ -101,13 +102,15 @@ export class VillaBot extends Bot<VillaBotConfig> {
return this.getUser(this.selfId);
}

public override sendMessage(
public override async sendMessage(
channelId: string,
content: Fragment,
guildId?: string | undefined,
options?: SendOptions | undefined,
): Promise<string[]> {
return new VillaMessanger(this, channelId, guildId, options).send(content);
return (
await new VillaMessanger(this, channelId, guildId, options).send(content)
).map((msg) => msg.id!);
}

protected async handleCallback(ctx: KoaContext) {
Expand Down Expand Up @@ -139,8 +142,8 @@ export class VillaBot extends Bot<VillaBotConfig> {
return;
}

this.avatar = body.event.robot.template.icon;
this.username = body.event.robot.template.name;
this.user.avatar = body.event.robot.template.icon;
this.user.name = body.event.robot.template.name;
this.description = body.event.robot.template.desc;

const eventData = body.event.extend_data.EventData;
Expand All @@ -149,9 +152,9 @@ export class VillaBot extends Bot<VillaBotConfig> {
const session = super.session({
type: "guild-member-added",
subtype: "group",
guildId: body.event.robot.villa_id.toString(),
guild: { id: body.event.robot.villa_id.toString() },
timestamp: eventData.JoinVilla.join_at,
userId: eventData.JoinVilla.join_uid.toString(),
user: { id: eventData.JoinVilla.join_uid.toString() },
});
logger.info(
`New member of villa ${body.event.robot.villa_id}: ${eventData.JoinVilla.join_uid}`,
Expand All @@ -168,24 +171,31 @@ export class VillaBot extends Bot<VillaBotConfig> {
? (msg.content as Message.TextMsgContent).text
: "";
const session = super.session({
author: {
username: eventData.SendMessage.nickname,
nickname: eventData.SendMessage.nickname,
userId: eventData.SendMessage.from_user_id.toString(),
type: "message",
channel: {
id: `${body.event.robot.villa_id}~${eventData.SendMessage.room_id}`,
type: Universal.Channel.Type.TEXT,
},
guild: { id: body.event.robot.villa_id.toString() },
member: {
name: eventData.SendMessage.nickname,
avatar: msg.user.portraitUri,
},
type: "message",
subtype: "group",
channelId: `${body.event.robot.villa_id}~${eventData.SendMessage.room_id}`,
content,
elements: parseMessage(eventData.SendMessage.object_name, msg, {
emoticonList: await this.getEmoticonList(),
strictEmoticon: this.config.emoticon.strict,
}),
guildId: body.event.robot.villa_id.toString(),
messageId: `${eventData.SendMessage.msg_uid}~${eventData.SendMessage.send_at}`,
message: {
id: `${eventData.SendMessage.msg_uid}~${eventData.SendMessage.send_at}`,
content,
elements: parseMessage(eventData.SendMessage.object_name, msg, {
emoticonList: await this.getEmoticonList(),
strictEmoticon: this.config.emoticon.strict,
}),
},
platform: this.platform,
timestamp: eventData.SendMessage.send_at,
userId: eventData.SendMessage.from_user_id.toString(),
selfId: this.selfId,
user: {
id: eventData.SendMessage.from_user_id.toString(),
avatar: msg.user.portraitUri,
},
});
logger.info(
`Receive message '${content}'(${eventData.SendMessage.msg_uid})`,
Expand All @@ -196,8 +206,7 @@ export class VillaBot extends Bot<VillaBotConfig> {
case Callback.RobotEventType.CreateRobot: {
const session = super.session({
type: "guild-added",
subtype: "group",
guildId: eventData.CreateRobot.villa_id.toString(),
guild: { id: eventData.CreateRobot.villa_id.toString() },
timestamp: new Date().getTime(),
});
logger.info(
Expand All @@ -209,8 +218,7 @@ export class VillaBot extends Bot<VillaBotConfig> {
case Callback.RobotEventType.DeleteRobot: {
const session = super.session({
type: "guild-deleted",
subtype: "group",
guildId: eventData.DeleteRobot.villa_id.toString(),
guild: { id: eventData.DeleteRobot.villa_id.toString() },
timestamp: new Date().getTime(),
});
logger.info(
Expand All @@ -224,30 +232,30 @@ export class VillaBot extends Bot<VillaBotConfig> {
type: `reaction-${
eventData.AddQuickEmoticon.is_cancel ? "deleted" : "added"
}`,
subtype: "group",
channelId: `${eventData.AddQuickEmoticon.villa_id}~${eventData.AddQuickEmoticon.room_id}`,
elements: [
h("face", {
id: eventData.AddQuickEmoticon.emoticon_id,
name: eventData.AddQuickEmoticon.emoticon,
platform: this.platform,
}),
],
guildId: eventData.AddQuickEmoticon.villa_id.toString(),
quote: {
author: {
userId: eventData.AddQuickEmoticon.uid.toString(),
},
channelId: `${eventData.AddQuickEmoticon.villa_id}~${eventData.AddQuickEmoticon.room_id}`,
channel: {
id: `${eventData.AddQuickEmoticon.villa_id}~${eventData.AddQuickEmoticon.room_id}`,
type: Universal.Channel.Type.TEXT,
},
guild: { id: eventData.AddQuickEmoticon.villa_id.toString() },
message: {
elements: [
h("quote", { id: eventData.AddQuickEmoticon.msg_uid.toString() }),
h("face", {
id: eventData.AddQuickEmoticon.emoticon_id,
name: eventData.AddQuickEmoticon.emoticon,
platform: this.platform,
}),
],
guildId: eventData.AddQuickEmoticon.villa_id.toString(),
messageId: `${eventData.AddQuickEmoticon.msg_uid}:0`,
timestamp: new Date().getTime(),
quote: {
elements: [
h("quote", {
id: eventData.AddQuickEmoticon.msg_uid.toString(),
}),
],
messageId: `${eventData.AddQuickEmoticon.msg_uid}:0`,
},
},
timestamp: new Date().getTime(),
userId: eventData.AddQuickEmoticon.uid.toString(),
user: { id: eventData.AddQuickEmoticon.uid.toString() },
});
logger.info(
`Receive reaction '${eventData.AddQuickEmoticon.emoticon}' on message ${eventData.AddQuickEmoticon.msg_uid}`,
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Bot, Schema } from "koishi";
import { Quester, Schema } from "@satorijs/satori";

export interface VillaBotConfig extends Bot.Config {
export interface VillaBotConfig extends Quester.Config {
id: string;
secret: string;
emoticon: {
Expand Down
16 changes: 10 additions & 6 deletions src/messanger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Dict, Element, Messenger, type SendOptions } from "koishi";
import { type Dict, Element, Messenger, Universal } from "@satorijs/satori";
import type { SendOptions } from "@satorijs/protocol";
import { API, Message } from "./structs";
import { isBot, logger } from "./utils";
import type { VillaBot } from "./bot";
Expand Down Expand Up @@ -59,10 +60,13 @@ export class VillaMessanger extends Messenger<VillaBot> {
}

const session = this.bot.session(this.session);
session.messageId = `${res.data.bot_msg_id}:bot_msg`;
session.timestamp = new Date().getTime();
session.userId = this.bot.userId;
this.results.push(session);
const message: Universal.Message = {
id: `${res.data.bot_msg_id}:bot_msg`,
timestamp: new Date().getTime(),
user: this.bot.user,
};
session.event.message = message;
this.results.push(message);
session.app.emit(session, "send", session);
}
} catch (err) {
Expand Down Expand Up @@ -140,7 +144,7 @@ export class VillaMessanger extends Messenger<VillaBot> {
const {
id,
name = `#${
(await this.bot.getChannel(id)).channelName ?? id.split("~")[1]!
(await this.bot.getChannel(id)).name ?? id.split("~")[1]!
}`,
} = element.attrs as Dict<string, "id" | "name" | "guild">;
const [villaId, roomId] = id.split("~") as [string, string];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/callbackRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from "koishi";
import type { Context } from "@satorijs/satori";
import type { KoaContext, KoaMiddleware } from "../types";
import { logger } from "./logger";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/createAxios.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from "koishi";
import type { Context } from "@satorijs/satori";

export const createAxios = (
ctx: Context,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/emoticons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from "koishi";
import type { Context } from "@satorijs/satori";
import { API, type Emoticon } from "../structs";
import { logger } from "./logger";
import type { VillaBot } from "../bot";
Expand Down
7 changes: 4 additions & 3 deletions src/utils/getChannel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Universal } from "koishi";
import { Universal } from "@satorijs/satori";
import type { API } from "../structs";
import type { VillaBot } from "../bot";
import { logger } from "./logger";
Expand Down Expand Up @@ -28,7 +28,8 @@ export async function getChannel(
}

return {
channelId,
channelName: res.data?.room.room_name,
id: channelId,
name: res.data?.room.room_name,
type: Universal.Channel.Type.TEXT,
};
}
11 changes: 6 additions & 5 deletions src/utils/getChannelList.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Universal } from "koishi";
import { Universal } from "@satorijs/satori";
import type { API } from "../structs";
import type { VillaBot } from "../bot";
import { logger } from "./logger";

export async function getChannelList(
this: VillaBot,
guildId: string,
): Promise<Universal.Channel[]> {
): Promise<Universal.List<Universal.Channel>> {
const res = await this.axios.get<API.GetRoomList.Response>(
"/vila/api/bot/platform/getVillaGroupRoomList",
{
Expand All @@ -27,10 +27,11 @@ export async function getChannelList(
res.data.list.forEach((group) =>
group.room_list.forEach((room) =>
channels.push({
channelId: `${guildId}~${room.room_id}`,
channelName: room.room_name,
id: `${guildId}~${room.room_id}`,
name: room.room_name,
type: Universal.Channel.Type.TEXT,
}),
),
);
return channels;
return { data: channels };
}
6 changes: 3 additions & 3 deletions src/utils/getGuild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Universal } from "koishi";
import type { Universal } from "@satorijs/satori";
import type { VillaBot } from "../bot";
import type { API } from "../structs";
import { logger } from "./logger";
Expand All @@ -23,7 +23,7 @@ export async function getGuild(
}

return {
guildId: res.data.villa.villa_id.toString(),
guildName: res.data?.villa.name,
id: res.data.villa.villa_id.toString(),
name: res.data?.villa.name,
};
}
Loading

0 comments on commit 9f378f0

Please sign in to comment.