为什么我无法让我的不和谐机器人将用户添加到角色?

时间:2020-08-30 09:12:43

标签: javascript node.js discord discord.js bots

我一直在寻找答案,但似乎没有任何效果。也许有人可以帮我吗?

“!imprison1 @user垃圾邮件”应导致漫游器发送一条消息“用户已因垃圾邮件而被监禁”,并向该用户添加一个名为“囚犯”的角色。

const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();

client.once('ready', () => {
  console.log('Ready!');
});

function getUserFromMention(mention) {
  if (!mention) return;

  if (mention.startsWith('<@') && mention.endsWith('>')) {
    mention = mention.slice(2, -1);

    if (mention.startsWith('!')) {
      mention = mention.slice(1);
    }

    return client.users.cache.get(mention);
  }
}

function getUserFromMentionRegEx(mention) {
  const matches = mention.match(/^<@!?(\d+)>$/);
  const id = matches[1];

  return client.users.cache.get(id);
}

client.on('message', (message) => {
  if (!message.content.startsWith(config.prefix)) return;

  const withoutPrefix = message.content.slice(config.prefix.length);
  const split = withoutPrefix.split(/ +/);
  const command = split[0];
  const args = split.slice(1);

  if (command === 'imprison1') {
    if (args[0]) {
      let user = getUserFromMention(args[0]);

      if (!user) {
        return message.reply('Invalid user.');
      }

      return message.channel.send(
        `${user.username} has been imprisoned for ${args[1]}`
      );
      user.roles.add('749291623170572428');
    }

    return message.channel.send(`Add @mention to imprison a user.`);
  }
});

1 个答案:

答案 0 :(得分:0)

由于您的函数,<span data-reactid="36">At close: 3:30PM IST</span> 返回一个getUserFromMention对象,而不是User对象。

更改:

GuildMember

使用:

return client.users.cache.get(mention);

// Remember to pass the message as the second parameter, as the message is undefined currently.
return message.guild.members.cache.get(mention);
相关问题