Skip to content

Commit

Permalink
feat(core): support reaction api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 25, 2023
1 parent 48f19d3 commit 9984dfa
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
29 changes: 27 additions & 2 deletions adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,37 @@ export class DiscordBot extends Bot<DiscordBot.Config> {

async getGuildList() {
const data = await this.internal.getCurrentUserGuilds()
return data.map(v => adaptGuild(v))
return data.map(adaptGuild)
}

async getChannelList(guildId: string) {
const data = await this.internal.getGuildChannels(guildId)
return data.map(v => adaptChannel(v))
return data.map(adaptChannel)
}

createReaction(channelId: string, messageId: string, emoji: string) {
return this.internal.createReaction(channelId, messageId, emoji)
}

deleteReaction(channelId: string, messageId: string, emoji: string, userId?: string) {
if (!userId) {
return this.internal.deleteOwnReaction(channelId, messageId, emoji)
} else {
return this.internal.deleteUserReaction(channelId, messageId, emoji, userId)
}
}

clearReaction(channelId: string, messageId: string, emoji?: string) {
if (!emoji) {
return this.internal.deleteAllReactions(channelId, messageId)
} else {
return this.internal.deleteAllReactionsForEmoji(channelId, messageId, emoji)
}
}

async getReactions(channelId: string, messageId: string, emoji: string) {
const data = await this.internal.getReactions(channelId, messageId, emoji)
return data.map(adaptUser)
}
}

Expand Down
4 changes: 2 additions & 2 deletions adapters/discord/src/types/reaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Emoji, GuildMember, integer, Internal, snowflake } from '.'
import { Emoji, GuildMember, integer, Internal, snowflake, User } from '.'

/** https://discord.com/developers/docs/resources/channel#reaction-object-reaction-structure */
export interface Reaction {
Expand Down Expand Up @@ -107,7 +107,7 @@ declare module './internal' {
* Get a list of users that reacted with this emoji. Returns an array of user objects on success. The emoji must be URL Encoded or the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the format name:id with the emoji name and emoji id.
* @see https://discord.com/developers/docs/resources/channel#get-reactions
*/
getReactions(channel_id: snowflake, message_id: snowflake, emoji: string, params?: Reaction.GetParams): Promise<Reaction[]>
getReactions(channel_id: snowflake, message_id: snowflake, emoji: string, params?: Reaction.GetParams): Promise<User[]>
/**
* Deletes all reactions on a message. This endpoint requires the 'MANAGE_MESSAGES' permission to be present on the current user. Fires a Message Reaction Remove All Gateway event.
* @see https://discord.com/developers/docs/resources/channel#delete-all-reactions
Expand Down
30 changes: 28 additions & 2 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Bot, Context, Fragment, h, Quester, Schema } from '@satorijs/satori'
import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori'
import { Method } from 'axios'
import { adaptAuthor, adaptGroup, adaptMessage, adaptUser } from './utils'
import * as Kook from './types'
import FormData from 'form-data'
import { WsClient } from './ws'
import { HttpServer } from './http'
import { KookMessageEncoder } from './message'
import { isDirectChannel, KookMessageEncoder } from './message'

export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
http: Quester
Expand Down Expand Up @@ -106,6 +106,32 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
async kickGroup(guild_id: string, user_id: string) {
await this.request('POST', '/guild/kickout', { guild_id, user_id })
}

createReaction(channelId: string, messageId: string, emoji: string): Promise<void> {
if (isDirectChannel(channelId)) {
return this.internal.addDirectMessageReaction({ msg_id: messageId, emoji })
} else {
return this.internal.addMessageReaction({ msg_id: messageId, emoji })
}
}

deleteReaction(channelId: string, messageId: string, emoji: string, userId?: string): Promise<void> {
if (isDirectChannel(channelId)) {
return this.internal.deleteDirectMessageReaction({ msg_id: messageId, emoji, user_id: userId })
} else {
return this.internal.deleteMessageReaction({ msg_id: messageId, emoji, user_id: userId })
}
}

async getReactions(channelId: string, messageId: string, emoji: string): Promise<Universal.User[]> {
let users: Kook.User[]
if (isDirectChannel(channelId)) {
users = await this.internal.getDirectMessageReactionList({ msg_id: messageId, emoji })
} else {
users = await this.internal.getMessageReactionList({ msg_id: messageId, emoji })
}
return users.map(adaptUser)
}
}

export namespace KookBot {
Expand Down
6 changes: 5 additions & 1 deletion adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import internal from 'stream'

const attachmentTypes = ['image', 'video', 'audio', 'file']

export function isDirectChannel(channelId: string) {
return channelId.length > 30
}

export class KookMessageEncoder extends MessageEncoder<KookBot> {
private path: string
private params = {} as Partial<Kook.MessageParams>
Expand All @@ -18,7 +22,7 @@ export class KookMessageEncoder extends MessageEncoder<KookBot> {
const { code } = await this.bot.request('POST', '/user-chat/create', { target_id: this.session.channelId })
this.session.channelId = code
}
if (this.session.channelId.length > 30) {
if (isDirectChannel(this.session.channelId)) {
this.params.chat_code = this.session.channelId
this.path = '/user-chat/create-msg'
} else {
Expand Down
4 changes: 2 additions & 2 deletions adapters/onebot/src/bot/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bot, Fragment, Schema, SendOptions } from '@satorijs/satori'
import * as OneBot from '../utils'
import { OneBotMessenger } from './message'
import { OneBotMessageEncoder } from './message'

export class BaseBot<T extends BaseBot.Config = BaseBot.Config> extends Bot<T> {
public parent?: BaseBot
Expand All @@ -10,7 +10,7 @@ export class BaseBot<T extends BaseBot.Config = BaseBot.Config> extends Bot<T> {
if (!this.parent && !channelId.startsWith('private:')) {
guildId = channelId
}
return new OneBotMessenger(this, channelId, guildId, options).send(fragment)
return new OneBotMessageEncoder(this, channelId, guildId, options).send(fragment)
}

sendPrivateMessage(userId: string, fragment: Fragment, options?: SendOptions) {
Expand Down
4 changes: 2 additions & 2 deletions adapters/onebot/src/bot/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Messenger, pick } from '@satorijs/satori'
import { h, MessageEncoder, pick } from '@satorijs/satori'
import { BaseBot } from './base'
import { CQCode } from './cqcode'
import { Author } from '../types'
Expand All @@ -10,7 +10,7 @@ class State {
constructor(public type: 'message' | 'forward' | 'reply') {}
}

export class OneBotMessenger extends Messenger<BaseBot> {
export class OneBotMessageEncoder extends MessageEncoder<BaseBot> {
stack: State[] = [new State('message')]
children: CQCode[] = []

Expand Down

0 comments on commit 9984dfa

Please sign in to comment.