discord.js - Imagine ❄d-I❄
discord.js - Imagine ❄14mo ago
26 replies
JCo

Not triggering on entitlement create

Does anyone know why the code here wouldn't work to create a user and make a subscription on entitlement create?
import { EntitlementType, Events } from "discord.js";
import { Client } from "..";
import { ClientEvent } from "../lib/classes/Event";
import { db } from "../lib/utils/db";

export default new ClientEvent({
    name: Events.EntitlementCreate,
    once: false,
    async execute(entitlement) {
        console.log("New subscription");

        try {
            const { skuId, type, startsAt, endsAt, deleted, userId } =
                entitlement;

            if (type !== EntitlementType.ApplicationSubscription) {
                return;
            }

            const existingUser = await db.user.findUnique({
                where: {
                    id: userId,
                },
            });

            if (!existingUser) {
                const { username } = await Client.users.fetch(userId);

                await db.user.create({
                    data: {
                        id: userId,
                        username,
                    },
                });
            }

            const existingSubscription =
                await db.premiumSubscription.findUnique({
                    where: {
                        userId,
                    },
                });

            if (existingSubscription) {
                await db.premiumSubscription.update({
                    where: {
                        id: existingSubscription.id,
                    },
                    data: {
                        skuId,
                        status: deleted ? "CANCELLED" : "ACTIVE",
                        startDate: startsAt,
                        endDate: endsAt || null,
                        userId,
                    },
                });
            } else {
                await db.premiumSubscription.create({
                    data: {
                        skuId,
                        status: deleted ? "CANCELLED" : "ACTIVE",
                        startDate: startsAt,
                        endDate: endsAt || null,
                        userId,
                    },
                });
            }

            return;
        } catch (err) {
            console.error(err);
        }
    },
});
Was this page helpful?