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 toolkit2w 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
NyR2w ago
Does the event not emit?
JCo
JCoOP2w ago
No IDK how to get someone to test it for me without them having to pay for it
NyR
NyR2w ago
You can create a test entitlement
d.js docs (dev)
:method: EntitlementManager#createTest() [email protected] Creates a test entitlement :property: ClientApplication#entitlements [email protected] The entitlement manager for this application
JCo
JCoOP2w ago
my bot has no commands yet, thats for a interaction endpoint on https api I can trigger it from Insomnia tho
NyR
NyR2w 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
JCoOP2w ago
Yes but that is an emit for a comand is it not?
NyR
NyR2w 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
JCoOP2w ago
I created on using the payment flow from discord, as it is in my team, and nothing was handled by the app
NyR
NyR2w ago
Do you by any chance have enabled webhook events for Entitlement Create?
JCo
JCoOP2w ago
No description
JCo
JCoOP2w ago
Its off but yeah
NyR
NyR2w ago
I'm not sure if that affects it.. you can try disabling it specifically and then see
JCo
JCoOP2w 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
NyR2w 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
JCoOP2w 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
NyR2w 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
JCoOP2w 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?
Want results from more Discord servers?
Add your server