extremeley weird issue with closing DM's between bot and user

I've been trying to solve this for the past 3 days catuncanny 1. I have a function which makes the bot question the user on DM's 2. In order to prevent having users be questioned about 2 things at once, the function checks for an open DM channel with the user before starting, and also closes the channel with channel.delete() once the questioning process is done 3. I have 2 commands that use this function 4. Command A executes 100% properly 5. Although when i execute command B first, and then try executing A, the function will not fire even though it should have closed the previous DM channel
2 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
Lipton
LiptonOP17mo ago
using command A to trigger the function works exactly as it should, while if i execute B first, none of the commands will fire after that due to there being an open DM with the bot, and the user who triggered the command which is very weird because those 2 commands use literally the same module and call the same function, with almost identical arguments it cant be the last thing as the function is getting triggered by the commands just so its clear im having one function, that can be triggered by two different commands i know, one second
async function question(interaction, questiontab) {

if (interaction.user.dmChannel) {

interaction.reply(`Questioning can't proceed while you have another DM channel open with me.`)

console.log(interaction.user.dmChannel);

throw `${interaction.user.tag} attempted to start 2 questionings at once.`;

}

else {

interaction.reply(`Questioning process initiated on DM's. You may cancel it by saying 'cancel' at any time.`)

channel = await interaction.user.createDM();

const even = (element) => element === 'cancel';
const questions = questiontab;
let userapp = [];


const filter = m => m.author.id === interaction.user.id;

for (var i of questions) {

interaction.user.send(i);

try {
let collected = await channel.awaitMessages({ filter, max: 1, time: 240000, errors: ['time'] })
userapp.push(collected.first().content);
}
catch (error) {

interaction.user.send(`You have exceeded maximum answer time.`);
channel.delete();
throw `${interaction.user.tag} has exceeded maximum answer time.`;

}
if (userapp.some(even)) {

interaction.user.send('Questioning cancelled');
channel.delete();
throw `${interaction.user.tag} has cancelled his questioning process.`;

}

}

channel.delete();
return userapp;
}
}
module.exports = question;
async function question(interaction, questiontab) {

if (interaction.user.dmChannel) {

interaction.reply(`Questioning can't proceed while you have another DM channel open with me.`)

console.log(interaction.user.dmChannel);

throw `${interaction.user.tag} attempted to start 2 questionings at once.`;

}

else {

interaction.reply(`Questioning process initiated on DM's. You may cancel it by saying 'cancel' at any time.`)

channel = await interaction.user.createDM();

const even = (element) => element === 'cancel';
const questions = questiontab;
let userapp = [];


const filter = m => m.author.id === interaction.user.id;

for (var i of questions) {

interaction.user.send(i);

try {
let collected = await channel.awaitMessages({ filter, max: 1, time: 240000, errors: ['time'] })
userapp.push(collected.first().content);
}
catch (error) {

interaction.user.send(`You have exceeded maximum answer time.`);
channel.delete();
throw `${interaction.user.tag} has exceeded maximum answer time.`;

}
if (userapp.some(even)) {

interaction.user.send('Questioning cancelled');
channel.delete();
throw `${interaction.user.tag} has cancelled his questioning process.`;

}

}

channel.delete();
return userapp;
}
}
module.exports = question;
thats pretty much the whole questioning function in the for loop? i dont see how would it affect my problem, its not an issue with the loop (i replaced it) nowhere i think, its only there to iterate through questions which is an array its value is catuncanny about the actual issue though yes i have it points to the previously used channel i just removed it here xd command A
case 'server':{

const funkcja = require("../../library/questioning");

questions = [`A`,`B`,`C`];

funkcja(interaction, questions).then((answers)=>{

let query = 'INSERT INTO servers VALUES (?);';
let values = [answers[0], answers[1], answers[5], answers[3], answers[2], answers[4]];

database.query(query, [values], (err, rows, fields) => {
if (err) console.log(err);
});

return interaction.user.send(`Success. Server ${answers[0]} has been registered in the database.`);

})
.catch((error)=>{
console.log(error);
})
break;
}
case 'server':{

const funkcja = require("../../library/questioning");

questions = [`A`,`B`,`C`];

funkcja(interaction, questions).then((answers)=>{

let query = 'INSERT INTO servers VALUES (?);';
let values = [answers[0], answers[1], answers[5], answers[3], answers[2], answers[4]];

database.query(query, [values], (err, rows, fields) => {
if (err) console.log(err);
});

return interaction.user.send(`Success. Server ${answers[0]} has been registered in the database.`);

})
.catch((error)=>{
console.log(error);
})
break;
}
i know, but it doesnt cause any problems. I can execute command A, the function will start and the bot will begin asking me questions on DM's, and if i try to execute command A before finishing or cancelling the first questioning, the program wont let me once i finish or cancel i will be able to execute it again (i think im gonna paste B onto pastebin or something because its a lengthy eyesore) so i should
await channel.delete();
return userapp;
await channel.delete();
return userapp;
? in the function i tried it before and it didnt help lol should i delete the channel after every interaction.user.send(…)? actually wouldnt i have to do that every time i dm someone, so the system doesnt break yeah im starting to think this may not be the optimal solution okay you were right its fixed now and i will definetely try this thanks a lot^^
Want results from more Discord servers?
Add your server