Skip to content

Commit

Permalink
chore: fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Dec 16, 2024
1 parent cc4bbe0 commit b6ca38f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
12 changes: 11 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import Eris from 'eris';

import { onChannelCreate, onChannelDelete, onChannelUpdate, onEntitlementCreate, onEntitlementDelete, onEntitlementUpdate, onGuildJoin, onGuildLeave, onWebhooksUpdate } from './events';
import {
onChannelCreate,
onChannelDelete,
onChannelUpdate,
onEntitlementCreate,
onEntitlementDelete,
onEntitlementUpdate,
onGuildJoin,
onGuildLeave,
onWebhooksUpdate
} from './events';
import { logger } from './logger';
import { start as startPoster } from './poster';

Expand Down
104 changes: 61 additions & 43 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export function onChannelDelete(channel: Eris.AnyChannel) {
const ENTITLEMENTS_ENABLED = !!process.env.DISCORD_SKU_TIER_1 && !!process.env.DISCORD_SKU_TIER_2;

export async function onEntitlementCreate(entitlement: Eris.Entitlement) {
logger.info(`Entitlement ${entitlement.id} created (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`);
logger.info(
`Entitlement ${entitlement.id} created (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`
);
const active = entitlement.endsAt ? Date.now() < entitlement.endsAt : true;

try {
Expand All @@ -73,7 +75,12 @@ export async function onEntitlementCreate(entitlement: Eris.Entitlement) {
}

// Apply entitlement
if ((entitlement.skuID === process.env.DISCORD_SKU_TIER_1 || entitlement.skuID === process.env.DISCORD_SKU_TIER_2) && entitlement.guildID && active && ENTITLEMENTS_ENABLED) {
if (
(entitlement.skuID === process.env.DISCORD_SKU_TIER_1 || entitlement.skuID === process.env.DISCORD_SKU_TIER_2) &&
entitlement.guildID &&
active &&
ENTITLEMENTS_ENABLED
) {
const maxWebhooks = entitlement.skuID === process.env.DISCORD_SKU_TIER_2 ? 200 : 20;
logger.info(`Benefits for ${entitlement.guildID} updated (maxWebhooks=${maxWebhooks})`);
await prisma.server.upsert({
Expand All @@ -98,24 +105,28 @@ export async function onEntitlementCreate(entitlement: Eris.Entitlement) {
},
body: JSON.stringify({
content: `https://discord.com/application-directory/${entitlement.applicationID}/store/${entitlement.skuID}`,
embeds: [{
title: `Entitlement Created${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0x2ecc71,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Starts At: ${entitlement.startsAt ? `<t:${Math.round(new Date(entitlement.startsAt).valueOf() / 1000)}` : '<none>'}`,
`Ends At: ${entitlement.endsAt ? `<t:${Math.round(new Date(entitlement.endsAt).valueOf() / 1000)}` : '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}]
embeds: [
{
title: `Entitlement Created${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0x2ecc71,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Starts At: ${entitlement.startsAt ? `<t:${Math.round(new Date(entitlement.startsAt).valueOf() / 1000)}` : '<none>'}`,
`Ends At: ${entitlement.endsAt ? `<t:${Math.round(new Date(entitlement.endsAt).valueOf() / 1000)}` : '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}
]
})
}).catch(() => {});
}

export async function onEntitlementUpdate(entitlement: Eris.Entitlement) {
logger.info(`Entitlement ${entitlement.id} updated (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`);
logger.info(
`Entitlement ${entitlement.id} updated (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`
);

const active = entitlement.endsAt ? Date.now() < entitlement.endsAt : true;
try {
Expand Down Expand Up @@ -154,24 +165,28 @@ export async function onEntitlementUpdate(entitlement: Eris.Entitlement) {
},
body: JSON.stringify({
content: `https://discord.com/application-directory/${entitlement.applicationID}/store/${entitlement.skuID}`,
embeds: [{
title: `Entitlement Updated${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0xe67e22,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Starts At: ${entitlement.startsAt ? `<t:${Math.round(new Date(entitlement.startsAt).valueOf() / 1000)}` : '<none>'}`,
`Ends At: ${entitlement.endsAt ? `<t:${Math.round(new Date(entitlement.endsAt).valueOf() / 1000)}` : '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}]
embeds: [
{
title: `Entitlement Updated${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0xe67e22,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Starts At: ${entitlement.startsAt ? `<t:${Math.round(new Date(entitlement.startsAt).valueOf() / 1000)}` : '<none>'}`,
`Ends At: ${entitlement.endsAt ? `<t:${Math.round(new Date(entitlement.endsAt).valueOf() / 1000)}` : '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}
]
})
}).catch(() => {});
}

export async function onEntitlementDelete(entitlement: Eris.Entitlement) {
logger.info(`Entitlement ${entitlement.id} deleted (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`);
logger.info(
`Entitlement ${entitlement.id} deleted (guild=${entitlement.guildID}, user=${entitlement.userID}, sku=${entitlement.skuID}, type=${entitlement.type})`
);

try {
const dbEntitlement = await prisma.discordEntitlement.delete({
Expand All @@ -195,16 +210,18 @@ export async function onEntitlementDelete(entitlement: Eris.Entitlement) {
},
body: JSON.stringify({
content: `https://discord.com/application-directory/${entitlement.applicationID}/store/${entitlement.skuID}`,
embeds: [{
title: `Entitlement Deleted${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0xe74c3c,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}]
embeds: [
{
title: `Entitlement Deleted${!entitlement.startsAt ? ' [Test]' : ''}`,
color: 0xe74c3c,
description: [
`SKU: ${entitlement.skuID}`,
`User ID: ${entitlement.userID ?? '<none>'}`,
`Guild ID: ${entitlement.guildID ?? '<none>'}`,
`Type: ${Eris.Constants.EntitlementTypes[entitlement.type] ?? '<unknown>'} (${entitlement.type})`
].join('\n')
}
]
})
}).catch(() => {});
}
Expand All @@ -228,10 +245,11 @@ async function updateGuildBenefits(guildId: string) {
}
});

const maxWebhooks =
otherEntitlements.find((e) => e.skuId === process.env.DISCORD_SKU_TIER_2) ? 200 :
otherEntitlements.find((e) => e.skuId === process.env.DISCORD_SKU_TIER_1) ? 20 :
5;
const maxWebhooks = otherEntitlements.find((e) => e.skuId === process.env.DISCORD_SKU_TIER_2)
? 200
: otherEntitlements.find((e) => e.skuId === process.env.DISCORD_SKU_TIER_1)
? 20
: 5;

logger.info(`Benefits for ${guildId} updated (maxWebhooks=${maxWebhooks})`);
await prisma.server.upsert({
Expand Down Expand Up @@ -262,7 +280,7 @@ async function updateGuildBenefits(guildId: string) {
});
}

const entitlementCron = new CronJob('*/5 * * * *', onEntitlementCron, null, true, 'America/New_York');
export const entitlementCron = new CronJob('*/5 * * * *', onEntitlementCron, null, true, 'America/New_York');

async function onEntitlementCron() {
const expiredEntitlements = await prisma.discordEntitlement.findMany({
Expand Down

0 comments on commit b6ca38f

Please sign in to comment.