I am confused about this
when calling a function from an object literal.
module.exports = {
name: 'quiz',
owner: '',
numOfQuestions: 0,
difficulty: '',
channel: {},
execute: async function (args, msg) {
this.channel = msg.channel.id;
this.owner = msg.author.id;
this.channel = msg.channel;
// axios.get('https://opentdb.com/api.php?amount=1&category=15&difficulty=easy');
console.log('Quiz started');
console.log(this.name);
this.channel.send('How many questions?').then(this.getNumOfQuestions);
},
getNumOfQuestions: function () {
console.log(this.name);
this.channel.awaitMessages(this.filterNumOfQuestions, { max: 1, time: 15000})
.then(collected => {
this.channel.send(`You asked for ${collected.first().content} questions.`);
})
.catch(collected => {
this.channel.send('You did not tell me how many questions you wanted. Ending the quiz');
});
},
Another file requires this and calls the execute method. But this in the getNumOfQuestions function is not the instance of the object literal, this.name
for example returns undefined. I am not sure why, please can someone explain this.