-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tiago Garcia <[email protected]>
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |