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);
}
},
});
19 Replies
d.js toolkit
d.js toolkit2mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
NyR
NyR2mo ago
Does the event not emit?
JCo
JCoOP2mo ago
No IDK how to get someone to test it for me without them having to pay for it
NyR
NyR2mo ago
You can create a test entitlement
d.js docs (dev)
d.js docs (dev)2mo ago
:method: EntitlementManager#createTest() [email protected] Creates a test entitlement :property: ClientApplication#entitlements [email protected] The entitlement manager for this application
JCo
JCoOP2mo ago
my bot has no commands yet, thats for a interaction endpoint on https api I can trigger it from Insomnia tho
NyR
NyR2mo ago
? You don't need to have a command for creating entitlements. Test entitlement will be free for developers of the app so you can consume it without having to pay for it
JCo
JCoOP2mo ago
Yes but that is an emit for a comand is it not?
NyR
NyR2mo ago
I'm not sure what you are talking about Are you saying the event doesn't emit because you have no way to test it or the event doesn't emit at all when an entitlement is created?
JCo
JCoOP2mo ago
I created on using the payment flow from discord, as it is in my team, and nothing was handled by the app
NyR
NyR2mo ago
Do you by any chance have enabled webhook events for Entitlement Create?
JCo
JCoOP2mo ago
No description
JCo
JCoOP2mo ago
Its off but yeah
NyR
NyR2mo ago
I'm not sure if that affects it.. you can try disabling it specifically and then see
JCo
JCoOP2mo ago
What do I need to do to be able to send an entitlement create to my app? Authorization header? I've cancelled the entitlement in the settings and it has not sent an upate or delete to the bot events.
NyR
NyR2mo ago
Huh? Again I have no idea what you are talking about. Maybe you should explain what you are doing and what you expect to happen? ^ followup to this
JCo
JCoOP2mo ago
When a user creates or cancels an app subscription, it should send the EntitlementCreate, EntitlementUpdate or EntitlementDeleted event to the bot. The bot then finds the corresponding user and subscription, creates if not there on create, and deletes if its deleted. and updates if its been updated.
NyR
NyR2mo ago
Yeah I get that, but what makes you think it doesn't emit? How are you testing it? Or even, have you made sure that your event handler is properly configured? Is it just this event not emitting or all?
JCo
JCoOP2mo ago
I found out its how I was handling the DB. I was using a DB connection that runs on the edge in vercel, needed to modify for non-edge style hosting. And there is a weird issue with the one that is being run on my account that I tested. It doesnt emit when i activate or cancel it. If you want to test it I can add you to the team for now?

Did you find this page helpful?