Skip to content

Commit ba5e257

Browse files
committed
feat: support webhooks in threads
1 parent 2f23ac5 commit ba5e257

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/db/postgres.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface WebhookAttributes {
1919
guildID: string;
2020
webhookID: string;
2121
webhookToken: string;
22+
threadID: string;
23+
threadParent: string;
2224
whitelist: string;
2325
lists: string[];
2426
cards: string[];
@@ -37,6 +39,8 @@ export class Webhook extends Model<WebhookAttributes> implements WebhookAttribut
3739
public guildID!: string;
3840
public webhookID!: string;
3941
public webhookToken!: string;
42+
public threadID!: string;
43+
public threadParent!: string;
4044
public whitelist!: string;
4145
public lists!: string[];
4246
public cards!: string[];
@@ -86,6 +90,8 @@ Webhook.init(
8690
},
8791
webhookID: Sequelize.STRING,
8892
webhookToken: Sequelize.STRING,
93+
threadID: Sequelize.STRING,
94+
threadParent: Sequelize.STRING,
8995
whitelist: {
9096
type: Sequelize.BOOLEAN,
9197
allowNull: false,

src/util/webhookData.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ export default class WebhookData {
393393
}
394394

395395
private async _send(embeds: any[], attempt = 1) {
396+
const thread = this.webhook.threadID;
396397
try {
397-
await request('POST', `/webhooks/${this.webhook.webhookID}/${this.webhook.webhookToken}`, {
398+
await request('POST', `/webhooks/${this.webhook.webhookID}/${this.webhook.webhookToken}${thread ? `?thread_id=${thread}` : ''}`, {
398399
embeds
399400
});
400401
} catch (e) {
@@ -404,7 +405,9 @@ export default class WebhookData {
404405
await Webhook.update(
405406
{
406407
webhookID: null,
407-
webhookToken: null
408+
webhookToken: null,
409+
threadID: null,
410+
threadParent: null
408411
},
409412
{ where: { id: this.webhook.id } }
410413
);
@@ -413,7 +416,36 @@ export default class WebhookData {
413416
await Webhook.update(
414417
{
415418
webhookID: null,
416-
webhookToken: null
419+
webhookToken: null,
420+
threadID: null,
421+
threadParent: null
422+
},
423+
{ where: { id: this.webhook.id } }
424+
);
425+
} else if (e.code === 10003) {
426+
logger.warn(`Discord webhook points to a lost thread, removing thread setting @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
427+
await Webhook.update(
428+
{
429+
threadID: null,
430+
active: false
431+
},
432+
{ where: { id: this.webhook.id } }
433+
);
434+
} else if (e.code === 160005) {
435+
logger.warn(`Discord webhook points to a locked thread, removing thread setting @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
436+
await Webhook.update(
437+
{
438+
threadID: null,
439+
active: false
440+
},
441+
{ where: { id: this.webhook.id } }
442+
);
443+
} else if (e.code === 220001) {
444+
logger.warn(`Discord webhook tried posting to forum channel with no thread id @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
445+
await Webhook.update(
446+
{
447+
threadID: '0',
448+
active: false
417449
},
418450
{ where: { id: this.webhook.id } }
419451
);

0 commit comments

Comments
 (0)