Skip to content

Commit

Permalink
fix(discord): remove content while sending embed, update segments
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Mar 4, 2021
1 parent f490732 commit 7c9706f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class DiscordBot extends Bot<'discord'> {
let needSend = ''
const isWebhook = requestUrl.startsWith('/webhooks/')
const that = this
delete addition.content
async function sendMessage() {
const r = await that.request('POST', requestUrl, {
content: needSend,
Expand Down Expand Up @@ -110,12 +111,13 @@ export class DiscordBot extends Bot<'discord'> {
if (type === 'image' || type === 'video') {
if (data.url.startsWith('http://') || data.url.startsWith('https://')) {
const sendData = isWebhook ? {
embeds: [{ ...addition, ...data }],
embeds: [{ [type]: data }],
} : {
embed: { ...addition, ...data },
embed: { [type]: data },
}
const r = await this.request('POST', requestUrl, {
...sendData,
...addition
})
sentMessageId = r.id
} else {
Expand Down Expand Up @@ -210,7 +212,7 @@ export class DiscordBot extends Bot<'discord'> {
throw new Error('Up to 10 embed objects')
}

const messageId = await this.sendFullMessage(`/webhooks/${id}/${token}`, data.content)
const messageId = await this.sendFullMessage(`/webhooks/${id}/${token}`, data.content, data)
return messageId
}
}
1 change: 1 addition & 0 deletions packages/adapter-discord/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export interface Attachment {
proxy_url: string
height?: number
width?: number
content_type?: string
}

export interface DiscordUser {
Expand Down
24 changes: 20 additions & 4 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,26 @@ export function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session:

// embed 的 update event 太阴间了 只有 id embeds channel_id guild_id 四个成员
if (meta.attachments?.length) {
session.content += meta.attachments.map(v => segment('image', {
url: v.url,
proxy_url: v.proxy_url
})).join('')
session.content += meta.attachments.map(v => {
if (v.height && v.width && v.content_type?.startsWith('image/')) {
return segment('image', {
url: v.url,
proxy_url: v.proxy_url,
file: v.filename
})
} else if (v.height && v.width && v.content_type?.startsWith('video/')) {
return segment('video', {
url: v.url,
proxy_url: v.proxy_url,
file: v.filename
})
} else {
return segment('file', {
url: v.url,
file: v.filename
})
}
}).join('')
}
for (const embed of meta.embeds) {
switch (embed.type) {
Expand Down

0 comments on commit 7c9706f

Please sign in to comment.