So Im trying to get a couple messages returned from a specific channel to get info about 'clans' that were registered before. However I can fetch the channel and all and then I can filter and loop through the returned messages but I cannot return the messages that I fetched no matter what. No Error no nothing but if you call .getClans from another file it always returns undefined.
module.exports.getClans = (guild, channelid) => {
var channel = guild.channels.cache.get(channelid);
channel.messages.fetch({ limit: 10 }).then(messages => {
messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);
return messages
}
)
}
I have even done things such as
module.exports.getClans = (guild, channelid) => {
var channel = guild.channels.cache.get(channelid);
channel.messages.fetch({ limit: 10 }).then(messages => {
messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);
var returned = []
messages.forEach((el) =>{
returned.push(el)
})
return returned;
}
)
}
which I checked in the module function itself returned was defined but calling the function again returned nothing other than undefined. Please help I have no clue how to fix this.
You aren't actually returning anything from the function, you are only returning from the .then(messages, ...). You will need to retrieve what is being fetched and then return.
module.exports.getClans = (guild, channelid) => {
var channel = guild.channels.cache.get(channelid);
//This return is returning from .getClans()
return channel.messages.fetch({ limit: 10 }).then(messages => {
messages = messages.filter(msg => msg.content && msg.content.startsWith("clan{") && msg.author.bot==true);
//This return is returning from inside the .then()
return messages
}
)
}