run = async (interaction) => {
if (!interaction.guild) return
if (!interaction) return
const guildDB = await Guild.findById(interaction.guildId.toString()) || new Guild({ _id: interaction.guildId.toString() });
if (interaction.type === InteractionType.ApplicationCommand) {
const cmd = this.client.slashCommands.find(c => c.name == interaction.commandName)
if (cmd) {
if (cmd.requireDatabase) {
interaction.guild.db = guildDB
}
cmd.run(interaction)
}
} else if (interaction.type == InteractionType.ModalSubmit) {
if (interaction.customId.startsWith('wl_send_')) {
const recruiterId = interaction.customId.split('_')[2];
const name = interaction.fields.getField('name').value
const id = parseInt(interaction.fields.getField('id').value)
const whatsapp = interaction.fields.getField('whatsapp').value
const cargo = interaction.fields.getField('cargo').value
let userSchema = guildDB.users.find((u) => u._id === recruiterId);
if (!userSchema) {
userSchema = new User({ _id: recruiterId });
guildDB.users.push(userSchema);
await guildDB.save();
userSchema = guildDB.users.find((u) => u._id === recruiterId)
}
userSchema.recrutamentos = (userSchema.recrutamentos || 0) + 1
userSchema.recrutamentos_total = (userSchema.recrutamentos_total || 0) + 1
const embed = new EmbedBuilder()
.setAuthor({ name: `${interaction.member.displayName}`, iconURL: interaction.user.displayAvatarURL({ dynamic: true }) })
.setThumbnail(interaction.guild.iconURL())
.setTitle('Novo usuário recrutado')
.setColor('#2B2D31')
.setTimestamp()
.addFields(
{ name: `Usuário recrutado`, value: `${interaction.user.toString()}`, inline: true },
{ name: `Recrutador`, value: `<@${recruiterId}> (${userSchema.recrutamentos_total} recrutamentos)`, inline: true },
{ name: ``, value: ``, inline: true },
{ name: `Nome`, value: `\`${name}\``, inline: false },
{ name: `Cargo`, value: `\`${cargo}\``, inline: false },
{ name: `ID`, value: `\`${id}\``, inline: false },
{ name: `WhatsApp`, value: `\`${whatsapp}\``, inline: false },
)
await guildDB.save()
await interaction.guild.channels.cache.get(config.resultChannel).send({ embeds: [embed] })
interaction.update({ content: `Registro enviado.`, embeds: [], components: [] });
}
} else if (interaction.type === InteractionType.MessageComponent) {
if (interaction.customId == 'select_user') {
const recruiterId = interaction.values[0]
const modal = new ModalBuilder()
.setCustomId(`wl_send_${recruiterId}`)
.setTitle('Preencha os dados')
.addComponents(
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('name')
.setLabel('Nick')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: Cauã')
.setRequired(true)
.setMaxLength(25),
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('id')
.setLabel('Qual seu id')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: 975')
.setMaxLength(10)
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('whatsapp')
.setLabel('Qual seu WhatsApp?')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: (00) 12345-6789')
.setMaxLength(23),
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('cargo')
.setLabel('Qual cargo você irá entrar?')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: Morador e Cria')
.setMaxLength(30),
),
)
interaction.showModal(modal)
} else if (interaction.customId == 'startREC') {
const recrutadores = interaction.guild.roles.cache.get(config.recruiterRole).members.map(m => m)
if (recrutadores.length == 0) return interaction.reply({ content: 'Não há nenhum recrutador atualmente', ephemeral: true })
interaction.reply({
content: 'Selecione o recrutador', components: [
new ActionRowBuilder()
.addComponents(new StringSelectMenuBuilder()
.setCustomId(`select_user`)
.setPlaceholder('Selecione um recrutador')
.addOptions(recrutadores.map(x =>
new StringSelectMenuOptionBuilder()
.setLabel(x.displayName || '')
.setValue(`${x.id}`)
))
)
], ephemeral: true
})
}
}
}
const Event = require('../../structures/Event');
const { ActionRowBuilder, StringSelectMenuBuilder, EmbedBuilder, InteractionType, ModalBuilder, TextInputStyle, TextInputBuilder, StringSelectMenuOptionBuilder } = require('discord.js');
const config = require('../../../config');
const { Guild, User } = require('../../database/models/Guild');
module.exports = class extends Event {
constructor(client) {
super(client, {
name: 'interactionCreate'
});
}
run = async (interaction) => {
if (!interaction.guild) return;
if (!interaction) return;
const guildDB = await Guild.findById(interaction.guildId.toString()) || new Guild({ _id: interaction.guildId.toString() });
if (interaction.type === InteractionType.ApplicationCommand) {
const cmd = this.client.slashCommands.find(c => c.name == interaction.commandName);
if (cmd) {
if (cmd.requireDatabase) {
interaction.guild.db = guildDB;
}
cmd.run(interaction);
}
} else if (interaction.type == InteractionType.ModalSubmit) {
if (interaction.customId.startsWith('wl_send_')) {
const recruiterId = interaction.customId.split('_')[2];
const name = interaction.fields.getTextInputValue('name');
const id = interaction.fields.getTextInputValue('id'); // Mantive como string para preservar possíveis caracteres
const whatsapp = interaction.fields.getTextInputValue('whatsapp');
const cargo = interaction.fields.getTextInputValue('cargo');
let userSchema = guildDB.users.find((u) => u._id === recruiterId);
if (!userSchema) {
userSchema = new User({ _id: recruiterId });
guildDB.users.push(userSchema);
await guildDB.save();
userSchema = guildDB.users.find((u) => u._id === recruiterId);
}
userSchema.recrutamentos = (userSchema.recrutamentos || 0) + 1;
userSchema.recrutamentos_total = (userSchema.recrutamentos_total || 0) + 1;
// Definir o nickname do usuário
await interaction.member.setNickname(name).catch(console.error);
// Atribuir um cargo específico ao usuário
await interaction.member.roles.add(config.specificRole).catch(console.error);
const embed = new EmbedBuilder()
.setAuthor({ name: `${interaction.member.displayName}`, iconURL: interaction.user.displayAvatarURL({ dynamic: true }) })
.setThumbnail(interaction.guild.iconURL())
.setTitle('Novo usuário recrutado')
.setColor('#2B2D31')
.setTimestamp()
.addFields(
{ name: `Usuário recrutado`, value: `${interaction.user.toString()}`, inline: true },
{ name: `Recrutador`, value: `${recruiterId} (${userSchema.recrutamentos_total} recrutamentos)`, inline: true },
{ name: ``, value: ``, inline: true },
{ name: `Nome`, value: `\`${name}\``, inline: false },
{ name: `Cargo`, value: `\`${cargo}\``, inline: false },
{ name: `ID`, value: `\`${id}\``, inline: false },
{ name: `WhatsApp`, value: `\`${whatsapp}\``, inline: false },
);
await guildDB.save();
await interaction.guild.channels.cache.get(config.resultChannel).send({ embeds: [embed] });
interaction.update({ content: `Registro enviado.`, embeds: [], components: [] });
}
} else if (interaction.type === InteractionType.MessageComponent) {
if (interaction.customId == 'select_user') {
const recruiterId = interaction.values[0];
const modal = new ModalBuilder()
.setCustomId(`wl_send_${recruiterId}`)
.setTitle('Preencha os dados')
.addComponents(
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('name')
.setLabel('Nick')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: Cauã')
.setRequired(true)
.setMaxLength(25),
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('id')
.setLabel('Qual seu id')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: 975')
.setMaxLength(10)
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('whatsapp')
.setLabel('Qual seu WhatsApp?')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: (00) 12345-6789')
.setMaxLength(23),
),
new ActionRowBuilder()
.addComponents(
new TextInputBuilder()
.setCustomId('cargo')
.setLabel('Qual cargo você irá entrar?')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Exemplo: Morador e Cria')
.setMaxLength(30),
),
);
interaction.showModal(modal);
} else if (interaction.customId == 'startREC') {
const recrutadores = interaction.guild.roles.cache.get(config.recruiterRole).members.map(m => m);
if (recrutadores.length == 0) return interaction.reply({ content: 'Não há nenhum recrutador atualmente', ephemeral: true });
interaction.reply({
content: 'Selecione o recrutador', components: [
new ActionRowBuilder()
.addComponents(new StringSelectMenuBuilder()
.setCustomId(`select_user`)
.setPlaceholder('Selecione um recrutador')
.addOptions(recrutadores.map(x =>
new StringSelectMenuOptionBuilder()
.setLabel(x.displayName || '')
.setValue(`${x.id}`)
))
)
], ephemeral: true
});
}
}
}
}
const { Schema, model } = require('mongoose');
const userSchema = new Schema({
_id: { type: String, required: true },
recrutamentos_total: { type: Number, default: 0 },
recrutamentos: { type: Number, default: 0 }
});
const guildSchema = new Schema({
_id: { type: String, required: true }, // ID do servidor
users: [userSchema], // Informações dos usuários
});
const Guild = model('guilds', guildSchema);
const User = model('users', userSchema);
module.exports = { Guild, User };