diff --git a/adapters/discord/src/bot.ts b/adapters/discord/src/bot.ts index 7c4a63f3..cb194d12 100644 --- a/adapters/discord/src/bot.ts +++ b/adapters/discord/src/bot.ts @@ -92,10 +92,8 @@ export class DiscordBot extends Bot { } async getMessageList(channelId: string, before?: string) { - // doesn't include `before` message - // 从旧到新 - const data = (await this.internal.getChannelMessages(channelId, { before, limit: 50 })).reverse() - return await Promise.all(data.map(data => decodeMessage(this, data))) + const data = await this.internal.getChannelMessages(channelId, { before, limit: 100 }) + return { data: await Promise.all(data.reverse().map(data => decodeMessage(this, data, {}, false))) } } async getUser(userId: string) { @@ -105,7 +103,7 @@ export class DiscordBot extends Bot { async getGuildMemberList(guildId: string) { const data = await this.internal.listGuildMembers(guildId) - return data.map(v => decodeUser(v.user)) + return { data: data.map(v => decodeUser(v.user)) } } async getChannel(channelId: string) { diff --git a/adapters/discord/src/utils.ts b/adapters/discord/src/utils.ts index 48da990e..1b6ac52a 100644 --- a/adapters/discord/src/utils.ts +++ b/adapters/discord/src/utils.ts @@ -43,7 +43,7 @@ export const encodeRole = (role: Partial): Partial permissions: role.permissions && '' + role.permissions, }) -export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, session: Partial = {}) { +export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, session: Partial = {}, reference = true) { const { platform } = bot session.messageId = meta.id @@ -129,7 +129,7 @@ export async function decodeMessage(bot: DiscordBot, meta: Discord.Message, sess } session.elements = h.parse(session.content) // 遇到过 cross post 的消息在这里不会传消息 id - if (meta.message_reference) { + if (reference && meta.message_reference) { const { message_id, channel_id } = meta.message_reference session.quote = await bot.getMessage(channel_id, message_id) } diff --git a/adapters/kook/src/bot.ts b/adapters/kook/src/bot.ts index 3b3ac903..a87a9b0e 100644 --- a/adapters/kook/src/bot.ts +++ b/adapters/kook/src/bot.ts @@ -94,7 +94,7 @@ export class KookBot extends Bot { async getGuildMemberList(guild_id: string) { const { items } = await this.request('GET', '/guild/user-list', { guild_id }) - return items.map(adaptAuthor) + return { data: items.map(adaptAuthor) } } async setGroupNickname(guild_id: string, user_id: string, nickname: string) { diff --git a/adapters/line/src/bot.ts b/adapters/line/src/bot.ts index 1e101f28..e8134ea5 100644 --- a/adapters/line/src/bot.ts +++ b/adapters/line/src/bot.ts @@ -1,4 +1,4 @@ -import { Bot, Context, Logger, Quester, Schema, Universal } from '@satorijs/satori' +import { Bot, Context, Logger, Quester, Schema } from '@satorijs/satori' import { HttpServer } from './http' import { Internal } from './types' import { LineMessageEncoder } from './message' @@ -43,7 +43,7 @@ export class LineBot extends Bot { } // https://developers.line.biz/en/reference/messaging-api/#get-profile - async getSelf(): Promise { + async getSelf() { const { userId, displayName, pictureUrl } = await this.internal.getBotInfo() return { userId, @@ -52,7 +52,7 @@ export class LineBot extends Bot { } } - async getFriendList(): Promise { + async getFriendList() { let userIds: string[] = [] let start: string do { @@ -67,7 +67,7 @@ export class LineBot extends Bot { return userIds.map(v => ({ userId: v })) } - async getGuild(guildId: string): Promise { + async getGuild(guildId: string) { const res = await this.internal.getGroupSummary(guildId) return { guildId: res.groupId, @@ -75,7 +75,7 @@ export class LineBot extends Bot { } } - async getGuildMemberList(guildId: string): Promise { + async getGuildMemberList(guildId: string) { let userIds: string[] = [] let start: string do { @@ -84,10 +84,10 @@ export class LineBot extends Bot { start = res.next } while (start) - return userIds.map(v => ({ userId: v })) + return { data: userIds.map(v => ({ userId: v })) } } - async getGuildMember(guildId: string, userId: string): Promise { + async getGuildMember(guildId: string, userId: string) { const res = await this.internal.getGroupMemberProfile(guildId, userId) return ({ userId: res.userId, diff --git a/adapters/matrix/src/bot.ts b/adapters/matrix/src/bot.ts index f5ff7ac5..7f832910 100644 --- a/adapters/matrix/src/bot.ts +++ b/adapters/matrix/src/bot.ts @@ -1,4 +1,4 @@ -import { Bot, Context, omit, Quester, Schema, Universal } from '@satorijs/satori' +import { Bot, Context, omit, Quester, Schema } from '@satorijs/satori' import { HttpAdapter } from './http' import { MatrixMessageEncoder } from './message' import * as Matrix from './types' @@ -57,7 +57,7 @@ export class MatrixBot extends Bot { })) } - async getMessage(channelId: string, messageId: string): Promise { + async getMessage(channelId: string, messageId: string) { const event = await this.internal.getEvent(channelId, messageId) return await adaptMessage(this, event) } @@ -70,7 +70,7 @@ export class MatrixBot extends Bot { return await this.getUser(this.userId) } - async getUser(userId: string): Promise { + async getUser(userId: string) { const profile = await this.internal.getProfile(userId) let avatar: string if (profile.avatar_url) avatar = this.internal.getAssetUrl(profile.avatar_url) @@ -82,35 +82,35 @@ export class MatrixBot extends Bot { } } - async getFriendList(): Promise { + async getFriendList() { return [] } - async deleteFriend(): Promise { } + async deleteFriend() { } - async getGuild(guildId: string): Promise { + async getGuild(guildId: string) { const { channelName } = await this.getChannel(guildId) return { guildId, guildName: channelName } } - async getChannel(channelId: string): Promise { + async getChannel(channelId: string) { const events = await this.internal.getState(channelId) const channelName = (events.find(event => event.type === 'm.room.name')?.content as Matrix.M_ROOM_NAME)?.name return { channelId, channelName } } - async getGuildList(): Promise { + async getGuildList() { return await Promise.all(this.rooms.map(roomId => this.getGuild(roomId))) } - async getChannelList(guildId: string): Promise { + async getChannelList(guildId: string) { return await Promise.all(this.rooms.map(roomId => this.getChannel(roomId))) } - async getGuildMemberList(guildId: string): Promise { + async getGuildMemberList(guildId: string) { const state = await this.internal.getState(guildId) const levels = state.find(event => event.type === 'm.room.power_levels').content as Matrix.M_ROOM_POWER_LEVELS - return state + const data = state .filter(event => event.type === 'm.room.member') .map(event => { const content = event.content as Matrix.M_ROOM_MEMBER @@ -123,17 +123,18 @@ export class MatrixBot extends Bot { roles: [levels.users[event.state_key].toString()], } }) + return { data } } - async getGuildMember(guildId: string, userId: string): Promise { - return (await this.getGuildMemberList(guildId)).find(user => user.userId === userId) + async getGuildMember(guildId: string, userId: string) { + return (await this.getGuildMemberList(guildId)).data.find(user => user.userId === userId) } - async createReaction(channelId: string, messageId: string, emoji: string): Promise { + async createReaction(channelId: string, messageId: string, emoji: string) { await this.internal.sendReaction(channelId, messageId, emoji) } - async handleFriendRequest(): Promise { } + async handleFriendRequest() { } // as utils.ts commented, messageId is roomId async handleGuildRequest(messageId: string, approve: boolean, commit: string) { diff --git a/adapters/onebot/src/bot/base.ts b/adapters/onebot/src/bot/base.ts index b72c0f74..43ab5e53 100644 --- a/adapters/onebot/src/bot/base.ts +++ b/adapters/onebot/src/bot/base.ts @@ -70,7 +70,7 @@ export class BaseBot extends Bot { } // 从旧到新 - return await Promise.all(list.map(item => OneBot.adaptMessage(this, item))) + return { data: await Promise.all(list.map(item => OneBot.adaptMessage(this, item))) } } } diff --git a/adapters/onebot/src/bot/index.ts b/adapters/onebot/src/bot/index.ts index 80608d55..8c44a4f0 100644 --- a/adapters/onebot/src/bot/index.ts +++ b/adapters/onebot/src/bot/index.ts @@ -81,7 +81,7 @@ export class OneBotBot extends Ba async getGuildMemberList(guildId: string) { const data = await this.internal.getGroupMemberList(guildId) - return data.map(OneBot.adaptGuildMember) + return { data: data.map(OneBot.adaptGuildMember) } } async kickGuildMember(guildId: string, userId: string, permanent?: boolean) { diff --git a/adapters/onebot/src/bot/qqguild.ts b/adapters/onebot/src/bot/qqguild.ts index 5249d624..2cadc301 100644 --- a/adapters/onebot/src/bot/qqguild.ts +++ b/adapters/onebot/src/bot/qqguild.ts @@ -79,6 +79,6 @@ export class QQGuildBot extends BaseBot { if (data.finished) break nextToken = data.next_token } - return list + return { data: list } } } diff --git a/adapters/slack/src/bot.ts b/adapters/slack/src/bot.ts index cfc13af5..b613a095 100644 --- a/adapters/slack/src/bot.ts +++ b/adapters/slack/src/bot.ts @@ -1,4 +1,4 @@ -import { Bot, Context, Fragment, Quester, Schema, SendOptions, Universal } from '@satorijs/satori' +import { Bot, Context, Fragment, Quester, Schema, SendOptions } from '@satorijs/satori' import { WsClient } from './ws' import { HttpServer } from './http' import { adaptChannel, adaptGuild, adaptMessage, adaptUser } from './utils' @@ -49,14 +49,14 @@ export class SlackBot extends Bot { + async deleteMessage(channelId: string, messageId: string) { await this.internal.chatDelete(Token.BOT, { channel: channelId, ts: Number(messageId), }) } - async getMessage(channelId: string, messageId: string): Promise { + async getMessage(channelId: string, messageId: string) { const msg = await this.internal.conversationsHistory(Token.BOT, { channel: channelId, oldest: Number(messageId), @@ -67,17 +67,17 @@ export class SlackBot extends Bot { + async getMessageList(channelId: string, before?: string) { const msg = await this.request<{ messages: GenericMessageEvent[] }>('POST', '/conversations.history', { channel: channelId, latest: before, }) - return Promise.all(msg.messages.map(v => adaptMessage(this, v))) + return { data: await Promise.all(msg.messages.map(v => adaptMessage(this, v))) } } - async getUser(userId: string, guildId?: string): Promise { + async getUser(userId: string, guildId?: string) { // users:read // @TODO guildId const { user } = await this.request<{ user: SlackUser }>('POST', '/users.info', { @@ -86,13 +86,13 @@ export class SlackBot extends Bot { + async getGuildMemberList(guildId: string) { // users:read const { members } = await this.request<{ members: SlackUser[] }>('POST', '/users.list') - return members.map(adaptUser) + return { data: members.map(adaptUser) } } - async getChannel(channelId: string, guildId?: string): Promise { + async getChannel(channelId: string, guildId?: string) { const { channel } = await this.request<{ channel: SlackChannel }>('POST', '/conversations.info', { @@ -101,7 +101,7 @@ export class SlackBot extends Bot { + async getChannelList(guildId: string) { const { channels } = await this.request<{ channels: SlackChannel[] }>('POST', '/conversations.list', { @@ -110,14 +110,14 @@ export class SlackBot extends Bot { + async getGuild(guildId: string) { const { team } = await this.request<{ team: SlackTeam }>('POST', '/team.info', { team_id: guildId, }) return adaptGuild(team) } - async getGuildMember(guildId: string, userId: string): Promise { + async getGuildMember(guildId: string, userId: string) { const { user } = await this.request<{ user: SlackUser }>('POST', '/users.info', { user: userId, }) @@ -127,7 +127,7 @@ export class SlackBot extends Bot { + async sendPrivateMessage(channelId: string, content: Fragment, options?: SendOptions) { // "channels:write,groups:write,mpim:write,im:write", const { channel } = await this.internal.conversationsOpen(Token.BOT, { users: channelId, @@ -136,7 +136,7 @@ export class SlackBot extends Bot { + async getReactions(channelId: string, messageId: string, emoji: string) { const { message } = await this.internal.reactionsGet(Token.BOT, { channel: channelId, timestamp: messageId, @@ -147,7 +147,7 @@ export class SlackBot extends Bot { + async createReaction(channelId: string, messageId: string, emoji: string) { // reactions.write await this.internal.reactionsAdd(Token.BOT, { channel: channelId, @@ -156,7 +156,7 @@ export class SlackBot extends Bot { + async clearReaction(channelId: string, messageId: string, emoji?: string) { const { message } = await this.internal.reactionsGet(Token.BOT, { channel: channelId, timestamp: messageId, diff --git a/adapters/telegram/src/bot.ts b/adapters/telegram/src/bot.ts index dfcabbe2..82202540 100644 --- a/adapters/telegram/src/bot.ts +++ b/adapters/telegram/src/bot.ts @@ -217,7 +217,7 @@ export class TelegramBot exte const data = await this.internal.getChatAdministrators({ chat_id }) const users = data.map(adaptGuildMember) await Promise.all(users.map(this.setAvatarUrl.bind(this))) - return users + return { data: users } } async kickGuildMember(chat_id: string, user_id: string | number, permanent?: boolean) { diff --git a/packages/core/src/universal.ts b/packages/core/src/universal.ts index c894d959..f50b76c4 100644 --- a/packages/core/src/universal.ts +++ b/packages/core/src/universal.ts @@ -28,6 +28,10 @@ export namespace Universal { 'channel.mute': 'muteChannel', } + export interface List { + data: T[] + } + export interface Methods { // message sendMessage(channelId: string, content: Element.Fragment, guildId?: string, options?: SendOptions): Promise @@ -35,7 +39,7 @@ export namespace Universal { sendPrivateMessage(userId: string, content: Element.Fragment, options?: SendOptions): Promise // sendPrivateMessage(session: Session.Payload, content: segment.Fragment, options?: SendOptions): Promise getMessage(channelId: string, messageId: string): Promise - getMessageList(channelId: string, before?: string): Promise + getMessageList(channelId: string, before?: string, limit?: number): Promise> editMessage(channelId: string, messageId: string, content: Element.Fragment): Promise deleteMessage(channelId: string, messageId: string): Promise @@ -57,7 +61,7 @@ export namespace Universal { // guild member getGuildMember(guildId: string, userId: string): Promise - getGuildMemberList(guildId: string): Promise + getGuildMemberList(guildId: string): Promise> kickGuildMember(guildId: string, userId: string, permanent?: boolean): Promise muteGuildMember(guildId: string, userId: string, duration: number, reason?: string): Promise