Skip to content

Commit

Permalink
feat(discord): change image url
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Mar 3, 2021
1 parent 3ec10bd commit e012bc9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
1 change: 0 additions & 1 deletion packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import WsClient from './ws'

interface DiscordOptions extends Adapter.WsClientOptions {
axiosConfig?: AxiosRequestConfig
preferImageSource?: boolean
}

declare module 'koishi-core' {
Expand Down
12 changes: 4 additions & 8 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session:
session.author = adaptAuthor(meta.author)
session.userId = meta.author.id
}
const urlKey = bot.app.options.discord.preferImageSource ? 'url' : 'proxy_url'
// https://discord.com/developers/docs/reference#message-formatting
session.content = ''
if (meta.content) {
Expand All @@ -51,20 +50,17 @@ 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[urlKey],
url: v.url,
proxy_url: v.proxy_url
})).join('')
}
for (const embed of meta.embeds) {
switch (embed.type) {
case 'video':
session.content += segment('video', { url: embed[urlKey] })
session.content += segment('video', { url: embed.url, proxy_url: embed.video.proxy_url })
break
case 'image':
if (embed.thumbnail?.proxy_url && bot.app.options.discord.preferImageSource) {
session.content += segment('image', { url: embed.thumbnail[urlKey] ?? embed.thumbnail.url ?? embed.url })
} else {
session.content += segment('image', { url: embed.thumbnail.proxy_url })
}
session.content += segment('image', { url: embed.thumbnail.url, proxy_url: embed.thumbnail.proxy_url })
break
case 'gifv':
session.content += segment('video', { url: embed.video.url })
Expand Down

0 comments on commit e012bc9

Please sign in to comment.