简体   繁体   English

如何为特定公会中的用户添加角色

[英]How to add a role to a user in a specific guild

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当我尝试它输出的命令时: 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.如果您使用.filter方法获取mutualGuilds ,它将返回一个集合。 You can only access the .roles property on a single guild, so you need to grab the .first() one for example, like this:您只能在单个公会上访问.roles属性,因此您需要获取 .first .first()例如,如下所示:

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:或者您可以使用.each()方法为每个互助行会执行一次提供的 function:

const mutualGuilds = bot.guilds.cache.filter((guild) => {
  return guild.members.cache.has(message.author.id);
});

mutualGuilds.each((guild) => {
  if (guild.roles.cache.has("guildid")) {
  // ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM