Skip to content

Commit

Permalink
Merge pull request #276 from nwplus/add-members-command
Browse files Browse the repository at this point in the history
add members + verify for hackers + other roles
  • Loading branch information
DonaldKLee authored Oct 24, 2024
2 parents ab4e366 + b20e421 commit dbebe1c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 50 deletions.
31 changes: 19 additions & 12 deletions commands/verification/add-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ class AddMembers extends Command {
const guild = interaction.guild;
const participantsType = interaction.options.getString('participantstype');
const overwrite = interaction.options.getBoolean('overwrite') ?? false;

const userRoles = guild.members.cache.get(userId).roles.cache;
const staffRoleID = this.initBotInfo.roleIDs.staffRole;
const adminRoleID = this.initBotInfo.roleIDs.adminRole;

if (!guild.members.cache.get(userId).roles.cache.has(this.initBotInfo.roleIDs.staffRole) && !guild.members.cache.get(userId).roles.cache.has(this.initBotInfo.roleIDs.adminRole)) {
return this.error({ message: 'You do not have permissions to run this command!', ephemeral: true })
if (!userRoles.has(staffRoleID) && !userRoles.has(adminRoleID)) {
return this.error({ message: 'You do not have permissions to run this command!', ephemeral: true });
}

if (!this.initBotInfo.verification.roles.find((r) => r.name === participantsType)) {
const roles = this.initBotInfo.verification.roles;
const roleExists = roles.some(role => role.name === participantsType);
if (!roleExists) {
await interaction.reply({ content: 'The role you entered does not exist!', ephemeral: true });
return;
}

console.log('PASSED AGAIN');
const modal = new Modal()
.setCustomId('emailsModal')
.setTitle('Enter all emails to be added as ' + participantsType)
Expand All @@ -61,14 +67,15 @@ class AddMembers extends Command {
.catch(error => {
});

if (submitted) {
const emailsRaw = submitted.fields.getTextInputValue('emails');
const emails = emailsRaw.split(/\r?\n|\r|\n/g);
emails.forEach(email => {
addUserData(email, participantsType, interaction.guild.id, overwrite);
});
submitted.reply({ content: emails.length + ' emails have been added as ' + participantsType, ephemeral: true })
}
if (submitted) {
const emailsRaw = submitted.fields.getTextInputValue('emails');
console.log(emailsRaw, ' is the raw emails');
const emails = emailsRaw.split(/[\r?\n|\r|\n|,]+/g).map(email => email.trim()).filter(Boolean);
emails.forEach(email => {
addUserData(email, participantsType, interaction.guild.id, overwrite);
});
submitted.reply({ content: emails.length + ' emails have been added as ' + participantsType, ephemeral: true })
}
}
}
module.exports = AddMembers;
126 changes: 88 additions & 38 deletions db/firebase/firebaseUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,72 +237,122 @@ module.exports = {
* @param {String} [lastName=''] - users last name
* @async
*/
async addUserData(email, type, guildId, overwrite) {
async addUserData(email, type, guildId, overwrite) {
const cleanEmail = email.trim().toLowerCase();
const documentRef = getFactotumDoc().collection('guilds').doc(guildId).collection('members').doc(cleanEmail);
const doc = await documentRef.get();

if (doc.exists && !overwrite) {
const querySnapshot = type === "hacker" ? await getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('applicants')
.where('email', '==', cleanEmail)
.limit(1)
.get()
: await getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('otherRoles')
.where('email', '==', cleanEmail)
.limit(1)
.get()

let docRef;

if (!querySnapshot.empty) {
const doc = querySnapshot.docs[0];
console.log(doc, ' is doc data');
docRef = doc.ref;

const types = doc.data().types || [];
const containsType = types.some(existingType => existingType.type === type);
if (!containsType) {
types.push({ type, isVerified: false });

if (!containsType || overwrite) {
if (!containsType) {
types.push({ type, isVerified: false });
}
await docRef.update({ types });
}
await documentRef.update({ types });
} else {
docRef = type=== "hacker" ? getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('applicants')
.doc()
: getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('otherRoles')
.doc();

const data = { email: cleanEmail, types: [{ isVerified: false, type }] };
await documentRef.set(data);
await docRef.set(data);
}
},

/**
* Verifies the any event member via their email.
* @param {String} email - the user email
* @param {String} id - the user's discord snowflake
* @param {String} guildId - the guild id
* @returns {Promise<String[]>} - the types this user is verified
* Verifies any event member via their email.
* @param {String} email - The user's email.
* @param {String} id - The user's Discord ID.
* @param {String} guildId - The guild ID.
* @returns {Promise<String[]>} - The types this user is verified for.
* @async
* @throws Error if the email provided was not found.
*/
async verify(email, id, guildId) {
let emailLowerCase = email.trim().toLowerCase();
let userRef = getFactotumDoc().collection('InitBotInfo').doc(guildId).collection('applicants').where('email', '==', emailLowerCase).limit(1);
let user = (await userRef.get()).docs[0];
let otherRolesRef = getFactotumDoc().collection('InitBotInfo').doc(guildId).collection('otherRoles').where('email', '==', emailLowerCase).limit(1);
let otherRolesUser = (await otherRolesRef.get()).docs[0];
if (user) {
const emailLowerCase = email.trim().toLowerCase();

const userRef = getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('applicants')
.where('email', '==', emailLowerCase)
.limit(1);
const userSnapshot = await userRef.get();
const user = userSnapshot.docs[0];

const otherRolesRef = getFactotumDoc()
.collection('InitBotInfo')
.doc(guildId)
.collection('otherRoles')
.where('email', '==', emailLowerCase)
.limit(1);
const otherRolesUserSnapshot = await otherRolesRef.get();
const otherRolesUser = otherRolesUserSnapshot.docs[0];

if (user || otherRolesUser) {
let returnTypes = [];
let data;

/** @type {FirebaseUser} */
let data = user.data();
if (!data?.isVerified) {
data.isVerified = true;
data.VerifiedTimestamp = admin.firestore.Timestamp.now();
data.discordId = id;
await user.ref.update(data);
returnTypes.push("hacker")
if (user) {
data = user.data();
} else {
data = otherRolesUser.data();
}

return returnTypes;
} else if (otherRolesUser) {
let returnTypes = [];
if (data?.types) {
data.types = data.types.map((role) => {
if (!role.isVerified) {
role.isVerified = true;
returnTypes.push(role.type);
}
return role;
});

/** @type {FirebaseUser} */
let data = otherRolesUser.data();
if (!data?.isVerified) {
data.isVerified = true;
data.VerifiedTimestamp = admin.firestore.Timestamp.now();
data.discordId = id;
await otherRolesUser.ref.update(data);
returnTypes.push(data?.role)

if (user) {
await user.ref.update(data);
} else if (otherRolesUser) {
await otherRolesUser.ref.update(data);
}
}

return returnTypes;
return returnTypes;
} else {
throw new Error('The email provided was not found!');
}
},


/**
* Attends the user via their discord id
* @param {String} id - the user's discord snowflake
Expand Down

0 comments on commit dbebe1c

Please sign in to comment.