Skip to content

Commit

Permalink
feat: support webhooks in threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Jan 20, 2025
1 parent 2f23ac5 commit ba5e257
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion locale
Submodule locale updated 3 files
+16 −3 commands/en.json
+367 −85 commands/fr.json
+237 −217 webhook/fr.json
6 changes: 6 additions & 0 deletions src/db/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface WebhookAttributes {
guildID: string;
webhookID: string;
webhookToken: string;
threadID: string;
threadParent: string;
whitelist: string;
lists: string[];
cards: string[];
Expand All @@ -37,6 +39,8 @@ export class Webhook extends Model<WebhookAttributes> implements WebhookAttribut
public guildID!: string;
public webhookID!: string;
public webhookToken!: string;
public threadID!: string;
public threadParent!: string;
public whitelist!: string;
public lists!: string[];
public cards!: string[];
Expand Down Expand Up @@ -86,6 +90,8 @@ Webhook.init(
},
webhookID: Sequelize.STRING,
webhookToken: Sequelize.STRING,
threadID: Sequelize.STRING,
threadParent: Sequelize.STRING,
whitelist: {
type: Sequelize.BOOLEAN,
allowNull: false,
Expand Down
38 changes: 35 additions & 3 deletions src/util/webhookData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ export default class WebhookData {
}

private async _send(embeds: any[], attempt = 1) {
const thread = this.webhook.threadID;
try {
await request('POST', `/webhooks/${this.webhook.webhookID}/${this.webhook.webhookToken}`, {
await request('POST', `/webhooks/${this.webhook.webhookID}/${this.webhook.webhookToken}${thread ? `?thread_id=${thread}` : ''}`, {
embeds
});
} catch (e) {
Expand All @@ -404,7 +405,9 @@ export default class WebhookData {
await Webhook.update(
{
webhookID: null,
webhookToken: null
webhookToken: null,
threadID: null,
threadParent: null
},
{ where: { id: this.webhook.id } }
);
Expand All @@ -413,7 +416,36 @@ export default class WebhookData {
await Webhook.update(
{
webhookID: null,
webhookToken: null
webhookToken: null,
threadID: null,
threadParent: null
},
{ where: { id: this.webhook.id } }
);
} else if (e.code === 10003) {
logger.warn(`Discord webhook points to a lost thread, removing thread setting @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
await Webhook.update(
{
threadID: null,
active: false
},
{ where: { id: this.webhook.id } }
);
} else if (e.code === 160005) {
logger.warn(`Discord webhook points to a locked thread, removing thread setting @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
await Webhook.update(
{
threadID: null,
active: false
},
{ where: { id: this.webhook.id } }
);
} else if (e.code === 220001) {
logger.warn(`Discord webhook tried posting to forum channel with no thread id @ ${this.webhook.webhookID}:${this.webhook.id}`, e);
await Webhook.update(
{
threadID: '0',
active: false
},
{ where: { id: this.webhook.id } }
);
Expand Down

0 comments on commit ba5e257

Please sign in to comment.