TypeScript Check

Hey. The code works but I am curious if there is any way to do a better coding job like cleaniness etc. Thanks in advance!
// © Sympact06


import { ApplyOptions } from '@sapphire/decorators';
import { Args, Command, CommandOptions } from '@sapphire/framework';
import { send } from '@sapphire/plugin-editable-commands';
import { Message, EmbedBuilder, User } from 'discord.js';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

@ApplyOptions<CommandOptions>({
description: 'Get the invite info of a user',
})
export class UserCommand extends Command {
public override async registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(command =>
command
.setName(this.name)
.setDescription(this.description)
.addUserOption(option =>
option
.setName('user')
.setDescription('The user to get the invite info of')
.setRequired(false)
)
);
}

public override async messageRun(message: Message, args: Args) {
const user = await args.pick('user').catch(() => message.author);
const embed = await this.generateInviteInfoEmbed(user);
return send(message, { embeds: [embed] });
}

public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const user = interaction.options.getUser('user') || interaction.user;
const embed = await this.generateInviteInfoEmbed(user);
return interaction.reply({ embeds: [embed] });
}

public override async contextMenuRun(interaction: Command.ContextMenuCommandInteraction) {
const user = interaction.targetId ? await interaction.client.users.fetch(interaction.targetId) : interaction.user;
const embed = await this.generateInviteInfoEmbed(user);
return interaction.reply({ embeds: [embed] });
}

private async generateInviteInfoEmbed(user: User) {
const invites = await prisma.invite.findMany({
where: { inviterId: user.id },
include: { usages: true },
});

let totalInvites = invites.reduce((count, invite) => count + invite.uses + invite.manualadded, 0);
const left = invites.reduce((count, invite) => count + invite.usages.filter((usage) => usage.leftAt !== null).length, 0);
const stillInServer = totalInvites - left;
const manual = invites.reduce((count, invite) => count + invite.manualadded, 0);

const embed = new EmbedBuilder()
.setTitle(`${user.tag}'s Invites`)
.addFields(
{ name: 'Total Invites', value: totalInvites.toString(), inline: true },
{ name: 'Members Left', value: left.toString(), inline: true },
{ name: 'Members Still In Server', value: stillInServer.toString(), inline: true },
{ name: 'Manual Added Invites', value: manual.toString(), inline: true }
)
.setColor('#3986E4');

return embed;
}
}
// © Sympact06


import { ApplyOptions } from '@sapphire/decorators';
import { Args, Command, CommandOptions } from '@sapphire/framework';
import { send } from '@sapphire/plugin-editable-commands';
import { Message, EmbedBuilder, User } from 'discord.js';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

@ApplyOptions<CommandOptions>({
description: 'Get the invite info of a user',
})
export class UserCommand extends Command {
public override async registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(command =>
command
.setName(this.name)
.setDescription(this.description)
.addUserOption(option =>
option
.setName('user')
.setDescription('The user to get the invite info of')
.setRequired(false)
)
);
}

public override async messageRun(message: Message, args: Args) {
const user = await args.pick('user').catch(() => message.author);
const embed = await this.generateInviteInfoEmbed(user);
return send(message, { embeds: [embed] });
}

public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const user = interaction.options.getUser('user') || interaction.user;
const embed = await this.generateInviteInfoEmbed(user);
return interaction.reply({ embeds: [embed] });
}

public override async contextMenuRun(interaction: Command.ContextMenuCommandInteraction) {
const user = interaction.targetId ? await interaction.client.users.fetch(interaction.targetId) : interaction.user;
const embed = await this.generateInviteInfoEmbed(user);
return interaction.reply({ embeds: [embed] });
}

private async generateInviteInfoEmbed(user: User) {
const invites = await prisma.invite.findMany({
where: { inviterId: user.id },
include: { usages: true },
});

let totalInvites = invites.reduce((count, invite) => count + invite.uses + invite.manualadded, 0);
const left = invites.reduce((count, invite) => count + invite.usages.filter((usage) => usage.leftAt !== null).length, 0);
const stillInServer = totalInvites - left;
const manual = invites.reduce((count, invite) => count + invite.manualadded, 0);

const embed = new EmbedBuilder()
.setTitle(`${user.tag}'s Invites`)
.addFields(
{ name: 'Total Invites', value: totalInvites.toString(), inline: true },
{ name: 'Members Left', value: left.toString(), inline: true },
{ name: 'Members Still In Server', value: stillInServer.toString(), inline: true },
{ name: 'Manual Added Invites', value: manual.toString(), inline: true }
)
.setColor('#3986E4');

return embed;
}
}
1 Reply
Favna
Favna5mo ago
FYI https://chat.openai.com/ exists in anno 2024 👀 "can this code be improved" and dump code anyway code looks good the only thing that's a bit of an eyebrow raiser is that prisma constant at the top. You're supposed to only create 1 prisma instance for the whole application, not 1 per command. What is the common practise is to attach the instance to container so you can use it everywhere https://sapphirejs.dev/docs/Guide/additional-information/using-and-extending-container
Want results from more Discord servers?
Add your server