Skip to content

Commit

Permalink
feat(protocol): introduce features and resourceUrls
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 20, 2024
1 parent a6a8d8b commit 31cb578
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.C
},
})
this.internal = new Internal(this)
this.resourceUrls.push('https://cdn.discordapp.com/')
ctx.plugin(WsClient, this)
}

Expand Down
2 changes: 2 additions & 0 deletions adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const decodeChannel = (data: Discord.Channel): Universal.Channel => ({
: data.type === Discord.Channel.Type.GUILD_VOICE ? Universal.Channel.Type.VOICE
: data.type === Discord.Channel.Type.GUILD_CATEGORY ? Universal.Channel.Type.CATEGORY
: Universal.Channel.Type.TEXT,
parentId: data.parent_id,
position: data.position,
})

export const decodeRole = (role: Discord.Role): Universal.GuildRole => ({
Expand Down
1 change: 1 addition & 0 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
'Authorization': `Bot ${config.token}`,
},
}).extend(config)
this.resourceUrls.push('https://www.kookapp.cn/')
this.internal = new Kook.Internal(this.http)

if (config.protocol === 'http') {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Adapter } from './adapter'
import { MessageEncoder } from './message'
import { defineAccessor, Session } from './session'
import { Event, List, Login, Methods, SendOptions, Status, User } from '@satorijs/protocol'
import { Universal } from '.'

const eventAliases = [
['message-created', 'message'],
Expand All @@ -27,6 +28,8 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
public isBot = true
public hidden = false
public platform: string
public features: string[]
public resourceUrls: string[] = []
public adapter?: Adapter<C, this>
public error?: Error
public callbacks: Dict<Function> = {}
Expand All @@ -48,6 +51,10 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
self.platform = platform
}

this.features = Object.entries(Universal.Methods)
.filter(([, value]) => this[value.name])
.map(([key]) => key)

ctx.on('ready', async () => {
await Promise.resolve()
self.dispatchLoginEvent('login-added')
Expand Down Expand Up @@ -191,7 +198,7 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
}

toJSON(): Login {
return clone(pick(this, ['platform', 'selfId', 'status', 'user', 'hidden']))
return clone(pick(this, ['platform', 'selfId', 'status', 'user', 'hidden', 'features', 'resourceUrls']))
}

async getLogin() {
Expand Down Expand Up @@ -221,6 +228,8 @@ for (const name of iterableMethods) {
if (!this[name + 'List']) throw new Error(`not implemented: ${name}List`)
const getList = async () => {
list = await this[name + 'List'](...args, list?.next)
// `bot.getMessageList()` returns messages in ascending order
if (name === 'getMessage') list.data.reverse()
}
return {
async next() {
Expand Down
3 changes: 3 additions & 0 deletions packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface Channel {
type: Channel.Type
name?: string
parentId?: string
position?: number
}

export namespace Channel {
Expand Down Expand Up @@ -210,6 +211,8 @@ export interface Login {
selfId?: string
hidden?: boolean
status: Status
features?: string[]
resourceUrls?: string[]
}

export const enum Status {
Expand Down

0 comments on commit 31cb578

Please sign in to comment.