Skip to content

Commit

Permalink
feat(server): initial support for http server
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 10, 2023
1 parent 505121e commit ef9db03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
}

async supports(name: string, session: Partial<Session> = {}) {
return !!this[Universal.Methods[name]]
return !!this[Universal.Methods[name]?.name]
}

async checkPermission(name: string, session: Partial<Session>) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class Session {

constructor(bot: Bot, payload: Partial<Session.Payload> = {}) {
this.data = {}
Object.assign(this, payload)
for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(payload))) {
if (descriptor.enumerable) continue
Object.defineProperty(this, key, descriptor)
}
this.selfId = bot.selfId
Expand Down
56 changes: 33 additions & 23 deletions packages/core/src/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ import { SendOptions } from './session'
import { Dict } from 'cosmokit'

export namespace Universal {
export const Methods = {
'message.get': 'getMessage',
'message.list': 'getMessageList',
'message.update': 'editMessage',
'message.delete': 'deleteMessage',
'reaction.create': 'createReaction',
'reaction.delete': 'deleteReaction',
'reaction.clear': 'clearReaction',
'reaction.list': 'getReactions',
'guild.get': 'getGuild',
'guild.list': 'getGuildList',
'guild.member.get': 'getGuildMember',
'guild.member.list': 'getGuildMemberList',
'guild.member.kick': 'kickGuildMember',
'guild.member.mute': 'muteGuildMember',
'guild.member.role': 'setGuildMemberRole',
'guild.role.list': 'getGuildRoles',
'guild.role.create': 'createGuildRole',
'guild.role.update': 'modifyGuildRole',
'guild.role.delete': 'deleteGuildRole',
'channel.get': 'getChannel',
'channel.list': 'getChannelList',
'channel.mute': 'muteChannel',
export interface Method {
name: string
fields: string[]
}

function Method(name: string, fields: string[]): Method {
return { name, fields }
}

export const Methods: Dict<Method> = {
'message.send': Method('sendMessage', ['channelId', 'content', 'guildId', 'options']),
'message.get': Method('getMessage', ['channelId', 'messageId']),
'message.list': Method('getMessageList', ['channelId', 'next']),
'message.update': Method('editMessage', ['channelId', 'messageId']),
'message.delete': Method('deleteMessage', ['channelId', 'messageId']),
'reaction.create': Method('createReaction', ['channelId', 'messageId', 'emoji']),
'reaction.delete': Method('deleteReaction', ['channelId', 'messageId', 'emoji', 'userId']),
'reaction.clear': Method('clearReaction', ['channelId', 'messageId', 'emoji']),
'reaction.list': Method('getReactionList', ['channelId', 'messageId', 'emoji', 'next']),
'guild.get': Method('getGuild', ['guildId']),
'guild.list': Method('getGuildList', ['next']),
'guild.member.get': Method('getGuildMember', ['guildId', 'userId']),
'guild.member.list': Method('getGuildMemberList', ['guildId', 'next']),
'guild.member.kick': Method('kickGuildMember', ['guildId', 'userId', 'permanent']),
'guild.member.mute': Method('muteGuildMember', ['guildId', 'userId', 'duration', 'reason']),
'guild.member.role': Method('setGuildMemberRole', ['guildId', 'userId', 'roleId']),
'guild.role.list': Method('getGuildRoleList', ['guildId', 'next']),
'guild.role.create': Method('createGuildRole', ['guildId', 'data']),
'guild.role.update': Method('modifyGuildRole', ['guildId', 'roleId', 'data']),
'guild.role.delete': Method('deleteGuildRole', ['guildId', 'roleId']),
'channel.get': Method('getChannel', ['channelId', 'guildId']),
'channel.list': Method('getChannelList', ['guildId', 'next']),
'channel.mute': Method('muteChannel', ['channelId', 'guildId', 'enable']),
}

export interface List<T> {
Expand Down
33 changes: 33 additions & 0 deletions packages/server-basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Context, Schema, Universal } from '@satorijs/satori'

export interface Config {
path: string
}

export const Config: Schema<Config> = Schema.object({
path: Schema.string().default('/basic'),
})

export function apply(ctx: Context, config: Config) {
ctx.router.all(config + '/:name', async (koa) => {
const method = Universal.Methods[koa.params.name]
if (!method) {
koa.body = 'method not found'
return koa.status = 404
}

const json = koa.method === 'GET' ? koa.query : koa.request.body
const selfId = json.self_id
const platform = json.platform
const bot = ctx.bots.find(bot => bot.selfId === selfId && bot.platform === platform)
if (!bot) {
koa.body = 'bot not found'
return koa.status = 403
}

const args = method.fields.map(field => json[field])
const result = await bot[method.name](...args)
koa.body = result
koa.status = 200
})
}

0 comments on commit ef9db03

Please sign in to comment.