Skip to content

Commit

Permalink
fixed save state in verification
Browse files Browse the repository at this point in the history
  • Loading branch information
DonaldKLee committed Nov 2, 2024
1 parent fd243fd commit 03cbda0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 91 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Previously known as nwPlus Discord Bot, Factotum started as a Discord bot to sup
## Set up the bot
At the moment, the bot is still in development, but IT CAN BE USED. Please email me at juapgarc@gmail.com or reach out to me on discord JPGarcia99#8803 to talk about using the bot in its current state! If you would like to test the bot that is also a possibility, just reach out!

```
npm install
npm run dev
```

## How does Factotum support hackathon teams and events?
Factotum brings a lot of features that would traditionally happen at in-person hacakthons all over the word to the Discord platform.

Expand Down
174 changes: 83 additions & 91 deletions commands/verification/start-verification.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Required imports
const { Command } = require('@sapphire/framework');
const { MessageEmbed, Modal, MessageActionRow, MessageButton, TextInputComponent } = require('discord.js');
const firebaseUtil = require('../../db/firebase/firebaseUtil');
const { Message, MessageEmbed, Modal, MessageActionRow, MessageButton, TextInputComponent } = require('discord.js');
const { discordLog } = require('../../discord-services');

/**
* Verification Command
* Starts a verification process in the landing channel.
*/
class StartVerification extends Command {
constructor(context, options) {
super(context, {
Expand All @@ -16,53 +21,77 @@ class StartVerification extends Command {
builder
.setName(this.name)
.setDescription(this.description)
),
{
idHints: '1060545714133938309'
};
);
}

async chatInputRun(interaction) {
this.initBotInfo = await firebaseUtil.getInitBotInfo(interaction.guild.id);
const guild = interaction.guild;
const userId = interaction.user.id;
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)) {
interaction.reply({ content: 'You do not have permissions to run this command!', ephemeral: true });
return;
}
if (!this.initBotInfo.verification.isEnabled) {
await interaction.reply({ content: 'Verification has not been enabled!', ephemeral: true});
return;
}
const { guild, user } = interaction;

const embed = new MessageEmbed()
.setTitle(`Please click the button below to check-in to the ${interaction.guild.name} server! Make sure you know which email you used to apply to ${interaction.guild.name}!`);
// modal timeout warning?
if (!guild.members.cache.get(user.id).roles.cache.has(this.initBotInfo.roleIDs.staffRole)) {
return interaction.reply({ content: 'You do not have permissions to run this command!', ephemeral: true });
}

const embed = new MessageEmbed().setTitle(`Please click the button below to check-in to the ${interaction.guild.name} server! Make sure you know which email you used to apply to ${interaction.guild.name}!`);
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('verify')
.setLabel('Check-in')
.setStyle('PRIMARY'),
);

interaction.reply({ content: 'Verification started!', ephemeral: true });
const msg = await interaction.channel.send({ content: 'If you have not already, make sure to enable DMs, emojis, and embeds/link previews in your personal Discord settings! If you have any issues, please find an organizer!', embeds: [embed], components: [row] });
const msg = await interaction.channel.send({
content: 'If you have not already, make sure to enable DMs, emojis, and embeds/link previews in your personal Discord settings! If you have any issues, please find an organizer!',
embeds: [embed],
components: [row]
});

this.listenToVerification(guild, msg);

await firebaseUtil.getSavedMessagesSubCol(interaction.guild.id).doc('startverification').set({
messageId: msg.id,
channelId: msg.channel.id,
});
}

async tryRestoreReactionListeners(guild) {
const savedMessagesCol = firebaseUtil.getSavedMessagesSubCol(guild.id);
const verificationDoc = await savedMessagesCol.doc('startverification').get();

const checkInCollector = msg.createMessageComponentCollector({ filter: i => !i.user.bot});
if (verificationDoc.exists) {
const { messageId, channelId } = verificationDoc.data();
const channel = await this.container.client.channels.fetch(channelId);

// console.log(this.botGuild.verification.guestRoleID)
// console.log(this.botGuild.verification)
if (channel) {
try {
const message = await channel.messages.fetch(messageId);
this.listenToVerification(guild, message);
} catch (e) {
console.error('Failed to fetch verification message:', e);
}
}
}
}

