mirror of
https://github.com/immich-app/discord-bot.git
synced 2026-09-30 21:37:38 +08:00
feat: stripe webhook notifications (#33)
This commit is contained in:
@@ -48,6 +48,7 @@ const channels = {
|
||||
General: '994044917355663450',
|
||||
BotSpam: '1159083520027787307',
|
||||
GithubStatus: '1240662502912692236',
|
||||
Stripe: '1263493037162627103',
|
||||
};
|
||||
|
||||
const misc = {
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
import express from 'express';
|
||||
import { bot } from '../main.js';
|
||||
import { Constants } from '../constants.js';
|
||||
import { bot } from '../../main.js';
|
||||
import { Constants } from '../../constants.js';
|
||||
import { EmbedBuilder, TextChannel } from 'discord.js';
|
||||
|
||||
const app = express.Router();
|
||||
@@ -113,4 +113,4 @@ app.post('/github-status/:slug', async (req, res) => {
|
||||
res.status(200).send();
|
||||
});
|
||||
|
||||
export const webhooks = app;
|
||||
export const githubWebhooks = app;
|
||||
@@ -0,0 +1,55 @@
|
||||
import express from 'express';
|
||||
import { bot } from '../../main.js';
|
||||
import { Constants } from '../../constants.js';
|
||||
import { EmbedBuilder, TextChannel } from 'discord.js';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
type StripeBase = {
|
||||
id: string;
|
||||
object: string;
|
||||
};
|
||||
|
||||
type StripePaymentIntent = StripeBase & {
|
||||
amount: number;
|
||||
currency: number;
|
||||
created: number;
|
||||
description: string;
|
||||
};
|
||||
|
||||
const isStripePaymentIntent = (payload: StripeBase): payload is StripePaymentIntent => {
|
||||
if ((payload as StripeBase).object === 'payment_intent') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const isImmichPaymentIntent = (payload: StripePaymentIntent): payload is StripePaymentIntent => {
|
||||
return ['immich-server', 'immich-client'].includes((payload as StripePaymentIntent).description);
|
||||
};
|
||||
|
||||
app.post('/stripe-payments/:slug', async (req, res) => {
|
||||
if (req.params.slug !== process.env.STRIPE_PAYMENT_SLUG) {
|
||||
res.status(401).send();
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(204).send();
|
||||
|
||||
const body = req.body;
|
||||
console.log(JSON.stringify(body));
|
||||
|
||||
if (!isStripePaymentIntent(body) || !isImmichPaymentIntent(body)) return;
|
||||
|
||||
const licenseType = body.description.split('-')[1];
|
||||
const channel = (await bot.channels.fetch(Constants.Channels.Stripe)) as TextChannel;
|
||||
const embed = new EmbedBuilder({
|
||||
title: `Immich ${licenseType} license purchased`,
|
||||
author: { name: 'Stripe Payments', url: 'https://stripe.com' },
|
||||
url: `https://dashboard.stripe.com/payments/${body.id}`,
|
||||
description: `Total: ${body.amount} ${body.currency}`,
|
||||
});
|
||||
await channel.send({ embeds: [embed] });
|
||||
});
|
||||
|
||||
export const stripeWebhooks = app;
|
||||
+3
-2
@@ -5,7 +5,8 @@ import { IntentsBitField, Partials } from 'discord.js';
|
||||
import { Client } from 'discordx';
|
||||
import { Constants } from './constants.js';
|
||||
import express from 'express';
|
||||
import { webhooks } from './controllers/webhooks.controller.js';
|
||||
import { githubWebhooks } from './controllers/webhooks/github.controller.js';
|
||||
import { stripeWebhooks } from './controllers/webhooks/stripe.controller.js';
|
||||
|
||||
export const bot = new Client({
|
||||
// Discord intents
|
||||
@@ -84,7 +85,7 @@ async function run() {
|
||||
|
||||
// Log in with your bot token
|
||||
app.use(express.json());
|
||||
app.use('/webhooks', webhooks);
|
||||
app.use('/webhooks', [githubWebhooks, stripeWebhooks]);
|
||||
app.listen(8080, () => {
|
||||
console.log('Bot listening on port 8080');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user