Skip to content

Commit 45741d2

Browse files
feat(discord): Handle missing permissions for webhook creation (#129)
Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com>
1 parent 15facd6 commit 45741d2

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/routes/subscriptions/discord/webhooks.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ErrorCode } from "@/schema/errors/ErrorCode"
99
import { zInsufficientPermissionsError } from "@/schema/errors/InsufficientPermissionsError"
1010
import { zInvalidDiscordAuthError } from "@/schema/errors/InvalidDiscordAuthError"
1111
import {
12+
DiscordWebhookMissingPermissionsError,
1213
deleteDiscordWebhook,
1314
getDiscordWebhookStatus,
1415
upsertDiscordWebhook
@@ -47,13 +48,22 @@ export const putDiscordWebhookRoute = new RaidHubRoute({
4748
message: "Forbidden" as const
4849
})
4950
}
50-
return RaidHubRoute.ok(
51-
await upsertDiscordWebhook({
52-
...req.body,
53-
guildId,
54-
channelId
55-
})
56-
)
51+
try {
52+
return RaidHubRoute.ok(
53+
await upsertDiscordWebhook({
54+
...req.body,
55+
guildId,
56+
channelId
57+
})
58+
)
59+
} catch (err) {
60+
if (err instanceof DiscordWebhookMissingPermissionsError) {
61+
return RaidHubRoute.fail(ErrorCode.InsufficientPermissionsError, {
62+
message: "Forbidden" as const
63+
})
64+
}
65+
throw err
66+
}
5767
}
5868
})
5969

src/services/subscriptions/discord-webhooks.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { Logger } from "@/lib/utils/logging"
33

44
const logger = new Logger("DISCORD_SUBSCRIPTIONS_SERVICE")
55

6+
export class DiscordWebhookMissingPermissionsError extends Error {
7+
constructor(channelId: string) {
8+
super(`Discord bot is missing permissions to create a webhook in channel ${channelId}`)
9+
this.name = "DiscordWebhookMissingPermissionsError"
10+
}
11+
}
12+
613
export type DiscordWebhookPlayerTargetInput = {
714
membershipId: string
815
requireFresh?: boolean
@@ -113,6 +120,9 @@ const createDiscordWebhook = async (
113120

114121
if (!response.ok) {
115122
const detail = await response.text()
123+
if (response.status === 403) {
124+
throw new DiscordWebhookMissingPermissionsError(body.channelId)
125+
}
116126
throw new Error(`Discord webhook create failed with status ${response.status}: ${detail}`)
117127
}
118128

0 commit comments

Comments
 (0)