Skip to content

Commit

Permalink
feat(discord): support reaction api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 4, 2021
1 parent 21d06f2 commit f0d9a61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 14 additions & 1 deletion packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class DiscordBot extends Bot<'discord'> {
result.author.nickname = msg.member?.nick
if (msg.message_reference) {
const quoteMsg = await this.$getMessage(msg.message_reference.channel_id, msg.message_reference.message_id)
result.quote = await adaptMessage(this, quoteMsg)
result.quote = adaptMessage(this, quoteMsg)
}
return result
}
Expand All @@ -260,6 +260,19 @@ export class DiscordBot extends Bot<'discord'> {
return adaptChannel(data)
}

async $createReaction(channelId: string, messageId: string, emoji: string) {
await this.request('PUT', `/channels/${channelId}/messages/${messageId}/reactions/${emoji}/@me`)
}

async $deleteReaction(channelId: string, messageId: string, emoji: string, userId = '@me') {
await this.request('DELETE', `/channels/${channelId}/messages/${messageId}/reactions/${emoji}/${userId}`)
}

async $deleteAllReactions(channelId: string, messageId: string, emoji?: string) {
const path = emoji ? '/' + emoji : ''
await this.request('DELETE', `/channels/${channelId}/messages/${messageId}/reactions${path}`)
}

async $executeWebhook(id: string, token: string, data: DC.ExecuteWebhookBody, wait = false): Promise<string> {
const chain = segment.parse(data.content)
if (chain.filter(v => v.type === 'image').length > 10) {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import { AuthorInfo, ChannelInfo, GroupInfo, segment, Session, UserInfo } from 'koishi-core'
import { AuthorInfo, ChannelInfo, GroupInfo, MessageInfo, segment, Session, UserInfo } from 'koishi-core'
import { DiscordBot } from './bot'
import * as DC from './types'

Expand Down Expand Up @@ -103,7 +103,7 @@ export function adaptMessage(bot: DiscordBot, meta: DC.Message, session: Partial
session.content += segment('video', { url: embed.video.url, proxy_url: embed.video.proxy_url })
}
}
return session
return session as MessageInfo
}

function adaptMessageSession(bot: DiscordBot, meta: DC.Message, session: Partial<Session> = {}) {
Expand Down
16 changes: 16 additions & 0 deletions packages/adapter-kaiheila/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ export class KaiheilaBot extends Bot {
}
}

async $createReaction(channelId: string, messageId: string, emoji: string) {
if (channelId.length > 30) {
await this.request('POST', '/direct-message/add-reaction', { msg_id: messageId, emoji })
} else {
await this.request('POST', '/message/add-reaction', { msg_id: messageId, emoji })
}
}

async $deleteReaction(channelId: string, messageId: string, emoji: string, userId?: string) {
if (channelId.length > 30) {
await this.request('POST', '/direct-message/delete-reaction', { msg_id: messageId, emoji })
} else {
await this.request('POST', '/message/delete-reaction', { msg_id: messageId, emoji, user_id: userId })
}
}

async getSelf() {
const data = adaptUser(await this.request<KHL.Self>('GET', '/user/me'))
renameProperty(data, 'selfId' as never, 'userId')
Expand Down

0 comments on commit f0d9a61

Please sign in to comment.