fix: properly handle error in discord provider
This commit is contained in:
parent
7236213ea4
commit
d6bc6eb0ff
1 changed files with 53 additions and 13 deletions
|
|
@ -42,7 +42,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
).json();
|
||||
|
||||
const { application } = await (
|
||||
await fetch('https://discord.com/api/oauth2/@me', {
|
||||
await this.fetch('https://discord.com/api/oauth2/@me', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${access_token}`,
|
||||
},
|
||||
|
|
@ -99,7 +99,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
this.checkScopes(this.scopes, scope.split(' '));
|
||||
|
||||
const { application } = await (
|
||||
await fetch('https://discord.com/api/oauth2/@me', {
|
||||
await this.fetch('https://discord.com/api/oauth2/@me', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${access_token}`,
|
||||
},
|
||||
|
|
@ -120,7 +120,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
@Tool({ description: 'Channels', dataSchema: [] })
|
||||
async channels(accessToken: string, params: any, id: string) {
|
||||
const list = await (
|
||||
await fetch(`https://discord.com/api/guilds/${id}/channels`, {
|
||||
await this.fetch(`https://discord.com/api/guilds/${id}/channels`, {
|
||||
headers: {
|
||||
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`,
|
||||
},
|
||||
|
|
@ -171,7 +171,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
}
|
||||
|
||||
const data = await (
|
||||
await fetch(`https://discord.com/api/channels/${channel}/messages`, {
|
||||
await this.fetch(`https://discord.com/api/channels/${channel}/messages`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`,
|
||||
|
|
@ -208,7 +208,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
// Create thread if this is the first comment
|
||||
if (!lastCommentId) {
|
||||
const { id: threadId } = await (
|
||||
await fetch(
|
||||
await this.fetch(
|
||||
`https://discord.com/api/channels/${channel}/messages/${postId}/threads`,
|
||||
{
|
||||
method: 'POST',
|
||||
|
|
@ -224,9 +224,6 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
)
|
||||
).json();
|
||||
threadChannel = threadId;
|
||||
} else {
|
||||
// Extract thread channel from the last comment's URL or use channel directly
|
||||
threadChannel = channel;
|
||||
}
|
||||
|
||||
const form = new FormData();
|
||||
|
|
@ -234,7 +231,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
'payload_json',
|
||||
JSON.stringify({
|
||||
content: commentPost.message.replace(/\[\[\[(@.*?)]]]/g, (match, p1) => {
|
||||
return `<${p1}>`;
|
||||
return `<${p1}>`;
|
||||
}),
|
||||
attachments: commentPost.media?.map((p, index) => ({
|
||||
id: index,
|
||||
|
|
@ -257,7 +254,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
}
|
||||
|
||||
const data = await (
|
||||
await fetch(
|
||||
await this.fetch(
|
||||
`https://discord.com/api/channels/${threadChannel}/messages`,
|
||||
{
|
||||
method: 'POST',
|
||||
|
|
@ -281,7 +278,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
|
||||
async changeNickname(id: string, accessToken: string, name: string) {
|
||||
await (
|
||||
await fetch(`https://discord.com/api/guilds/${id}/members/@me`, {
|
||||
await this.fetch(`https://discord.com/api/guilds/${id}/members/@me`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`,
|
||||
|
|
@ -305,7 +302,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
integration: Integration
|
||||
) {
|
||||
const allRoles = await (
|
||||
await fetch(`https://discord.com/api/guilds/${id}/roles`, {
|
||||
await this.fetch(`https://discord.com/api/guilds/${id}/roles`, {
|
||||
headers: {
|
||||
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`,
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -320,7 +317,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
.filter((f: any) => f.name !== '@everyone' && f.name !== '@here');
|
||||
|
||||
const list = await (
|
||||
await fetch(
|
||||
await this.fetch(
|
||||
`https://discord.com/api/guilds/${id}/members/search?query=${data.query}`,
|
||||
{
|
||||
headers: {
|
||||
|
|
@ -368,4 +365,47 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
|
|||
}
|
||||
return `[[[@${idOrHandle.replace('@', '')}]]]`;
|
||||
}
|
||||
|
||||
override handleErrors(
|
||||
body: string
|
||||
):
|
||||
| { type: 'refresh-token' | 'bad-body' | 'retry'; value: string }
|
||||
| undefined {
|
||||
if (body.includes('50001')) {
|
||||
return {
|
||||
type: 'bad-body',
|
||||
value: "Bot doesn't have access to this channel",
|
||||
};
|
||||
}
|
||||
|
||||
if (body.includes('50013')) {
|
||||
return {
|
||||
type: 'bad-body',
|
||||
value: 'Bot lacks permission in this channel',
|
||||
};
|
||||
}
|
||||
|
||||
if (body.includes('10003')) {
|
||||
return {
|
||||
type: 'bad-body',
|
||||
value: 'Channel no longer exists',
|
||||
};
|
||||
}
|
||||
|
||||
if (body.includes('40005')) {
|
||||
return {
|
||||
type: 'bad-body',
|
||||
value: "Attachment exceeds Discord's size limit",
|
||||
};
|
||||
}
|
||||
|
||||
if (body.includes('20028')) {
|
||||
return {
|
||||
type: 'retry',
|
||||
value: 'Rate limited by Discord',
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue