JCo
JCo
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by JCo on 12/7/2024 in #djs-questions
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);
}
},
});
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);
}
},
});
27 replies
CDCloudflare Developers
Created by JCo on 3/25/2024 in #workers-help
How do I set router properties on @cloudflare/itty-router-openapi
No description
4 replies