Skip to content

Commit

Permalink
Find user command
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Garcia <[email protected]>
  • Loading branch information
TiagoRG committed Sep 20, 2024
1 parent a70e097 commit 7e81457
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ const guild_commands = [
.addUserOption(option => option.setName('utilizador').setDescription('Utilizador a consultar').setRequired(true))
.setDefaultMemberPermissions(0),

new SlashCommandBuilder().setName('find-user').setDescription('Procura utilizadores')
.addStringOption(option => option.setName('query').setDescription('Query a procurar (número mecanográfico ou parte do nome)').setRequired(true))
.setDefaultMemberPermissions(0),

new ContextMenuCommandBuilder().setName('Completar faina').setType(ApplicationCommandType.User)
.setDefaultMemberPermissions(0).setDMPermission(false),

Expand Down
71 changes: 71 additions & 0 deletions src/discord/commands/find-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { EmbedBuilder, type ChatInputCommandInteraction } from "discord.js";
import { Command } from "../registry/Command";
import Database from "../../Database";
import Constants from "../../Constants";

export default class FindUserCommand extends Command {
constructor() {
super("find-user", "Procura utilizadores");
}

private static readonly MAX_USERS = 8;

public async execute(interaction: ChatInputCommandInteraction) {
await interaction.deferReply({ ephemeral: true });

const query = interaction.options.getString("query", true);

const users = await Database.getAll("SELECT * FROM Users WHERE Nome LIKE ? OR NMec = ? OR NomeDeFaina LIKE ?", [`%${query}%`, query, `%${query}%`]);
users.sort((a: any, b: any) => (a.NMec as number) - (b.NMec as number));


if (users.length === 0) {
await interaction.editReply({ content: "Não foi possível encontrar nenhum utilizador." });
return;
}

const embedList = [];

embedList.push(new EmbedBuilder()
.setTitle("Utilizadores encontrados")
.setColor(Constants.EMBED_COLORS.ACCEPTED));

let userCount = 0;

for (const user of users) {
const discordUser = await interaction.guild?.members.fetch(user.DiscordID).catch(() => null);

if (!discordUser) {
continue;
}

if (userCount >= FindUserCommand.MAX_USERS) {
embedList.push(new EmbedBuilder().setColor(Constants.EMBED_COLORS.ACCEPTED));
userCount = 0;
}

embedList[embedList.length-1].addFields({
name: "Discord",
value: discordUser.user.toString(),
inline: true
}).addFields({
name: "Nome",
value: user.Nome,
inline: true
}).addFields({
name: "Número mecanográfico",
value: user.NMec,
inline: true
});

userCount++;
}

embedList[embedList.length-1].setFooter({
text: `Dados pedidos por ${interaction.user.tag} (${interaction.user.id})`,
iconURL: interaction.user.displayAvatarURL()
}).setTimestamp(Date.now());

await interaction.editReply({ embeds: embedList });
}
}

0 comments on commit 7e81457

Please sign in to comment.