Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(discord): Optimize segment.image sending #282

Merged
merged 9 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,59 @@ export class DiscordBot extends Bot<'discord'> {
})
sentMessageId = r.id
} else {
try {
const { axiosConfig, discord = {} } = this.app.options
type SendMode = 'auto' | 'download' | 'direct'
dragon-fish marked this conversation as resolved.
Show resolved Hide resolved
const sendMode =
data.mode as SendMode || // define in segment
shigma marked this conversation as resolved.
Show resolved Hide resolved
discord.handleExternalAssets as SendMode || // define in app options
shigma marked this conversation as resolved.
Show resolved Hide resolved
'auto' // default

// Utils
async function downloadSend() {
dragon-fish marked this conversation as resolved.
Show resolved Hide resolved
const a = await axios.get(data.url, {
...axiosConfig,
...discord.axiosConfig,
responseType: 'arraybuffer',
})
const r = await this.sendEmbedMessage(requestUrl, a.data, {
const r = await that.sendEmbedMessage(requestUrl, a.data, {
...addition,
})
sentMessageId = r.id
} catch (e) {
throw new SenderError(data.url, data, this.selfId)
}
async function directSend() {
const r = await that.request('POST', requestUrl, {
content: data.url,
...addition,
})
sentMessageId = r.id
}

if (sendMode === 'direct') {
// send url directly
await directSend()
} else if (sendMode === 'download') {
// download send
await downloadSend()
} else {
// auto mode
axios.head(data.url, {
...axiosConfig,
...discord.axiosConfig,
})
.then(async ({ headers }) => {
if (headers?.['content-type']?.includes('image')) {
await directSend()
} else {
await downloadSend()
}
})
.catch(async () => {
dragon-fish marked this conversation as resolved.
Show resolved Hide resolved
try {
await downloadSend()
} catch (e) {
throw new SenderError(data.url, data, this.selfId)
}
})
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './bot'
interface DiscordOptions extends Adapter.WsClientOptions {
endpoint?: string
axiosConfig?: AxiosRequestConfig
handleExternalAssets?: 'auto' | 'download' | 'direct'
}

declare module 'koishi-core' {
Expand Down