So I have this current code:
const mutualGuilds = bot.guilds.cache.filter((guild) => {
return guild.members.cache.has(message.author.id);
});
if (mutualGuilds.roles.cache.has("guildid")) {
if (mutualGuilds.users.cache.has(user.id)) {
let userInGuild = mutualGuilds.users.cache.find(user.id).roles.add("roleid");
}
}
And when I try the command it outputs: Cannot read property 'cache' of undefined
If someone could help that would be amazing since my bot is on release today.
If you use the .filter method to get the mutualGuilds, it will return a collection. You can only access the .roles property on a single guild, so you need to grab the .first() one for example, like this:
const mutualGuilds = bot.guilds.cache.filter((guild) => {
return guild.members.cache.has(message.author.id);
});
if (mutualGuilds.first().roles.cache.has("guildid")) {
// ...
Or you can use the .each() method to execute the provided function once for each mutual guild:
const mutualGuilds = bot.guilds.cache.filter((guild) => {
return guild.members.cache.has(message.author.id);
});
mutualGuilds.each((guild) => {
if (guild.roles.cache.has("guildid")) {
// ...
});
Related
I want to check if a member is or not in a server.
This is my current code:
function CheckIfManagerIsInServer(client, server_id, member) {
let isIn = { isIn: true, isNotIn: [] };
if (CheckIfIsInChain(server_id)) {
database.chains.forEach(chain => {
if(chain.membersID.includes(server_id)) {
chain.members.forEach(server => {
const guild = client.guilds.cache.get(server)
// check if member is in the server
})
}
})
}
return isIn;
}
member parameter is the id of the member.
How i can check if the member is or not in the server (guild)?
I'm using v13.5.0.
My bot is verified and it has GUILD_MEMBERS enabled.
I have tried multiple methods but they doesn't works.
How I can do?
Thank you in advance and sorry for bad english!
For checking that you can do like this
if (guild.members.cache.find(m => m.id === member)?.id) {
// true
} else {
// false
}
I have a route handler in which I am attempting to find a user's collection of club objects, then find the specific club object using Array.prototype.find(). I have no idea why it is always returning undefined. Can anyone see what I am doing wrong?
router.get('/club', async (req,res) => {
try {
console.log(req.query.club);
// 6008d7537ea5c22b61dc616b
const { clubs } = await User.findOne({ spotifyId: req.query.id }).populate('clubs').select('clubs');
console.log(clubs);
// [{"_id":"6008d7537ea5c22b61dc616b","name":"asfasfs","description":"sdfasfa","privacy":"private","frequency":"bi-weekly","__v":0},{"_id":"6008ec8026900630c24fd533","name":"asfasfdsasf","description":"asdfasfdsf","privacy":"private","frequency":"monthly","__v":0}]
let club = clubs.find(currentClub => (
currentClub._id === req.query.club
));
console.log(club)
// undefined
res.send(club)
}
catch {
return res.status(400).send('No club found with given id')
}
})
_id is an object
Change your code like this
String(currentClub._id) === req.query.club
or
currentClub._id.toString() === req.query.club
I want my bot to check every guild that it is in and create some roles when it sees that a guild doesn't have them.
My code looks like this:
let newRoles = {
'guildId': '',
'rank1': '', // the role id
'rank2': ''
}
client.guilds.forEach(guild => {
checkHasRoles(guild.id) // returns a Boolean
if (hasRoles === false) {
newRoles.guildId = guild.id
guild.roles.create({
data: {
name: 'rank1',
color: '992d22'
}
})
role = guild.roles.cache.find(role => role.name === 'rank1')
newRoles['rank1'] = role.id
// The same for the other roles
rolesArray.push(newRoles)
save(rolesArray, file)
rolesArray = load(file)
}
})
The error I get is: (node:11833) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined. I guess that there is a problem with calling the create() and find() function together but I can't think of a way to do this differently.
I'd be grateful if you could give me a detailed explaination. I'm new to this.
You have to use a promise for this instead of trying to find a new role because this will not work.
message.guild.roles.create({
data: {
name: 'rank1',
color: '992d22'
}
}).then(role => {
let roleId = role.id;
});
I am trying to query a value from a document in collection, in fire base, particularly "currentKilos":
I am using the below function to query it:
showKiols = () => {
db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').get().then.subscribe(value=> {
this.updatedKilos = value;
alert(this.updatedKilos);
})
}
However, I am getting attached error:
Also, I tried below function as well, but it is not working:
db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').valueChanges().subscribe(value => {
this.updatedKilos = value.currentKilos;
alert(this.updatedKilos)
})
FYI, this is the function to add currentKilos to firebase, and it is working fine:
updateKilos = () => {
db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').set({ currentKilos: this.state.currentKilos })
alert('Data Updated')
}
Appreciate your support.
I'm not sure where you get get().then.subscribe() from, but at the very least that then should be then((value)=>{...}).
Try the following:
showKiols = () => {
db.collection('users')
.doc(this.props.user.uid)
.collection('mainData')
.doc('currentKilos')
.get()
.then(value => {
this.updatedKilos = value.data();
alert(this.updatedKilos);
})
}
I'm looking a good and right way to set/ initalize values with http trigger.
what I did is ref to the node in firebase and get data then update it.
module.exports.initializeAnswers = functions.https.onRequest(async(req,res)=>{
try{
// i want to initalize each key
await firebase.ref('/CurrentGame').once('value',snapshot=>{
snapshot.forEach((childSnapshot)=>{
if(childSnapshot !=null){
childSnapshot.update(0)
}
return false
});
})
}catch(e){
console.info(e)
return res.status(400).send({error:0})
}
})
I'm looking for a right way and not by the 'update' function
I want to initalize each value to zero with http trigger
I understand that you will have several children (variable number) under the "answers" node and only one "rightAnswer" node. If my understanding is correct, the following code will do the trick:
exports.initializeAnswers = functions.https.onRequest((req, res) => {
admin.database().ref('/CurrentGame/answers').once('value', snapshot => {
const updates = {};
snapshot.forEach((child) => {
updates['/answers/' + child.key] = 0;
});
updates['/rightAnswer'] = 0;
return admin.database().ref('/CurrentGame').update(updates);
}).then(() => {
res.status(200).end();
}).catch((err) => {
console.log(err);
res.status(500).send(err);
});
});
You can use Firebase Cloud Functions to initialize the values on HTTP trigger. check this link