refactor: 🧑💻 improve developer experience
This commit is contained in:
parent
089b20a8cd
commit
96cb38464a
19 changed files with 94 additions and 87 deletions
|
@ -0,0 +1,2 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE GuildMemberCredits RENAME TO GuildMemberCredit;
|
|
@ -63,7 +63,7 @@ model GuildMember {
|
|||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
GuildShopRoles GuildShopRoles[]
|
||||
GuildMemberCredits GuildMemberCredits?
|
||||
GuildMemberCredit GuildMemberCredit?
|
||||
|
||||
// Unique Identifier
|
||||
@@unique([userId, guildId])
|
||||
|
@ -214,7 +214,7 @@ model GuildCounters {
|
|||
@@unique([guildId, channelId])
|
||||
}
|
||||
|
||||
model GuildMemberCredits {
|
||||
model GuildMemberCredit {
|
||||
userId String
|
||||
guildId String
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
const EmbedSuccess = await BaseEmbedSuccess(guild, ":credit_card:︱Balance");
|
||||
|
||||
// 5. Upsert the user in the database.
|
||||
const createGuildMemberCredits = await prisma.guildMemberCredits.upsert({
|
||||
const createguildMemberCredit = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: (target || user).id,
|
||||
|
@ -61,7 +61,7 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
include: { GuildMember: true },
|
||||
});
|
||||
|
||||
logger.silly(createGuildMemberCredits);
|
||||
logger.silly(createguildMemberCredit);
|
||||
|
||||
await upsertGuildMember(guild, user);
|
||||
|
||||
|
@ -70,8 +70,8 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
embeds: [
|
||||
EmbedSuccess.setDescription(
|
||||
target
|
||||
? `${target} has ${createGuildMemberCredits.balance} coins in his account.`
|
||||
: `You have ${createGuildMemberCredits.balance} coins in your account.`
|
||||
? `${target} has ${createguildMemberCredit.balance} coins in his account.`
|
||||
: `You have ${createguildMemberCredit.balance} coins in your account.`
|
||||
),
|
||||
],
|
||||
});
|
||||
|
|
|
@ -65,7 +65,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
// 5. Start an transaction of the credits.
|
||||
await creditsTransfer(guild, user, account, credits);
|
||||
|
||||
const receiverGuildMember = await prisma.guildMemberCredits.upsert({
|
||||
const receiverGuildMember = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: account.id,
|
||||
|
@ -106,7 +106,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
],
|
||||
});
|
||||
|
||||
const senderGuildMember = await prisma.guildMemberCredits.upsert({
|
||||
const senderGuildMember = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: user.id,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { GuildMemberCredits } from "@prisma/client";
|
||||
import { GuildMemberCredit } from "@prisma/client";
|
||||
import {
|
||||
CommandInteraction,
|
||||
SlashCommandSubcommandBuilder,
|
||||
|
@ -32,7 +32,7 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Top");
|
||||
|
||||
// 4. Get the top 10 users.
|
||||
const topTen = await prisma.guildMemberCredits.findMany({
|
||||
const topTen = await prisma.guildMemberCredit.findMany({
|
||||
where: {
|
||||
guildId: guild.id,
|
||||
},
|
||||
|
@ -44,9 +44,9 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
logger.silly(topTen);
|
||||
|
||||
// 5. Create the top 10 list.
|
||||
const entry = (guildMemberCredits: GuildMemberCredits, index: number) =>
|
||||
`${index + 1}. ${userMention(guildMemberCredits.userId)} | :coin: ${
|
||||
guildMemberCredits.balance
|
||||
const entry = (guildMemberCredit: GuildMemberCredit, index: number) =>
|
||||
`${index + 1}. ${userMention(guildMemberCredit.userId)} | :coin: ${
|
||||
guildMemberCredit.balance
|
||||
}`;
|
||||
|
||||
// 6. Send embed
|
||||
|
|
|
@ -124,7 +124,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
// Get guild object
|
||||
const pricePerHour = upsertGuildConfigShopRoles.pricePerHour;
|
||||
|
||||
const updateGuildMember = await prisma.guildMemberCredits.update({
|
||||
const updateGuildMember = await prisma.guildMemberCredit.update({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId,
|
||||
|
|
|
@ -57,7 +57,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
await guild?.roles
|
||||
.delete(optionRole?.id, `${user?.id} canceled from shop`)
|
||||
.then(async () => {
|
||||
const createGuildMember = await prisma.guildMemberCredits.upsert({
|
||||
const createGuildMember = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: user.id,
|
||||
|
|
|
@ -48,7 +48,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
}
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
|
||||
const upsertGuildMemberCredits = await prisma.guildMemberCredits.upsert({
|
||||
const upsertguildMemberCredit = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: user.id,
|
||||
|
@ -74,8 +74,8 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
},
|
||||
});
|
||||
|
||||
if (!upsertGuildMemberCredits)
|
||||
throw new Error("upsertGuildMemberCredits unavailable");
|
||||
if (!upsertguildMemberCredit)
|
||||
throw new Error("upsertguildMemberCredit unavailable");
|
||||
|
||||
const upsertGuildConfigApisCpgg = await prisma.guildConfigApisCpgg.upsert({
|
||||
where: {
|
||||
|
@ -99,17 +99,17 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
},
|
||||
});
|
||||
|
||||
logger.silly(upsertGuildMemberCredits);
|
||||
logger.silly(upsertguildMemberCredit);
|
||||
|
||||
const dmUser = client?.users?.cache?.get(user?.id);
|
||||
|
||||
if ((optionAmount || upsertGuildMemberCredits.balance) < 100)
|
||||
if ((optionAmount || upsertguildMemberCredit.balance) < 100)
|
||||
throw new Error("You can't withdraw to CPGG below 100 credits.");
|
||||
|
||||
if ((optionAmount || upsertGuildMemberCredits.balance) > 1000000)
|
||||
if ((optionAmount || upsertguildMemberCredit.balance) > 1000000)
|
||||
throw new Error("Amount or user credits is above 1.000.000.");
|
||||
|
||||
if (upsertGuildMemberCredits.balance < optionAmount)
|
||||
if (upsertguildMemberCredit.balance < optionAmount)
|
||||
throw new Error("You can't withdraw more than you have on your account.");
|
||||
|
||||
if (!upsertGuildConfigApisCpgg.urlIv || !upsertGuildConfigApisCpgg.urlContent)
|
||||
|
@ -148,15 +148,15 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
?.post("vouchers", {
|
||||
uses: 1,
|
||||
code,
|
||||
credits: optionAmount || upsertGuildMemberCredits.balance,
|
||||
credits: optionAmount || upsertguildMemberCredit.balance,
|
||||
memo: `${interaction?.createdTimestamp} - ${interaction?.user?.id}`,
|
||||
})
|
||||
?.then(async () => {
|
||||
logger?.silly(`Successfully created voucher.`);
|
||||
upsertGuildMemberCredits.balance -=
|
||||
optionAmount || upsertGuildMemberCredits.balance;
|
||||
upsertguildMemberCredit.balance -=
|
||||
optionAmount || upsertguildMemberCredit.balance;
|
||||
|
||||
const updateGuildMember = await prisma.guildMemberCredits.update({
|
||||
const updateGuildMember = await prisma.guildMemberCredit.update({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId: user.id,
|
||||
|
@ -165,7 +165,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
},
|
||||
data: {
|
||||
balance: {
|
||||
decrement: optionAmount || upsertGuildMemberCredits.balance,
|
||||
decrement: optionAmount || upsertguildMemberCredit.balance,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
@ -181,7 +181,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
|
|||
.setTimestamp()
|
||||
.addFields({
|
||||
name: "💶 Credits",
|
||||
value: `${optionAmount || upsertGuildMemberCredits.balance}`,
|
||||
value: `${optionAmount || upsertguildMemberCredit.balance}`,
|
||||
inline: true,
|
||||
})
|
||||
.setColor(successColor)
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// Dependencies
|
||||
import { Message } from "discord.js";
|
||||
import logger from "../../middlewares/logger";
|
||||
|
||||
// Modules
|
||||
import counter from "./modules/counter";
|
||||
|
@ -14,17 +13,13 @@ export const options: IEventOptions = {
|
|||
|
||||
// Execute the function
|
||||
export const execute = async (oldMessage: Message, newMessage: Message) => {
|
||||
const { author, guild } = newMessage;
|
||||
const { author } = newMessage;
|
||||
|
||||
await audits.execute(oldMessage, newMessage);
|
||||
|
||||
logger?.silly(
|
||||
`Message update event fired by ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||
);
|
||||
|
||||
if (author?.bot) return logger?.silly(`Message update event fired by bot`);
|
||||
if (author.bot) return;
|
||||
|
||||
await counter(newMessage);
|
||||
|
||||
return true;
|
||||
return;
|
||||
};
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
/* eslint-disable no-loops/no-loops */
|
||||
import { Client } from "discord.js";
|
||||
import checkDirectory from "../../helpers/checkDirectory";
|
||||
import { ICommand } from "../../interfaces/Command";
|
||||
|
@ -6,8 +5,10 @@ import logger from "../../middlewares/logger";
|
|||
|
||||
// Register the commands.
|
||||
export const register = async (client: Client) => {
|
||||
await checkDirectory("commands").then(async (commandNames) => {
|
||||
for await (const commandName of commandNames) {
|
||||
const profiler = logger.startTimer();
|
||||
|
||||
await checkDirectory("commands").then((commandNames) => {
|
||||
commandNames.forEach(async (commandName) => {
|
||||
const commandProfiler = logger.startTimer();
|
||||
|
||||
await import(`../../commands/${commandName}`)
|
||||
|
@ -30,6 +31,10 @@ export const register = async (client: Client) => {
|
|||
level: "error",
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return profiler.done({
|
||||
message: "Successfully registered all commands!",
|
||||
});
|
||||
};
|
||||
|
|
|
@ -14,14 +14,6 @@ prisma.$use(async (params, next) => {
|
|||
`Query ${params.model}.${params.action} took ${after - before}ms`
|
||||
);
|
||||
|
||||
if (after - before >= 50) {
|
||||
logger.warn(
|
||||
`Query ${params.model}.${params.action} took long time ${
|
||||
after - before
|
||||
}ms`
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
/* eslint-disable no-loops/no-loops */
|
||||
import { Client } from "discord.js";
|
||||
import checkDirectory from "../../helpers/checkDirectory";
|
||||
import { IEvent } from "../../interfaces/Event";
|
||||
|
@ -8,10 +7,7 @@ import logger from "../../middlewares/logger";
|
|||
export const register = async (client: Client) => {
|
||||
const profiler = logger.startTimer();
|
||||
|
||||
await checkDirectory("events").then(async (eventNames) => {
|
||||
const totalEvents = eventNames.length;
|
||||
let loadedEvents = 0;
|
||||
|
||||
await checkDirectory("events").then((eventNames) => {
|
||||
// Import an event.
|
||||
const importEvent = async (name: string) => {
|
||||
await import(`../../events/${name}`).then((event: IEvent) => {
|
||||
|
@ -37,20 +33,17 @@ export const register = async (client: Client) => {
|
|||
type: event.options.type,
|
||||
message: `Listening to event '${name}'`,
|
||||
});
|
||||
return loadedEvents++;
|
||||
|
||||
return event;
|
||||
});
|
||||
};
|
||||
|
||||
for await (const eventName of eventNames) {
|
||||
eventNames.forEach(async (eventName) => {
|
||||
await importEvent(eventName);
|
||||
});
|
||||
});
|
||||
|
||||
if (loadedEvents === totalEvents) {
|
||||
return profiler.done({
|
||||
message: "Successfully listening to all events!",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
|
|
@ -6,25 +6,45 @@ import logger from "../../middlewares/logger";
|
|||
|
||||
// Start all jobs that are in the schedules directory
|
||||
export const start = async (client: Client) => {
|
||||
const jobNames = await checkDirectory("schedules");
|
||||
if (!jobNames) return logger.warn("⏰ No available jobs found");
|
||||
const profiler = logger.startTimer();
|
||||
|
||||
return await Promise.all(
|
||||
jobNames.map(async (jobName) => {
|
||||
await checkDirectory("schedules").then((jobNames) => {
|
||||
jobNames.forEach(async (jobName) => {
|
||||
await import(`../../schedules/${jobName}`)
|
||||
.then((job: IJob) => {
|
||||
return schedule.scheduleJob(job.options.schedule, async () => {
|
||||
logger.verbose(`⏰ Performed the job "${jobName}"`);
|
||||
await job.execute(client);
|
||||
schedule.scheduleJob(job.options.schedule, async () => {
|
||||
const jobProfiler = logger.startTimer();
|
||||
await job
|
||||
.execute(client)
|
||||
.then(() => {
|
||||
jobProfiler.done({
|
||||
message: `Successfully executed job '${jobName}'`,
|
||||
level: "debug",
|
||||
job,
|
||||
jobName,
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
jobProfiler.done({
|
||||
message: `Failed executing job '${jobName}'`,
|
||||
level: "debug",
|
||||
job,
|
||||
jobName,
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.warn({
|
||||
jobName,
|
||||
message: `Failed to start job ${jobName}`,
|
||||
message: `Failed to schedule job ${jobName}`,
|
||||
error,
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
return profiler.done({
|
||||
message: "Successfully scheduled all jobs!",
|
||||
});
|
||||
};
|
||||
|
|
|
@ -8,7 +8,7 @@ export default async (guild: Guild, user: User, amount: number) => {
|
|||
transactionRules(guild, user, amount);
|
||||
|
||||
// 2. Make the transaction.
|
||||
const recipient = await tx.guildMemberCredits.upsert({
|
||||
const recipient = await tx.guildMemberCredit.upsert({
|
||||
update: {
|
||||
balance: {
|
||||
increment: amount,
|
||||
|
|
|
@ -8,7 +8,7 @@ export default async (guild: Guild, user: User, amount: number) => {
|
|||
transactionRules(guild, user, amount);
|
||||
|
||||
// 2. Make the transaction.
|
||||
const recipient = await tx.guildMemberCredits.upsert({
|
||||
const recipient = await tx.guildMemberCredit.upsert({
|
||||
update: {
|
||||
balance: amount,
|
||||
},
|
||||
|
|
|
@ -8,7 +8,7 @@ export default async (guild: Guild, user: User, amount: number) => {
|
|||
transactionRules(guild, user, amount);
|
||||
|
||||
// 2. Make the transaction.
|
||||
const recipient = await tx.guildMemberCredits.upsert({
|
||||
const recipient = await tx.guildMemberCredit.upsert({
|
||||
update: {
|
||||
balance: {
|
||||
decrement: amount,
|
||||
|
|
|
@ -5,7 +5,7 @@ import transactionRules from "./transactionRules";
|
|||
export default async (guild: Guild, from: User, to: User, amount: number) => {
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
// 1. Decrement amount from the sender.
|
||||
const sender = await tx.guildMemberCredits.upsert({
|
||||
const sender = await tx.guildMemberCredit.upsert({
|
||||
update: {
|
||||
balance: {
|
||||
decrement: amount,
|
||||
|
@ -49,7 +49,7 @@ export default async (guild: Guild, from: User, to: User, amount: number) => {
|
|||
if (from.id === to.id) throw new Error("You can't transfer to yourself.");
|
||||
|
||||
// 7. Increment the recipient's balance by amount.
|
||||
const recipient = await tx.guildMemberCredits.upsert({
|
||||
const recipient = await tx.guildMemberCredit.upsert({
|
||||
update: {
|
||||
balance: {
|
||||
increment: amount,
|
||||
|
|
|
@ -79,7 +79,7 @@ export default async (
|
|||
},
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
return setTimeout(async () => {
|
||||
await prisma.cooldown.delete({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
|
|
|
@ -34,7 +34,7 @@ export const execute = async (client: Client, role: GuildShopRoles) => {
|
|||
if (!getGuildConfigShopRoles)
|
||||
throw new Error("Could not find guild config shop roles.");
|
||||
|
||||
const getGuildMemberCredits = await prisma.guildMemberCredits.findUnique({
|
||||
const getguildMemberCredit = await prisma.guildMemberCredit.findUnique({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId,
|
||||
|
@ -46,13 +46,13 @@ export const execute = async (client: Client, role: GuildShopRoles) => {
|
|||
},
|
||||
});
|
||||
|
||||
logger.silly(getGuildMemberCredits);
|
||||
logger.silly(getguildMemberCredit);
|
||||
|
||||
if (!getGuildMemberCredits) throw new Error("Could not find guild member.");
|
||||
if (!getguildMemberCredit) throw new Error("Could not find guild member.");
|
||||
|
||||
const pricePerHour = getGuildConfigShopRoles.pricePerHour;
|
||||
|
||||
if (getGuildMemberCredits.balance < pricePerHour) {
|
||||
if (getguildMemberCredit.balance < pricePerHour) {
|
||||
await rMember.roles
|
||||
.remove(roleId)
|
||||
.then(async () => {
|
||||
|
@ -79,7 +79,7 @@ export const execute = async (client: Client, role: GuildShopRoles) => {
|
|||
throw new Error("User does not have enough credits.");
|
||||
}
|
||||
|
||||
const createGuildMember = await prisma.guildMemberCredits.upsert({
|
||||
const createGuildMember = await prisma.guildMemberCredit.upsert({
|
||||
where: {
|
||||
userId_guildId: {
|
||||
userId,
|
||||
|
|
Loading…
Add table
Reference in a new issue