sympact
sympact
SIASapphire - Imagine a framework
Created by sympact on 5/14/2024 in #sapphire-support
giveaway command problem
let me post
88 replies
SIASapphire - Imagine a framework
Created by sympact on 4/11/2024 in #sapphire-support
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;
}
}
5 replies