listenToVerification(guild, msg) {
const collector = msg.createMessageComponentCollector({ filter: i => i.customId === 'verify' && !i.user.bot });

checkInCollector.on('collect', async i => {
if (!interaction.guild.members.cache.get(i.user.id).roles.cache.has(this.initBotInfo.verification.guestRoleID)) {
await i.reply({ content: 'You are not eligible to be checked in! If you don\'t have correct access to the server, please contact an organizer.', ephemeral: true});
collector.on('collect', async i => {
const member = guild.members.cache.get(i.user.id);
this.initBotInfo = await firebaseUtil.getInitBotInfo(guild.id);

// Check if the user has the guest role
if (!member.roles.cache.has(this.initBotInfo.verification.guestRoleID)) {
await i.reply({ content: 'You are not eligible to be checked in! If you don\'t have correct access to the server, please contact an organizer.', ephemeral: true });
return;
}

const modal = new Modal()
.setCustomId('verifyModal')
.setTitle('Check-in to gain access to the server!')
.addComponents([
.addComponents(
new MessageActionRow().addComponents(
new TextInputComponent()
.setCustomId('email')
Expand All @@ -73,100 +102,63 @@ class StartVerification extends Command {
.setPlaceholder('Email Address')
.setRequired(true),
),
]);
);

await i.showModal(modal);

const submitted = await i.awaitModalSubmit({ time: 300000, filter: j => j.user.id === i.user.id })
.catch(error => {
});
const submitted = await i.awaitModalSubmit({ time: 300000, filter: j => j.user.id === i.user.id }).catch(console.error);

if (submitted) {
const email = submitted.fields.getTextInputValue('email');
let types;

try {
types = await firebaseUtil.verify(email, submitted.user.id, submitted.guild.id);
} catch {
submitted.reply({ content: 'Your email could not be found! Please try again or ask an admin for help.', ephemeral: true });
discordLog(interaction.guild, `VERIFY FAILURE : <@${submitted.user.id}> Verified email: ${email} but was a failure, I could not find that email!`);
discordLog(guild, `VERIFY FAILURE : <@${submitted.user.id}> Verified email: ${email} but was a failure, I could not find that email!`);
return;
}

if (types.length === 0) {
submitted.reply({ content: 'You have already verified!', ephemeral: true });
discordLog(interaction.guild, `VERIFY WARNING : <@${submitted.user.id}> Verified email: ${email} but they are already verified for all types!`);
discordLog(guild, `VERIFY WARNING : <@${submitted.user.id}> Verified email: ${email} but they are already verified for all types!`);
return;
}

let correctTypes = [];
types.forEach(type => {
console.log(type, ' is the TYPE');
const roleObj = this.initBotInfo.verification.roles.find(role => role.name === type);
if (roleObj || type === 'staff' || type === 'mentor') {
const member = interaction.guild.members.cache.get(submitted.user.id);
let roleId;
if (type === 'staff') {
roleId = this.initBotInfo.roleIDs.staffRole;
} else if (type === 'mentor') {
roleId = this.initBotInfo.roleIDs.mentorRole;
} else {
roleId = roleObj.roleId;
}
if (member && roleId) {
member.roles.add(roleId);

if (correctTypes.length === 0) {
member.roles.remove(this.initBotInfo.verification.guestRoleID);
member.roles.add(this.initBotInfo.roleIDs.memberRole);
}
correctTypes.push(type);
} else {
console.warn(`Could not add role: ${roleId} for type: ${type}`);
const member = guild.members.cache.get(submitted.user.id);
let roleId;

if (type === 'staff') {
roleId = this.initBotInfo.roleIDs.staffRole;
} else if (type === 'mentor') {
roleId = this.initBotInfo.roleIDs.mentorRole;
} else {
roleId = roleObj ? roleObj.roleId : null;
}

if (member && roleId) {
member.roles.add(roleId);

if (correctTypes.length === 0) {
member.roles.remove(this.initBotInfo.verification.guestRoleID);
member.roles.add(this.initBotInfo.roleIDs.memberRole);
}
correctTypes.push(type);
} else {
discordLog(interaction.guild, `VERIFY WARNING: <@${submitted.user.id}> was of type ${type} but I could not find that type!`);
console.warn(`Could not add role: ${roleId} for type: ${type}`);
}
});

if (correctTypes.length > 0) {
submitted.reply({ content: 'You have successfully verified as a ' + correctTypes.join(', ') + '!', ephemeral: true });
submitted.reply({ content: `You have successfully verified as a ${correctTypes.join(', ')}!`, ephemeral: true });
}
}
});

const savedMessagesCol = firebaseUtil.getSavedMessagesSubCol(interaction.guild.id);
await savedMessagesCol.doc('startverification').set({
messageId: msg.id,
channelId: msg.channel.id,
});
const mentorCaveDoc = await savedMessagesCol.doc('startverification').get();
if (!mentorCaveDoc.exists) return 'Saved messages doc for start verification cave does not exist';
}

/**
*
* @param {Guild} guild
*/
async tryRestoreReactionListeners(guild) {
const savedMessagesCol = firebaseUtil.getSavedMessagesSubCol(guild.id);
const reportDoc = await savedMessagesCol.doc('startverification').get();
if (reportDoc.exists) {
const { messageId, channelId } = reportDoc.data();
const channel = await this.container.client.channels.fetch(channelId);
if (channel) {
try {
/** @type {Message} */
const message = await channel.messages.fetch(messageId);
this.listenToReports(guild, message);
} catch (e) {
// message doesn't exist anymore
return e;
}
} else {
return 'Saved message channel does not exist';
}
} else {
return 'No existing saved message for start verification command';
}
}
}

module.exports = StartVerification;

0 comments on commit 03cbda0

Please sign in to comment.