mobile update
This commit is contained in:
+104
-83
@@ -59,99 +59,120 @@ async function selectMenuHandler(interaction) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function modalSubmitHandler(interaction) {
|
||||
if (interaction.customId === 'wgeBaseModal') {
|
||||
const db = mClient.db('neo-db')
|
||||
const wgeColl = db.collection('timer_wge')
|
||||
const participateColl = db.collection('items_wge')
|
||||
if (interaction.customId !== 'wgeBaseModal') return;
|
||||
|
||||
// Get the data entered by the user
|
||||
const db = mClient.db('neo-db');
|
||||
const wgeColl = db.collection('timer_wge');
|
||||
const participateColl = db.collection('items_wge');
|
||||
const historyWgeColl = db.collection('history_wge');
|
||||
|
||||
var wgeAssignedUser = {}
|
||||
wgeAssignedUser.id = interaction.components[0].component.values[0]
|
||||
const assigningUser = interaction.user;
|
||||
const selectedUserId = interaction.components[0].component.values[0];
|
||||
const topic = interaction.fields.getTextInputValue('wgeTopicInput');
|
||||
const defaultAssignedTime = 7 * 24 * 60 * 60 * 1000; // 7 days
|
||||
|
||||
const wgeAssigningUser = interaction.user
|
||||
if (wgeAssignedUser.id === process.env.D_ID) {
|
||||
// Randomizer Function
|
||||
const randomParticipant = await participateColl.aggregate([
|
||||
{ $match: { status: true } },
|
||||
{ $sample: { size: 1 } }
|
||||
]).toArray()
|
||||
// Get current running WGE so we can exclude the current expert
|
||||
const prevWge = await wgeColl.findOne({
|
||||
debug: false,
|
||||
state: 'running'
|
||||
});
|
||||
|
||||
if (!randomParticipant) { return interaction.followUp({ content: 'No User is participating!' }) }
|
||||
let assignedUser;
|
||||
|
||||
random = await interaction.guild.members.fetch(randomParticipant[0].userID)
|
||||
wgeAssignedUser = random.user
|
||||
} else {
|
||||
// validate non-random user
|
||||
const foundParticipant = await participateColl.findOne({
|
||||
$and: [
|
||||
{ assignedUser: wgeAssignedUser.id },
|
||||
{ status: true}
|
||||
]
|
||||
})
|
||||
|
||||
if(!foundParticipant || foundParticipant == null){ return interaction.reply({
|
||||
content: `<@${wgeAssignedUser.id}> is not participating!`,
|
||||
flags: MessageFlags.Ephemeral
|
||||
})}
|
||||
}
|
||||
|
||||
|
||||
const prevWge = await wgeColl.findOne({
|
||||
$and: [
|
||||
{ debug: false },
|
||||
{ state: 'running' }
|
||||
]
|
||||
})
|
||||
|
||||
if (prevWge != null) {
|
||||
// Save previous run in history_wge
|
||||
const historyWgeColl = db.collection('history_wge')
|
||||
await historyWgeColl.insertOne({
|
||||
assignedUser: prevWge.assignedUser,
|
||||
assigningUser: prevWge.assigningUser,
|
||||
timeRemaining: prevWge.timeRemaining,
|
||||
topic: prevWge.topic,
|
||||
debug: prevWge.debug,
|
||||
createdAt: prevWge.createdAt
|
||||
});
|
||||
|
||||
console.log({
|
||||
message: 'saved previous wge in history_wge',
|
||||
data: prevWge
|
||||
})
|
||||
|
||||
await wgeColl.deleteOne({
|
||||
_id: prevWge._id
|
||||
})
|
||||
}
|
||||
|
||||
const wgeAssignedTopic = interaction.fields.getTextInputValue('wgeTopicInput');
|
||||
|
||||
const wgeDefaultAssignedTime = 7 * 24 * 60 * 60 * 1000; // 7 days in ms
|
||||
|
||||
await wgeColl.insertOne({
|
||||
assignedUser: wgeAssignedUser.id,
|
||||
assigningUser: wgeAssigningUser.id,
|
||||
|
||||
startedAt: Date.now(),
|
||||
timeRemaining: wgeDefaultAssignedTime,
|
||||
|
||||
state: 'running',
|
||||
|
||||
notifications: {
|
||||
half: false,
|
||||
day24: false,
|
||||
complete: false
|
||||
// Random assignment
|
||||
if (selectedUserId === process.env.D_ID) {
|
||||
const randomParticipant = await participateColl.aggregate([
|
||||
{
|
||||
$match: {
|
||||
status: true,
|
||||
...(prevWge && {
|
||||
userID: { $ne: prevWge.assignedUser }
|
||||
})
|
||||
}
|
||||
},
|
||||
{ $sample: { size: 1 } }
|
||||
]).toArray();
|
||||
|
||||
topic: wgeAssignedTopic,
|
||||
debug: false,
|
||||
if (randomParticipant.length === 0) {
|
||||
return interaction.reply({
|
||||
content: 'There are no eligible participants available.',
|
||||
flags: MessageFlags.Ephemeral
|
||||
});
|
||||
}
|
||||
|
||||
createdAt: new Date()
|
||||
const member = await interaction.guild.members.fetch(randomParticipant[0].userID);
|
||||
assignedUser = member.user;
|
||||
}
|
||||
// Manual assignment
|
||||
else {
|
||||
const participant = await participateColl.findOne({
|
||||
userID: selectedUserId,
|
||||
status: true
|
||||
});
|
||||
|
||||
return interaction.reply({ content: `<@${wgeAssignedUser.id}> is the next expert! Their topic is **${wgeAssignedTopic}**` })
|
||||
if (!participant) {
|
||||
return interaction.reply({
|
||||
content: `<@${selectedUserId}> is not participating!`,
|
||||
flags: MessageFlags.Ephemeral
|
||||
});
|
||||
}
|
||||
|
||||
if (prevWge && selectedUserId === prevWge.assignedUser) {
|
||||
return interaction.reply({
|
||||
content: `<@${selectedUserId}> was the previous expert and cannot be selected again this round.`,
|
||||
flags: MessageFlags.Ephemeral
|
||||
});
|
||||
}
|
||||
|
||||
assignedUser = { id: selectedUserId };
|
||||
}
|
||||
|
||||
// Archive previous WGE
|
||||
if (prevWge) {
|
||||
await historyWgeColl.insertOne({
|
||||
assignedUser: prevWge.assignedUser,
|
||||
assigningUser: prevWge.assigningUser,
|
||||
timeRemaining: prevWge.timeRemaining,
|
||||
topic: prevWge.topic,
|
||||
debug: prevWge.debug,
|
||||
createdAt: prevWge.createdAt
|
||||
});
|
||||
|
||||
console.log({
|
||||
message: 'Saved previous WGE to history_wge',
|
||||
data: prevWge
|
||||
});
|
||||
|
||||
await wgeColl.deleteOne({
|
||||
_id: prevWge._id
|
||||
});
|
||||
}
|
||||
|
||||
// Create new WGE
|
||||
await wgeColl.insertOne({
|
||||
assignedUser: assignedUser.id,
|
||||
assigningUser: assigningUser.id,
|
||||
|
||||
startedAt: Date.now(),
|
||||
timeRemaining: defaultAssignedTime,
|
||||
|
||||
state: 'running',
|
||||
|
||||
notifications: {
|
||||
half: false,
|
||||
day24: false,
|
||||
complete: false
|
||||
},
|
||||
|
||||
topic,
|
||||
debug: false,
|
||||
|
||||
createdAt: new Date()
|
||||
});
|
||||
|
||||
return interaction.reply({
|
||||
content: `<@${assignedUser.id}> is the next expert! Their topic is **${topic}**`
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user