【问题标题】:How to enter a variable in a string in node.js/discord.js?如何在 node.js/discord.js 中的字符串中输入变量?
【发布时间】:2020-02-28 12:01:27
【问题描述】:

我正在使用 discord.js 模块在 NodeJS 中创建一个不和谐机器人,并且我想在用户发送特定文本命令的同一频道中发送预定义的消息。 例如。

const token = 'xyz';
const client = new Discord.Client();

client.on('message', (message) => {
    if (message.content === '!hi') {
        message.channel.send('Hello ${message.author}!');
    };
});

client.on('ready', () => {
    console.log('Bot is now connected');

    //  client.channels.find(x => x.name === 'test').send('Hello I\'m now connected.');
});

client.login(token);```

client.on('message', (message) => {
    if (message.content === '!hi') {
        message.channel.send('Hello ${message.author}!');    }});

我希望输出为Hello @discordusername!,但我得到的是Hello ${message.author}!

【问题讨论】:

标签: node.js discord.js


【解决方案1】:

对于 javascript 中的字符串插值,使用反引号而不是单引号。 例如:

`Hello ${message.author}!`

【讨论】:

    【解决方案2】:

    改变这个:

    message.channel.send('Hello ${message.author}!');

    用这个:

    message.channel.send(`Hello ${message.author}!`);

    【讨论】:

      【解决方案3】:
      message.channel.send(`Hello <@${message.author.tag}>!`)
      

      【讨论】:

      • @Gorgan-Stoyanov's answer from last year 中提出的相同语法相比,包含.tag 属性和&lt;@ … &gt; 包装器有什么好处?一个更可靠吗?这是否修复了一个错误?还是只是一种风格偏好?
      猜你喜欢
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2012-05-24
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多